2010-11-24 5 views
3

우리는 interop 문제에 직면 해 있습니다. 우리는 클라이언트 EXE를 C#으로 작성하고 있습니다. COM dll과 네이티브 C++ 정적 라이브러리로 작성된 레거시 코드가 있습니다. 우리는 C# 클라이언트의 기능을 완성하기 위해이 두 가지 모두를 사용해야했습니다. 우리는 interop을 사용하여 COM dll에 대한 참조를 추가했으며 C# 코드 내에 COM 클래스의 인스턴스를 만들 수있었습니다. 이제이 COM 클래스 메소드는 nactive C++의 객체 인 인수를 취합니다. COM 클래스의 메소드 중 일부는 C++ 정적 라이브러리에서 선언 된 클래스의 객체 인 인수를 필요로합니다. C#에서 C++ 네이티브 클래스의 인스턴스를 만들 수 없기 때문에 C++/CLI 래퍼를 네이티브 클래스에 작성하고 C# 코드로 워퍼 인스턴스를 만들고 워퍼를 통해 네이티브 클래스의 인스턴스에 액세스하여 COM 클래스에 전달하기로 결정했습니다. C# 클라이언트에서 만들었습니다. 문제는 우리가 네이티브 객체 포인터를 (IntPtr로서) COM 클래스에 전달할 때 네이티브 객체가 그 값으로 초기화되지 않는다는 것입니다. 무엇이 문제 일 수 있겠습니까? C# 코드에 관리되는 C++ 래퍼를 통해 원시 객체를 어떻게 다시 전달합니까?C++ 래퍼를 사용하는 관리 코드의 네이티브 C++ 인스턴스

//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 


} 
+0

'COMObject.Initialize (objPtr)'을 읽지 않아야합니까? –

+0

고마워요 –

답변

1

같은 원시 포인터를 걸리는 COM 방법은 아니다 자동화 호환. 매우 드문 경우이지만 COM 메서드는 이런 종류의 시나리오에서 COM 인터페이스 포인터를 사용합니다. 형식 라이브러리를 변환 한 후에 Object Browser를 사용하여 확인하는 것이 좋습니다. 인수 유형이 인 객체 일 때 문제가 발생합니다.

C++/CLI에서 COM 인터페이스 코드를 유지하는 것이 더 나을 것입니다.

0

초기화는 COM 메서드가 아니며, 실제로는 네이티브 C++ 개체 인 IntPtr 인수를 취하는 자체 메서드입니다.

관련 문제