pinvoke - How to define unmanaged dll dependency in C# -
i want link unmanaged c++ library c# app. using pinvoke process because unmanaged c++ dll has multiple dependencies won't compile clr. when compile example code below, getting following errors. did find reference need add dll reference, msvs tells me can't add it. read registering regsvr32, appears specific clr libraries, right? question is, how clear error 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 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 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 observer callback methods } // remainder of file not pasted
your "invalid token" compiler errors due semicolon after dllimport attributes. additionally, you're specifiying verbatim @"..." string double backslashes in it. think declaration should like:
[dllimport(@"..\lib\dds_service.dll", entrypoint="setcallback")] private static extern void setcallback(callback fn);
if dll com dll, can run regsvr32 register it, , add reference in project. if that's case, don't need use p/invoke: able reference other library.
Comments
Post a Comment