[英]How to define unmanaged dll dependency in C#
I want to link an unmanaged C++ library to a C# app. 我想将非托管C ++库链接到C#应用程序。 I am using the PInvoke process because the unmanaged C++ dll has multiple dependencies that won't compile with CLR.
我正在使用PInvoke进程,因为非托管C ++ dll具有多个依赖项,这些依赖项无法使用CLR进行编译。 When I compile the example code below, I am getting the following errors.
当我编译下面的示例代码时,出现以下错误。 I did find a reference that I need to add the dll reference, but MSVS tells me it can't add it.
我确实找到了需要添加dll引用的引用,但是MSVS告诉我它无法添加它。 I also read about registering it with the regsvr32, but that appears to be specific to CLR libraries, right?
我还阅读了有关在regsvr32中进行注册的信息,但这似乎特定于CLR库,对吗? So my question is, how do I get clear this error for a unmanaged dll?
所以我的问题是,如何清除非托管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();
}
and the C++ DLL's Component.h: 和C ++ DLL的Component.h:
typedef int (__stdcall * Callback)(const char* text);
Callback Handler=0;
class COM_Component : public CM_Component
{
// Contents not pasted
}
and the C++ DLL's Component.cpp: 和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
[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();
Remove the ;
删除
;
in the lines after the DllImport
attribute. 在
DllImport
属性之后的行中。
Your "invalid token" compiler errors are due to the semicolon immediately after the DllImport attributes. 您的“无效令牌”编译器错误是由于DllImport属性之后的分号引起的。 Additionally, you're specifiying a verbatim @"..." string with double backslashes in it.
此外,您正在指定一个带有双反斜杠的逐字@“ ...”字符串。 I think your declaration should look like:
我认为您的声明应如下所示:
[DllImport(@"..\lib\DDS_Service.dll", EntryPoint="SetCallback")]
private static extern void SetCallback(Callback fn);
If your DLL is a COM dll, then you can run regsvr32 to register it, and add a reference to it in your project. 如果您的DLL是COM dll,则可以运行regsvr32进行注册,并在项目中添加对其的引用。 If that's the case, then you don't need to use P/Invoke: you would be able to reference it like any other library.
如果是这种情况,那么您就不需要使用P / Invoke:您将能够像其他任何库一样引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.