我想將非託管C++庫鏈接到C#應用程序。我正在使用PInvoke進程,因爲非託管C++ dll具有多個不能用CLR編譯的依賴項。當我編譯下面的示例代碼時,出現以下錯誤。我找到了一個需要添加dll參考的參考,但MSVS告訴我它不能添加它。我也讀過關於使用regsvr32註冊它,但這似乎是特定於CLR庫,對吧?所以我的問題是,我如何清除這個錯誤的非託管DLL?如何在C#中定義非託管dll依賴關係
ServerTerminal.cs(62,48): error CS1031: Type expected
ServerTerminal.cs(62,48): error CS1519: Invalid token ';' in class, struct, or interface member declaration
ServerTerminal.cs(64,48): error CS1031: Type expected
ServerTerminal.cs(64,48): error CS1519: Invalid token ';' in class, struct, or interface member declaration
ServerTerminal.cs:
class ServerTerminal
{
private delegate int Callback(string text);
private Callback mInstance;
public ServerTerminal()
{
mInstance = new Callback(Handler);
SetCallback(mInstance);
}
public void Test()
{
TestCallback();
}
private int Handler(string text)
{
return 0;
}
[DllImport(@"..\\lib\\DDS_Service.dll", EntryPoint="SetCallback")];
private static extern void SetCallback(Callback fn);
[DllImport(@"..\\lib\\DDS_Service.dll", EntryPoint="TestCallback")];
private static extern void TestCallback();
}
和C++ DLL的Component.h:
typedef int (__stdcall * Callback)(const char* text);
Callback Handler=0;
class COM_Component : public CM_Component
{
// Contents not pasted
}
和C++ DLL的Component.cpp:
extern "C" __declspec(dllexport)
void __stdcall SetCallback(Callback handler)
{
Handler = handler;
}
extern "C" __declspec(dllexport)
void __stdcall TestCallback()
{
int retval = Handler("hello world");
}
COM_Component::COM_Component(void) : CM_Component(TDstring("COM_Component"))
{
// register the observer callback methods
}
// Remainder of file not pasted
具體來說,當我嘗試將DLL引用映射到C#項目時,出現的錯誤是:「無法添加對dll的引用。請確保該文件是可訪問的,並且它是有效的程序集或COM組件「。 –
刪除屬性 – erikkallen