marshalling COM to .NET problem
-
Could someone please help with marshalling to .NET. I posted this question here, not in C#, 'cause I know for sure the mistake is somewhere in .idl or cpp file. I have a Win32 C++ project with the following three files: //tlbsamp.cpp/////////////////////////////////////////////////// #include interface ITest{bool MyFunc();}; class Test:public ITest{ int i; public: Test(){i=10;} int MyFunc(){return i;} }; ITest* __stdcall MyDll_CreateTest(){ return new Test(); } int __stdcall MyDll_CreateInt(){ return rand();} BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) {return TRUE;} //tlbsamp.def////////////////////////////////////////////////////////// LIBRARY TLBSamp DESCRIPTION 'Sample DLL' EXPORTS MyDll_CreateTest MyDll_CreateInt //tlbsamp.idl///////////////////////////////////////////////////////////// import "oaidl.idl"; import "ocidl.idl"; [ uuid(983EE63A-7FB0-494a-AD63-14F48347DB7A), lcid(0x0409), version(1.0) ] library TLBSample { [object,uuid( 1F707072-05C6-4501-AD74-CFD57AB8002C)] interface ITest:IUnknown {int MyFunc ();} [ version(1.0), dllname("TLBSamp.dll")] module MyDllFunctions { [entry("MyDll_CreateTest")] ITest* __stdcall CreateTest(); [entry("MyDll_CreateInt")] int _stdcall CreateInt(); } }; I am compiling the project and get tlbsamp.dll ,tlbsamp.tlb, tlbsamp.lib, then I create a c# project, AddReference->COM-> Browse, all the .tlb file. In object browser I can see my ITest interface with MyFunk function. Here's the c# code. using System; using System.Runtime.InteropServices; using TLBSample; namespace Test_Call_MSDN_Dll { class Class1 { [DllImport(@"E:\MyProgs\NET\TLBSamp\debug\tlbsamp.dll")] public static extern TLBSample.ITest MyDll_CreateTest(); [DllImport(@"E:\MyProgs\NET\TLBSamp\debug\tlbsamp.dll")] public static extern Int32 MyDll_CreateInt(); [STAThread] static void Main(string[] args) { Console.WriteLine(MyDll_CreateInt().ToString());//OK-works fine ITest spClass=MyDll_CreateTest();//ExecutionEngineException(???) Console.WriteLine(spClass.MyFunc().ToString()); Сonsole.Read(); } } } What am I doing wrong? Thanks in advance!
-
Could someone please help with marshalling to .NET. I posted this question here, not in C#, 'cause I know for sure the mistake is somewhere in .idl or cpp file. I have a Win32 C++ project with the following three files: //tlbsamp.cpp/////////////////////////////////////////////////// #include interface ITest{bool MyFunc();}; class Test:public ITest{ int i; public: Test(){i=10;} int MyFunc(){return i;} }; ITest* __stdcall MyDll_CreateTest(){ return new Test(); } int __stdcall MyDll_CreateInt(){ return rand();} BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) {return TRUE;} //tlbsamp.def////////////////////////////////////////////////////////// LIBRARY TLBSamp DESCRIPTION 'Sample DLL' EXPORTS MyDll_CreateTest MyDll_CreateInt //tlbsamp.idl///////////////////////////////////////////////////////////// import "oaidl.idl"; import "ocidl.idl"; [ uuid(983EE63A-7FB0-494a-AD63-14F48347DB7A), lcid(0x0409), version(1.0) ] library TLBSample { [object,uuid( 1F707072-05C6-4501-AD74-CFD57AB8002C)] interface ITest:IUnknown {int MyFunc ();} [ version(1.0), dllname("TLBSamp.dll")] module MyDllFunctions { [entry("MyDll_CreateTest")] ITest* __stdcall CreateTest(); [entry("MyDll_CreateInt")] int _stdcall CreateInt(); } }; I am compiling the project and get tlbsamp.dll ,tlbsamp.tlb, tlbsamp.lib, then I create a c# project, AddReference->COM-> Browse, all the .tlb file. In object browser I can see my ITest interface with MyFunk function. Here's the c# code. using System; using System.Runtime.InteropServices; using TLBSample; namespace Test_Call_MSDN_Dll { class Class1 { [DllImport(@"E:\MyProgs\NET\TLBSamp\debug\tlbsamp.dll")] public static extern TLBSample.ITest MyDll_CreateTest(); [DllImport(@"E:\MyProgs\NET\TLBSamp\debug\tlbsamp.dll")] public static extern Int32 MyDll_CreateInt(); [STAThread] static void Main(string[] args) { Console.WriteLine(MyDll_CreateInt().ToString());//OK-works fine ITest spClass=MyDll_CreateTest();//ExecutionEngineException(???) Console.WriteLine(spClass.MyFunc().ToString()); Сonsole.Read(); } } } What am I doing wrong? Thanks in advance!
Ick. I don't think the P/Invoke and COM marshallers work well together. Use one or the other, but not both. Stability. What an interesting concept. -- Chris Maunder