Native c++ instance in managed code using C++ wrapper
We are facing interop problem, we are writing client exe in C#, we have some legacy code written in COM dll and one native C++ static library. We required to use both of them to complete functionality in C# client.We have added referance to COM dll using interop and able to create instanc开发者_开发技巧e of COM class inside C# Code. Now this COM class methods takes arguments which are objects of nactive C++, Some of the methods in COM class needs arguments which are objects of classes declared in c++ static library. Since we will not able to create instance of C++ native classes in C#, We decided to write C++/CLI wrapper on native class and create instance of warpper in C# code and access instance of native class through warpper and pass it to COM class created in C# client. Problem is when we pass native object pointer (as IntPtr) to COM class we don't get native object initialized to its values. what could be the problem? How we pass back native object through managed c++ wrapper 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
}
A COM method that takes a raw pointer like that isn't Automation compatible. It is quite unusual, COM methods take COM interface pointers in this kind of scenario. You'd better check with Object Browser what it looks like after the type library got converted. You've got a problem when the argument type is object.
You might be better off keeping the COM interface code in C++/CLI as well.
I've never used COMObject.Initialize(x), not sure what it does.
However, If you want to get the IUnknown out of your IntPtr you can do this:
object pUnk = Marshal.GetObjectForIUnknown(objPtr);
see also: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getobjectforiunknown.aspx
Initialize is not COM method, it is the our own method, which takes as argument IntPtr which is actually a native C++ object.
精彩评论