Source C ++ instance in managed code using C ++ shell

We are faced with the interaction problem, we write client exe in C #, we have some inherited code written in COM dll and one native C ++ static library. We demanded to use both of them to perform functions in the C # client. We added a link to COM-dll using interop and were able to create an instance of the COM class inside C # code. Now these COM class methods take arguments that are inactive C ++ objects. Some of the methods in a COM class need arguments that are objects of classes declared in the C ++ static library. Since we won’t be able to instantiate native C ++ classes in C #, we decided to write a C ++ / CLI wrapper in our native class and create a warpper instance in C # code and access the native class instance through warpper and pass it to the COM class created on a C # client.The problem is when we pass our own pointer to an object (like IntPtr) in the COM class, we do not get our own object initialized with its values. What could be the problem? How do we pass our own object through a managed C ++ shell to C # code?

 //Natvie C++ class
 class __declspec(dllexport) CConfiguration
 {

    public :
         CConfiguration(void);
         virtual ~CConfiguration(void);
         void SetIPAddress(const char *IPAddress);
         void SetPort(const char*Port);
         void GetIPAddress(char *IPAddress);
         void GetPort(char *Port);
    Private:
         std::string IPAddress;
         std::string Port;

 }


//Managed C++ Class  
public ref class ManagedConfigruation
{
         public :
        ManagedConfigruation(){}
        ~ManagedConfigruation(){}
         CConfiguration  *myConfiguration;          
              IntPtr  GetObjectOfConfigurationPtr();     
}

IntPtr ManagedConfigruation::GetObjectOfConfigurationPtr()
{
     myConfiguration = new CConfiguration();
     myConfiguration.SetIPAddress("127.0.0.1");
     myConfiguration.SetPort("6200");
     //Convert native object to IntPtr and return to C# class
     return System::IntPtr(myConfiguration);
};

 //C# class on client exe
 public class CSharpClass
{

    //Wrapper of Managed C++ class
    ManagedConfiguration objManagedConfiguration = new ManagedConfiguration();
    IntPtr objPtr = objManagedConfiguration.GetObjectOfConfigurationPtr();

    //Belwoo COMObject needs object of type CConfiguration native C++ class    
    COMObject.Initialize(objPtr);  //Here is the problem object does not contain anything


}
+3
3

COM, , Automation. , COM- COM- . Object Browser, . , .

, COM- ++/CLI.

+1

COMObject.Initialize(x), , .

, IUnknown IntPtr, :

object pUnk = Marshal.GetObjectForIUnknown(objPtr);

. : http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getobjectforiunknown.aspx

0

Initializing is not a COM method, it is our own method, which takes IntPtr as an argument, which is actually a native C ++ object.

0
source

Source: https://habr.com/ru/post/1776759/


All Articles