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. Trouble in ATL

Trouble in ATL

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpdatabasecomtools
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.
  • N Offline
    N Offline
    novachen
    wrote on last edited by
    #1

    Hi! I try to wrap a c++ lib behind a COM dll to make it easy to use by VB or c#. But i have some troubles. Look the demo: There is two classes in COM DLL, one called Apple and another called Banana. In Apple i try to hide a struct. It is not on the interface to the client. But the other class in the COM know it is there, and can access it. struct Image { int num; }; // CApple class ATL_NO_VTABLE CApple : public CComObjectRootEx, public CComCoClass, public IDispatchImpl { public: Image img; CApple() { } The class banana has two methods, GetApple create an Apple object and initialize its value, QueryApple try to query the value in the Apple. interface IBanana : IDispatch{ [id(1), helpstring("GetApple")] HRESULT GetApple([out,retval] IApple** apple); [id(2), helpstring("QueryApple")] HRESULT QueryApple([in] IApple* apple, [out,retval] int * ret); }; And in banana.cpp i try STDMETHODIMP CBanana::GetApple(IApple** apple) { CApple* p = new CComObject(); p->img.num = 100; *apple = p; return S_OK; } STDMETHODIMP CBanana::QueryApple(IApple* apple, int* ret) { CApple* p = dynamic_cast(apple); *ret = p->img.num; return S_OK; } But when i use this dll in c#, it always get a System.ExecutionEngineException. the script is FruitLib.Banana banana = new FruitLib.BananaClass(); FruitLib.Apple apple = banana.GetApple(); int n = banana.QueryApple(apple); What's wrong in my code? Any idea? Thanks!

    M 1 Reply Last reply
    0
    • N novachen

      Hi! I try to wrap a c++ lib behind a COM dll to make it easy to use by VB or c#. But i have some troubles. Look the demo: There is two classes in COM DLL, one called Apple and another called Banana. In Apple i try to hide a struct. It is not on the interface to the client. But the other class in the COM know it is there, and can access it. struct Image { int num; }; // CApple class ATL_NO_VTABLE CApple : public CComObjectRootEx, public CComCoClass, public IDispatchImpl { public: Image img; CApple() { } The class banana has two methods, GetApple create an Apple object and initialize its value, QueryApple try to query the value in the Apple. interface IBanana : IDispatch{ [id(1), helpstring("GetApple")] HRESULT GetApple([out,retval] IApple** apple); [id(2), helpstring("QueryApple")] HRESULT QueryApple([in] IApple* apple, [out,retval] int * ret); }; And in banana.cpp i try STDMETHODIMP CBanana::GetApple(IApple** apple) { CApple* p = new CComObject(); p->img.num = 100; *apple = p; return S_OK; } STDMETHODIMP CBanana::QueryApple(IApple* apple, int* ret) { CApple* p = dynamic_cast(apple); *ret = p->img.num; return S_OK; } But when i use this dll in c#, it always get a System.ExecutionEngineException. the script is FruitLib.Banana banana = new FruitLib.BananaClass(); FruitLib.Apple apple = banana.GetApple(); int n = banana.QueryApple(apple); What's wrong in my code? Any idea? Thanks!

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

      Firstly, when posting code to CodeProject, it's useful to use the <pre> format tag. You should also translate < to &lt; and > to &gt;. Alternatively select the 'Display this message as-is (no HTML) checkbox). Otherwise CP interprets template parameters and system includes as unknown HTML tags and removes them. Now for the problem: System.ExecutionEngineException, according to the documentation, is only thrown when an internal problem occurs in the runtime itself. The problem seems to be occurring when the Interop layer tries to call QueryApple. It never actually gets into the C++ module. I'll have to confess, I'm not an expert on Interop. I think you need actual support from MS on this one.

      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