Myca A.
Myca A.

Reputation: 81

How to define unmanaged dll dependency in C#

I want to link an unmanaged C++ library to a C# app. I am using the PInvoke process because the unmanaged C++ dll has multiple dependencies that won't compile with 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. I also read about registering it with the regsvr32, but that appears to be specific to CLR libraries, right? So my question is, how do I get clear this error for a unmanaged 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:

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:

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

Upvotes: 2

Views: 647

Answers (2)

adjan
adjan

Reputation: 13652

 [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.

Upvotes: 2

pmcoltrane
pmcoltrane

Reputation: 3112

Your "invalid token" compiler errors are due to the semicolon immediately after the DllImport attributes. 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. 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.

Upvotes: 2

Related Questions