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#
  4. A question in Implement COM interface in C# [modified]

A question in Implement COM interface in C# [modified]

Scheduled Pinned Locked Moved C#
c++csharpcomhelpquestion
1 Posts 1 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.
  • G Offline
    G Offline
    G Richard
    wrote on last edited by
    #1

    Experts, I want to implement some COM interfaces in C#, and these interface will be called by a DLL written by ATL. The COM interface definations are : __interface IDataRelation : IUnknown { ... HRESULT GetData( [in] long Item, [in] REFIID riid, [out, iid_is(riid), retval] void **Data); } __interface IArray : IUnknown { ... } __interface INumber : IArray { .... HRESULT GetUnits([out, retval] BSTR *Units); } __interface IDoubleNumber : INumber { ... } The native code will call IDataRelation.GetData() to get the pointer of INumber to get the data unit. I rewrite the interfaces in C# [ComImport, Guid(XXX), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IArray {...} [ComImport, Guid(XXX), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface INumber : IArray { ... [Return : MarshalAs(UnmanagedType.BSTR)] string GetUnit(); } [ComImport, Guid(XXX), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IDoubleNumber : INumber {...} [ComImport, Guid(XXX), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IDataRelation { [Return : MarshalAs(UnmanagedType.Interface)] object GetData( [In, MarshalAs(UnmanagedType.I4)] int Item, [In, MarshalAs(UnmanagedType.Struct)] ref Guid riid); } Then I construct two classes to implement these interfaces. public class ArrayBase : IArray, INumber, IDoubleNumber { string INumber.GetUnit() { return m_Unit; } } public class Data : ArrayBase, IDataRelatin { ... public object GetData(int Item, ref Guid riid); { return this; } } Every thing seems fine, IDataRelation can be retrived in the C++ code. CComQIPtr pRelation = GetDataRelation(...); //Get data relation if(pRelation != NULL) { CComQIPtr testNumber; pRelation->GetData(0, UUIDOF(INumber), reinterpret_cast( &testNumber )); if(testNumber != NULL) { CComBSTR m_DataUnit; testNumber->GetUnit(&m_DataUnit); //Error !!! } } When I call GetUnit, a strange error occured, see the following detail : Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declar

    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