C++ managed code calling unmanaged code with handles

1

After an afternoon of searching this problem has got me stumped! I'm writing a managed C++ application which needs to call some unmanaged code. This is the function I need to call:

public: void Connect(
    [out] LONG* pCookieID, 
    [out] LONG* pNumberOfStreams, 
    [out] VARIANT* pMediaType
);

And the documentation has this note: Parameter pMediaType must be set to null in C# and nothing in VB .NET before calling Connect, otherwise an exception with error code DISP_E_TYPEMISMATCH = 0x80020005 will be thrown.

So I need to pass 3 handles to the function for it to return data in them. The two LONG handles seem reasonably straightforward but I can't figure out how to pass a handle for a variant.

This is where I've got to:

int ^cookieID;
int ^numberOfStreams;
System::Object ^buffer;

GCHandle hcookieID = GCHandle::Alloc(cookieID, GCHandleType::Pinned);
GCHandle hnumberOfStreams = GCHandle::Alloc(numberOfStreams, 
    GCHandleType::Pinned);
GCHandle hbuffer = GCHandle::Alloc(buffer, GCHandleType::Pinned);

parser->Connect(hcookieID.AddrOfPinnedObject().ToInt32(),
    hnumberOfStreams.AddrOfPinnedObject().ToInt32(),
    (System::Object^)hbuffer.AddrOfPinnedObject().ToInt32());

hcookieID.Free();
hnumberOfStreams.Free();
hbuffer.Free();

The compiler accepts this but when the Connect method is called I get the following error:

"Type mismatch. (Exception from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))"} System::Exception^

I've searched high and low and tried all sorts of solutions but I can't find any.

c++
.net
unmanaged
asked on Stack Overflow Dec 15, 2010 by Adam • edited May 31, 2016 by J. Chomel

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0