Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. marshalling COM to .NET problem

marshalling COM to .NET problem

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++comhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 929285
    wrote on last edited by
    #1

    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!

    M 1 Reply Last reply
    0
    • U User 929285

      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!

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups