passing parameters in OLE Automation
David Dworkin -- ddworkin@wpine.com
Monday, December 09, 1996
Environment: VC++ 4.2b, Win 95, NT 4.0
Hi all,
I'm new to OLE and have a question on passing arguments through
OLE automation methods.
I'm using ClassWizard to define my methods for me. I have one outgoing
method which is kind of a general event notification method, so I have a type
and to parameters. Depending on the type, the parameters can be ints,
pointers to strings, pointer to data structures, etc. I'm kind of confused as
to what to make these.
1. Do I make them VARIANTs, and then use the variant API's to fill them in
and extract data from them? (and I'd know that the variant represents a string
or whatever based on the type)
2. In ClassWizard, one option is to make them LPVARIANTs. When would
I want to do this, vs. just making them variants?
3. How, exactly, would I go about passing some data structure (ie some structure
that consists of 3 ints, a char *, etc) I haven't found real good programming
examples of this
4. If I'm just passing a null-terminated string, can I pick "char *" from
ClassWizard and treat the argument as a string, or do I need to do VARIANTS.
Who's responsible (in the former case) for allocating/deallocating the data?
Sorry if these are basic questions. Thanks for your time,
David Dworkin
ddworkin@wpine.com
============================================================================
David Dworkin ddworkin@wpine.com
White Pine Software www.wpine.com (WWW)
542 Amherst Street Phone: 603-886-0903 x376
Nashua NH 03063 Fax: 603-886-9051
============================================================================
Doug Brubacher -- Doug_Brubacher@compuware.com
Friday, December 13, 1996
David,
I've been using the ATL to create my OLE Automation server so I can't
say I can answer all your questions, but I'll try to help.
When defining output parameters the MIDL compiler forces me to use
pointers to variables therefore if I'm returning a VARIANT I use a
VARIANT* or the LPVARIANT you mention.
You are definitely on the right track with using Variants if you are
going to be returning different variable types depending on the "type"
parameter.
As for returning structures, OLE Automation does not directly support
returning of User Defined structures/data types because it could not
possible know how to marshal such a thing. Not to worry though, you
can return a SAFEARRAY, and in your case you would want to return a
SAFEARRAY of type VARIANT where each element of the array represents a
specific member in your data structure. This obviously forces the
server and client to adhere to a common understanding of the array but
it does work.
There is an example Called MFCARRAY available on Microsoft's ftp site
which demonstrates all the basics (Just search for MFCARRAY at
Microsoft's web site.
Hope this helps, you can e-mail me at Doug_Brubacher@compuware.com if
you are still puzzled and I'll try to explain it in more detail.
Doug Brubacher
DouglasB@msn.com
Doug Brubacher -- Doug_Brubacher@compuware.com
Monday, December 16, 1996
David,
I've been using the ATL to create my OLE Automation server so I can't
say I can answer all your questions, but I'll try to help.
When defining output parameters the MIDL compiler forces me to use
pointers to variables therefore if I'm returning a VARIANT I use a
VARIANT* or the LPVARIANT you mention.
You are definitely on the right track with using Variants if you are
going to be returning different variable types depending on the "type"
parameter.
As for returning structures, OLE Automation does not directly support
returning of User Defined structures/data types because it could not
possible know how to marshal such a thing. Not to worry though, you
can return a SAFEARRAY, and in your case you would want to return a
SAFEARRAY of type VARIANT where each element of the array represents a
specific member in your data structure. This obviously forces the
server and client to adhere to a common understanding of the array but
it does work.
There is an example Called MFCARRAY available on Microsoft's ftp site
which demonstrates all the basics (Just search for MFCARRAY at
Microsoft's web site.
Hope this helps, you can e-mail me at Doug_Brubacher@compuware.com if
you are still puzzled and I'll try to explain it in more detail.
Doug Brubacher
DouglasB@msn.com
Mark Kurt -- mark@toolsfordesign.com
Tuesday, December 17, 1996
>
> I'm new to OLE and have a question on passing arguments through
> OLE automation methods.
This may be a case of the blind leading the blind then, but I'll give it a
shot.
>
> 3. How, exactly, would I go about passing some data structure (ie some
structure
> that consists of 3 ints, a char *, etc) I haven't found real good
programming
> examples of this
>
There have been some pretty good discussions of this on the DCOM list
(archives at http://microsoft.ease.lsoft.com/archives/dcom.html) and there
may have been a couple on this list (archives at
http://www.visionx.com/mfcpro/mfc-l/index.htm). From what I can gather,
and I what I am currently working on, you might want to try passing back
the IDispatch of a "structure" where the IDispatch has properties
(get/set methods) for the elements in the structure. This works nicely
with MFC because of the GetIDispatch and FromIDispatch methods
provided with CCmdTarget derived classes.
However, I am sure that there are some performance issues
that I am not qualified to answer. If you are not necessarily
worried about being VB friendly and have some time to learn
midl, you can use custom interfaces and these can return
structures without having to write custom marshaling
as long as the structures are defined within the .idl
of the interface.
(I started doing it this way, but then decided that the
IDispatch method was "cleaner" for what I was working on).
> 4. If I'm just passing a null-terminated string, can I pick "char *" from
> ClassWizard and treat the argument as a string, or do I need to do
VARIANTS.
> Who's responsible (in the former case) for allocating/deallocating the
data?
>
I believe that char* is really a pointer to 1 single character, if you want
to
pass strings you should use BSTR* or LPCTSTR. These can be used
very easily with MFC's CString.
| Вернуться в корень Архива
|