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?
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.
Short answer:
No
Long answer:
Create a C callable export (iow including the instance parameter).
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.
精彩评论