Original Post
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#:
C++/CLI:
Now ive testet that in a native C++-application like that:
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]
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 managedNow 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]