pinvoke c++ method?

S

superfluent

Is there any way of calling a method in an instance of a c++ class
(coded with evc4) in CF2? I have done this on the desktop by using a
mix of static factory functions (that create and return a pointer to an
instance), and then using the dllimprt attributes and specifying the
magled export "EntryPoint" and the calling convention
"CallingConvention=CallingConvention.ThisCall" for the methods I need.
However, the calling convetion does not seem to exist in CF2.

Is it possible to call methods directly without creating static
wrappers for each and every function in the class?
 
P

Paul G. Tobey [eMVP]

Since you don't have a way to make an instance of the class, no, not really.
You could cobble something together that might work, as you did on the
desktop (adding 'this' as one of the parameters, in the right place in the
parameter list). No, there's no way to call into native code without
declaring the types of the methods/functions and their parameters (of
course).

If this unmanaged code is so critical to your application, write your
application in unmanaged code...

Paul T.
 
H

Henrik Viklund

Paul said:
If this unmanaged code is so critical to your application, write your
application in unmanaged code...

Huh? Rather than switching to unmanaged code alltogether, I'd suggest
first looking into the possibility to create a wrapper/adapter dll that
exposes a CF friendly pinvokable interface to the c++ dll.

The following suggestion might or might not be a possible solution
depending on the structure and complexity of the c++ dll (and how much
logic you are willing to put into the wrapper dll), but i've done this
with great success a couple of times:

Create a wrapper dll with factory functions that creates instances of a
particular class and return the pointer to it. Then mirror each class'
methods with static functions that also take the "this" pointer as
argument. In .net CF I create wrapper classes that calls the factory
functions upon creation and holds the corresponding "this-pointer" as a
private member. Create wrapper class methods that call the static
dll-functions with the contained "this-pointer" as argument.

Henrik Viklund
 

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