SimpleButPerfect
SimpleButPerfect

Reputation: 1639

Calling member level functions from a dynamic link library using pinvoke in C#?

How would I use DLLImport pinvoke to invoke a function i wrote in a class in an unmanaged DLL? It always throws that the entry point doesn't exist in the dll. EX:

class Foo
{
  int __declspec(dllexport) Bar() {return 0;}
};

Bar is in the Foo class. when I use pinvoke as:

[DLLImport("Test.dll")]
public static extern int Bar();

When using it i get an exception saying that the entry point does not exist in the DLL. Is it possible to call functions directly from classes?

Upvotes: 0

Views: 538

Answers (3)

Arve
Arve

Reputation: 7506

Not easily...

To call a member function, the first "hidden" argument has to be a pointer to the C++ class who's member function you are calling.

And C++ functions are name mangeled, so you need to find the name mangeled name of the function you are calling.

In short: It is easier to create a C++/CLI wrapper of your C++ class to do this.

Upvotes: 1

rerun
rerun

Reputation: 25495

You are going to have to find the mangled name. You can use dumpbin /exports but 'im not sure that the calling convention will work through pinvoke.

Upvotes: 1

leppie
leppie

Reputation: 117220

Short answer:

No

Long answer:

Create a C callable export (iow including the instance parameter).

Upvotes: 1

Related Questions