does PInvoke support for polymorphism ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I want to use my unmanaged c++ class library from a c# client.
my unmanaged c++ class library use polymorpism, is this polymorphism also
exported to my c# client via PInvoke ?

thanks.
 
yaron said:
Hi,

I want to use my unmanaged c++ class library from a c# client.
my unmanaged c++ class library use polymorpism, is this polymorphism
also exported to my c# client via PInvoke ?

I'm assuming that you're asking "Can I call a virtual function through a
pointer to a native C++ object via PInvoke?". The answer to that is no (or
at least, not directly). MC++ IJW can, however.

To call virtual functions through PInvoke you'd need to write a little
"trampoline" function and export it from your native DLL. The function
would be a non-member, taking the object pointer and all of the virtual
methods parameters as it's parameters and would perform the virtual function
call.

-cd
 
Hi Daniel,

I will give a small scenario:
my unmanaged c++ dll receive method return a pointer to a generic message
base class but it actually can be many different concrete messages class
which derive from the generic message.
i want from my c# client to deal with the concrete message class.

can i cast down the generic message pointer to the concrete message and
access the concrete message fields from c#?

Thanks.
 
yaron said:
Hi Daniel,

I will give a small scenario:
my unmanaged c++ dll receive method return a pointer to a generic
message base class but it actually can be many different concrete
messages class which derive from the generic message.
i want from my c# client to deal with the concrete message class.

can i cast down the generic message pointer to the concrete message
and access the concrete message fields from c#?

You'd better do it with a managed C++ (or C++/CLI) adaptation layer :
P/Invoke is well suited for C-style APIs, but it may be difficult to use it
in an OO context.

Arnaud
MVP - VC
 
thanks a lot, it was very helpfull.


Arnaud Debaene said:
You'd better do it with a managed C++ (or C++/CLI) adaptation layer :
P/Invoke is well suited for C-style APIs, but it may be difficult to use it
in an OO context.

Arnaud
MVP - VC
 
Back
Top