how to pass interface pointer to a method in a DLL

R

Rui Mota

hello there!

I've been trying to translate some code in C++ into C#.

I've an object in a DLL wich has a Method that recieves an Interface pointer
as parameter.
something like this:

HRESULT _stdcall MyMethod([out] unsigned long* nElem, [out] IMyData**
aData);

In C++ I simply do this:

....
ULONG nElem;
MyLib::IMyData* aData = null;
myobj->MyMethod( &nElem, &aData);
....

but when I add the same object in the references in a C# project the method
becomes something like this:

void MyMethod(out uint nElem, System.IntPtr aData);

The method is not accepting my 'MyLib.IMyData aData' as a parameter and the
value IntPtr cannot be null.
I realy don't know how to parse MyLib.IMyData interface into a IntPtr Type.


Can you give me a clue?
 
M

Mattias Sjögren

Rui,
but when I add the same object in the references in a C# project the method
becomes something like this:

void MyMethod(out uint nElem, System.IntPtr aData);

That's strange. If the method signature is really what you say it is
in the typelib, Tlbimp shouldn't produce such a signature. Are you
sure the last parameter isn't really a void**?

Anyway, you can hopefully edit the interop assembly and change the
signature to take an out IMyData parameter instead. See instructions
at

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeditinginteropassembly.asp



Mattias
 
R

Rui Mota

hello there,

first of all thanks 'coz the link you sent helped me a lot but I still have
a problem.
the value returned is an array of IMyData and the
Marshal.PtrToStructure(...) method only gets the first item.

I'm getting disapointed with C# 'coz I've to write more code than only 3
lines in C++.
 
M

Mattias Sjögren

the value returned is an array of IMyData

Oh, OK. Then you should probably change the parameter type to IntPtr[]
and then call Marshal.GetObjectFromIUnknown for each of the returned
pointers.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top