Currently I’m trying to use a C++ library under C# using DLL importation. Library is called Interception.
The problem is that I don’t know how to translate #define entries and typedef declaration of the header file:
https://github.com/oblitum/Interception/blob/master/include/interception.h
I tried to use “using” directive, but with no success (I can’t access to the void definition).
Moreover, I didn’t understood the role of __declspec(dllimport) in this header. In my c# project, I just ignored it? Is it good to do that?
This is the code I want to use in c# (it’s a sample of the library)
https://github.com/oblitum/Interception/blob/master/samples/hardwareid/main.cpp
EDIT:
What I’ve tried: basic importation:
[DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)]
void interception_set_filter(void* context, InterceptionPredicate predicate, ushort filter);
I don’t know ho to convert InterceptionPredicate. According the header file, InterceptionFilter is a ushort, and InterceptionContext is a void pointer (void*).
First, it looks like you’re trying to implement a global keyboard/mouse hook .. if that’s the case, I’d recommend googling ‘C# low level keyboard and mouse hook’.
Now for your question, first is the
__declspec(dllimport)issue: this would be if you were actually using the header in a C++ application, that is the C++ equivilent of the C#DllImport.. so in effect you didn’t ignore it, you implemented it. In C++ it just tells the linker that the function declared as such will be imported from a specific DLL instead of it being a local function (pretty similar to what the C#DllImportdirective does)Next is for function pointer issue (InterceptionPredicate). In the header it is defined as such:
And
InterceptionDeviceis just an ‘int’. So the InterceptionPredicate is just a function pointer type (or Delegate in C#), so your delegate definition for InterceptionPredicate would look like this:A note about the UnmanagedFunctionPointer calling convention descriptor: IF you know what kind of calling convention (stdcall, fastcall, cdecl) the exported function might be using, you could specify here so that the .NET marshaler will know how to pass the data between the managed/unmanaged code, but if you don’t know it or it’s not specified typically you can just leave that off.
Also, as others have mentioned, unless you have the ‘unsafe’ flag specified in your C# properties, a
void*type should always be anIntPtrin C#.Also, be sure to mark the dll function in your C# code as
public static extern, see example below.So to make an example of the function you’ve specified, here’s what could be done:
Hope that gets you on the right track.