Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>COM's way of passing callbacks is interfaces. Trying to match unmanaged function pointers with .NET delegates is complicated and error-prone, as you've experienced. Interfaces are not as practical as delegates, but still better than function pointers.</p> <p>So if I were you I'd put the callbacks in a COM interface exported by the COM DLL:</p> <p>(The following is IDL code, which must go in the .idl file associated with the C++ project.)</p> <pre><code>interface ISomeObject : IUnknown { HRESULT DoTask1([in] int i); HRESULT DoTask2([in] BSTR s); } </code></pre> <p>Then build the C++ project, and add the type library as a reference from the C# project. If the type library is registered, you can add it by right-clicking the C# project name in the <strong>Solution Explorer</strong> pane of Visual Studio, select <strong>Add Reference</strong>, go to the <strong>COM</strong> tab, look for the name of the type library and add it as a reference.</p> <p>Once you have added a reference to the type library, you can use the COM interface as if it were a C# interface:</p> <pre><code>class MyForm : AnotherForm, ISomeObject { // ISomeObject methods: public void DoTask1(int i) { ... } public void DoTask2(string s) { ... } ... } </code></pre> <p>Then InitDotNet would take a ISomeObject pointer, and the C# code would simply call it by passing <strong>this</strong>:</p> <p>C++:</p> <pre><code>ISomeObject* g_pSomeObject; extern "C" __declspec(dllexport) void __stdcall InitDotNet(ISomeObject* o) { g_pSomeObject = o; } </code></pre> <p>C#:</p> <pre><code>[DllImport("mycppdll.dll")] private static extern void InitDotNet(ISomeObject o); private void DoInitDotNet() { // The following works because MyForm implements ISomeObject InitDotNet(this); } </code></pre> <p>But I would also make InitDotNet a method of a COM interface and not a global function.</p> <p>Last but not least, what's the purpose of the VB class? If it's only purpose is only to wrap the COM class, you don't need it: COM classes/interfaces are directly consumable from C#.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload