2

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
Myca A.
  • 81
  • 7
  • Specifically, the error I get when I try to map the DLL reference to the C# project is: "A reference to dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component." – Myca A. Jun 27 '14 at 16:28
  • Remove the semicolon after the attribute – erikkallen Jun 27 '14 at 16:39

2 Answers2

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

adjan
  • 13,371
  • 2
  • 31
  • 48
2

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.

pmcoltrane
  • 3,052
  • 1
  • 24
  • 30