[英]When calling an exported class of a C++ DLL by C#, C-style string members of the class are OK in one exported function, but not in the other
[英]Can I export a C++ interface with C-Style methods to a C# dll
我正在嘗試避免使用COM。 我正在C ++ exe上設計混合的C#和C ++控件。
我想出的一種方法是從C#調用C ++ exe,然后將Windows消息發送到C#Windows。 但是,我在控件基類上調用的方法數量太長,無法證明Windows消息的合理性。
因此,如果可以將整個C#接口導出到C ++ exe,那將更加容易。
我想避免使用COM,因為我可能必須支持Windows 2000,並且不依賴清單文件來進行COM部署會給當前在注冊表中設置不多的軟件包帶來麻煩。
您可以為每個C ++控件編寫一個C包裝程序,並從C#中編寫PInvoke。
例如,此C ++類:
class Example
{
public:
int MyMethod(int param);
}
並在c ++ exe的extern“ C”塊中:
void * CreateExample() { return new Example(); }
int Example_MyMethod(void * handle, int param) { reinterpret_cast<Example*>(handle)->MyMethod(param)); }
並在C#中:
public class Example
{
private IntPtr handle;
public Example()
{
handle = _CreateExample();
}
public int MyMethod(int param)
{
return _MyMethod(param);
}
[DllImport("yourdll.exe")]
private static extern IntPtr _CreateExample();
[DllImport("yourdll.exe")]
private static extern int _MyMethod(IntPtr handle, int param);
}
我在VS 2010中使用C ++ / CLI來做到這一點。 您可以公開一個可以以常規方式使用的DLL C接口,該實現只會將數據從該接口封送至您的C#程序集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.