Re: Pass arguments with IDispatch.Invoke

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I'm not familiar with C# and COM Interop so can't comment...

The right group for this kind of questions is:

microsoft.public.dotnet.framework.interop

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

<brosembob@xxxxxxxxx> wrote in message
news:1157122628.723159.14970@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks guys for your help.
Regarding the solution that I found previously; Is it Ok to do it this
way?

// Following is the C# method
[MethodImpl(MethodImplOptions.Synchronized)]
public int GetVersion(IntPtr LibraryVersion)
{
//LibraryVersion is the pointer in unmanaged memory
MY_VERSION v =
(MY_VERSION)Marshal.PtrToStructure(LibraryVersion,typeof (MY_VERSION));

v.major = 1;
v.minor = 2;
v.patchLevel = 3;
v.build = 4;

Marshal.StructureToPtr(v, LibraryVersion, true);
}

I'm getting pointer to the structure from C, and using Marshalling
populating it and send it back.
The structure seems fine/populated on the C side after.

Thanks,
Bob

Alexander Nickolov wrote:
Additionally, a struct cannot be returned as an [out, retval]
parameter. You must use [in, out], pass a struct at the caller
and modify it at the callee. The problem is a VARIANT does
_not_ own the struct it points to (it's like VT_BYREF in this
regard). Thus it cannot be properly disposed of at the marshaling
layer.

This is briefly mentioned at the FAQ article Igor already pointed
you to:

http://vcfaq.mvps.org/com/4.htm

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:OrNJ5mHzGHA.1292@xxxxxxxxxxxxxxxxxxxxxxx
brosembob@xxxxxxxxx wrote:
Thanks for your reply and help. I'm not quite sure I understand the
example.

long value; // the [out] value will end up here
VARIANT v;
V_VT(&v) = VT_I4 | VT_BYREF;
V_I4REF(&v) = &value;
// pack v into DISPPARAMS

What is the value? Is it pointer to my structute?

I gave an example for packing [in, out] long parameter. For UDT,
'value'
would be of that UDT type, and you would need to set up VT_RECORD |
VT_BYREF variant.

However, as Alexander pointed out, return values ([out, retval]
parameters) work completely differently. You just pass a pointer to an
empty VARIANT as varResult parameter to Invoke (comes right after
DISPPARAMS), and when Invoke returns the variant is filled with data.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925





.



Relevant Pages

  • Re: optional Safearray of UDTs from VB
    ... > phEngine.DoProcess InstMeasure 'VB ARRAY / safearray parameter ... > "Alexander Nickolov" wrote in message ... >> I think I already told you to use a VARIANT, ... >> Microsoft MVP, MCSD ...
    (microsoft.public.vc.atl)
  • Re: Extracting Data in C++ VARIANT that was created by VB
    ... > learning exercise I am trying using a VARIANT instead and this is where I ... > "Alexander Nickolov" wrote in message ... >> Microsoft MVP, MCSD ...
    (microsoft.public.vc.atl)
  • Re: How to casting to VARIANT*
    ... Microsoft MVP, MCSD ... but I wonder why MSWORD is designed this way. ... > in there that just take a plain VARIANT. ... > optional are also passed by pointer. ...
    (microsoft.public.vc.atl)
  • Re: Getting Value from and [out] parameter.
    ... Microsoft MVP, MCSD ... With sufficient thrust, pigs fly just fine. ... Alexander Nickolov wrote: ... You need to pass a variant of type VT_BSTR | VT_BYREF, ...
    (microsoft.public.win32.programmer.ole)
  • Re: Pass arguments with IDispatch.Invoke
    ... Marshal.StructureToPtr(v, LibraryVersion, true); ... I'm getting pointer to the structure from C, ... Alexander Nickolov wrote: ... The problem is a VARIANT does ...
    (microsoft.public.vc.atl)