-2

I have a COM Dll written in unmanaged C++ that expose a com interface. The trivial sample of idl is shown below:

import "oaidl.idl";
import "ocidl.idl";

[
    object,
    uuid(A806FAED-FCE2-4F1B-AE67-4B36D398508E),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IObjectToPass : IDispatch{
    [propget, id(1)] HRESULT Name([out, retval] BSTR* pVal);
    [propput, id(1)] HRESULT Name([in] BSTR newVal);
};
[
    uuid(B99537C0-BEBC-4670-A77E-06AB5350DC23),
    version(1.0),
]
library     {
    importlib("stdole2.tlb");
    [
        uuid(FB7C7D08-0E38-40D2-A160-0FC8AE76C3CF)      
    ]
    coclass ObjectToPass
    {
    [default] interface IObjectToPass;
    };
}; 

so IObjectToPass interface is the interface exposed. Now I've implemented a .NET library (ObjectConsumer) in C# that import via an Interop created by tlbimp this COM object so I can use the IObjectToPass interface. Here below the sample code of this library:

using Interop.ComTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;ComTestLib

using System.Threading.Tasks;

namespace ObjectConsumer 
{
    public class Consumer
    {
        public void PassObject(IObjectToPass passobj)
        {
            string str = passobj.Name;
        }
    }
}

both c++ and c# module compiles without problem

Now I've created a simple console application in c++ wich support CLR that import both ComTestLib via import and Consumer adding a reference to console application project. My goal is to get an instance of ObjectToPass inside my console application and pass to consumer calling the public method PassObject as show below

#include "stdafx.h"
#import "..\Debug\ComTest.tlb" no_namespace, raw_interfaces_only  

int main()
{
    CoInitialize(NULL);
    IObjectToPassPtr obj;

    HRESULT hr = obj.CreateInstance(__uuidof(IObjectToPass) );
    ObjectConsumer::Consumer^ consumer = gcnew ObjectConsumer::Consumer();
    obj->put_Name(_T("MyName"));
    consumer->PassObject(obj);

    CoUninitialize();
        return 0;
 }

This code won't compile. Compiler cannot convert argument 1 from 'IObjectToPassPtr' to 'Interop::ComTest::IObjectToPass ^'. I've made different attempts but it seems that IObjectToPass* cannot be casted to 'Interop::ComTest::IObjectToPass^. Is there a way to do it?

Alessandro Ciurlo
  • 102
  • 1
  • 2
  • 11
  • I've simulated the scenario inside I've to move, too complex to explain here. I've a thirdy part com object which is passed from the main application to a com unmanaged dll I need to replace this unmanged dll with a .NET one so I've to pass and use the thirdy part com object. – Alessandro Ciurlo Feb 03 '17 at 16:48

1 Answers1

-1

Skip the COM interface and just export a factory function in c++.

#define MY_API __declspec(dllexport)
#ifdef __cplusplus
#   define EXTERNC    extern "C"
#else
#   define EXTERNC 
#endif

 EXTERNC MY_API IObjectToPass* APIENTRY MyFactory(void);
 EXTERNC MY_API  void APIENTRY SomeFunction(IObjectToPass* handle);

// Implementation:

MY_API IObjectToPass* APIENTRY MyFactory(void)
{
    return new ObjectToPass();
}
MY_API void APIENTRY SomeFunction(IObjectToPass* handle)
{
    handle->DoFunction();
}

// And then export any needed functions too. 

Now in C#:

public class MyWrapper{
IntPtr myReference;
public ObjectWrapper()
    {
        try
        {

            this.myReference = MyFactory();
            if (myReference == IntPtr.Zero)
            {

                throw "Error or something";
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            throw e;
        }

    }

    //Path to dll
    [DllImport(lib_path)]
    private static extern IntPtr MyFactory();

    [DllImport(lib_path)]
    private static extern void SomeFunction(IntPtr toTrigger);
 }

Consider getting rid of AT LEAST one of the three forms of C to simplify the design or else what I suggested does work and I have used it before!

sbail95
  • 198
  • 8