Skip to main content
GameDev.net gamedev.net
🔒 Locked

.NET Interop

Started by Plerion Jun 13, 2010 at 4:52 PM 1 replies 800+ views
Original Post
Plerion
Plerion
Hello everyone!

Im playing a bit around with some Marshalling and other stuff in .NET. I found the nice function Marshal::GetIUnknownForObject and thought "Hey, that might be interesting". So i wrote a simple interface <-> class pair in C# and an intermediate DLL in C++/CLI like the following:

C#:
namespace Com{    [Guid("6261039D-6CEE-481D-A166-E072A4E97913")]    [ComVisible(true)]    public interface IInterop    {        void DummyProc();    }    [Guid("5EC7BA19-E7B1-4FAE-90C5-6E3F219994D3")]    [ComVisible(true)]    public class ClsInterop : IInterop    {        public ClsInterop()        {        }        public void DummyProc()        {        }    }}


C++/CLI:
void* GetInterface(){	Com::IInterop^ inter = gcnew Com::ClsInterop();	IntPtr pInterface = Marshal::GetIUnknownForObject(inter);	return pInterface.ToPointer();}#pragma unmanagedextern "C" __declspec(dllexport) void* QueryInterface() { return GetInterface(); }#pragma managed


Now ive testet that in a native C++-application like that:
#include <Windows.h>#include <iostream>typedef void* (*tProc)();class __declspec(uuid("{6261039D-6CEE-481D-A166-E072A4E97913}")) IInterop : public IUnknown{public:	virtual void DummyProc() = 0;};int main(){	HMODULE hMod = LoadLibrary("Intermediate.dll");	tProc proc = (tProc)GetProcAddress(hMod, "QueryInterface");	IUnknown* pUnk = (IUnknown*)proc();	IInterop* pInterop = 0;	pUnk->QueryInterface(IID_PPV_ARGS(&pInterop));	std::cout << pInterop << std::endl;	pInterop->DummyProc();	std::cin.sync();	std::cin.get();}


This works nice until the call to DummyProc. pInterop is not NULL and QueryInterface obviously worked (changed one number of the declspec(uuid()) and then it returned 0, so it really seems to accept it. But why is it crashing when i call the DummyProc? Hopefully someone can help me!

/Edit:
Weird: If i attach the VS Debugger (F5) to the exe i dont get an error, the window just closes when i hit pInterop->DummyProc().

Greetings
Plerion

[Edited by - Plerion on June 13, 2010 5:15:00 PM]
Plerion
Plerion
Ok, ive found out that the interface is completly correct. The error occurs when the handle goes back to the CLR. So the code crashes in
CLR.dll:IsInProcCCWTearOff

Sadly google says me "No results found for IsInProcCCWTearOff" :p

IsInProcCCWTearOff:6033EE08  mov         edi,edi  6033EE0A  push        ebp  6033EE0B  mov         ebp,esp  6033EE0D  mov         eax,dword ptr [ebp+8]  6033EE10  mov         eax,dword ptr [eax]  6033EE12  mov         eax,dword ptr [eax] <----- Crash (seems like __vfptr is incorrect)  6033EE14  cmp         eax,offset Unknown_QueryInterface (602D255Dh)  6033EE19  jne         IsInProcCCWTearOff+13h (60245CE6h)  6033EE1F  xor         eax,eax  6033EE21  inc         eax  6033EE22  pop         ebp  6033EE23  ret         4  
Plerion
Plerion
I had a wrong interface it seems. i created a TLB from the .NET-assembly and imported that TLB and used the generated interface and now it works.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.