ich suche ein Tutorial, in dem gezeigt wird, wie man aus C# heraus die Methoden einer C++ Klasse aufruft, die in einer nativen DLL komplimiert sind
meine C++ Klasse
class __declspec( dllexport )MyClass
{
bool foo()
{
return true;
}
};
Du hast 4 Optionen (in der Reihenfolge des wenigstens Aufwandes):
* PInvoke mit thiscall und mangled names
* baue einen managed Wrapper in Managed C++
* umgebe die Klasse mit einem COM-Wrapper
* baue eine Wrapper-DLL, die alle Methoden als Funktionen rausreicht, das Objekt als zusätzlichen Parameter
Ohne großen Aufwand kann man nur herkömmliche DLLs (ohne Klassen) aufrufen.
http://msdn.microsoft.com/msdnmag/issues/05/09/CAtWork/
Hier was zum thiscall-Aufruf:
A managed class contains an integer member, whose address I want to pass to an unmanaged function. This means that the address of the managed instance must not change for the duration of the unmanaged function call. I create a pinning pointer, which locks the object in memory, and can then pass the address of a data member to the function. When the pinning pointer is zeroed out—or goes out of scope—the instance will be free to move.
Calling Exported C++ Member Functions
You’ll need to take special care when calling functions in DLLs that are class members. When a C++ class member function is compiled, the compiler generates a mangled name—a name for the function that encodes its actual name, plus details of the class it belongs to, and its argument and return types. If you use Platform Invoke to call such a function, you’ll need to specify the mangled name and use the appropriate calling convention.
Here is an example that shows how to call a C++ member function. First let’s look at the declaration of the class and the exported function:
class MyClass
{
public:
int Square(int i) { return i*i; }
};
And here is the Platform Invoke prototype you can use to call it:
[DllImport("MyDll.dll", EntryPoint="?Square@MyClass@@QAEHH@Z",
CallingConvention=CallingConvention::ThisCall)]
extern "C" int Square(IntPtr pThis, int i);
Tip
You can find the exported name of a C++ member function by using the Dumpbin.exe utility with the /exports option to examine the DLL.
The EntryPoint parameter gives the exported name of the member function, but the programmer can call it using the more friendly name Square. Because the function is a C++ member function but is not static, it needs to be passed a pointer to an object of the appropriate type, which will act as the this pointer for the function. You can see the this pointer being passed as the first parameter. You also need to choose the correct calling convention: for C++ member functions, this will be the ThisCall convention, which assumes that the first parameter in the call will be the this pointer.
ThisCall
The first parameter is the this pointer and is stored in register ECX. Other parameters are pushed on the stack. This calling convention is used to call methods on classes exported from an unmanaged DLL.
hört sich interessant an - werde ich mal testen