将C dll和方法导入c#

时间:2013-11-10 22:39:49

标签: c# c pointers dll

我在C#中使用C方法时遇到了问题 - 在C中编译并运行正常,返回设备ID的正确结果 - 但是对于Windows,函数似乎没有正确构建字符串。 C中有效的源代码如下:来自oblita.com上的interception.dll示例:

int main()
{
using namespace std;

InterceptionContext context;
InterceptionDevice device;
InterceptionStroke stroke;

wchar_t hardware_id[500];

raise_process_priority();

context = interception_create_context();

interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);
interception_set_filter(context, interception_is_mouse, INTERCEPTION_FILTER_MOUSE_LEFT_BUTTON_DOWN);

while(interception_receive(context, device = interception_wait(context), &stroke, 1) > 0)
{
    if(interception_is_keyboard(device))
    {
        InterceptionKeyStroke &keystroke = *(InterceptionKeyStroke *) &stroke;

        if(keystroke.code == SCANCODE_ESC) break;
    }

    size_t length = interception_get_hardware_id(context, device, hardware_id, sizeof(hardware_id));

    if(length > 0 && length < sizeof(hardware_id))
        wcout << hardware_id << endl;

    interception_send(context, device, &stroke, 1);
}

interception_destroy_context(context);

return 0;

现在,我已经在C#中导入了DLL,它应该正确地覆盖所有变量转换:

    [DllImport("interception.dll", EntryPoint = "interception_get_hardware_id", CallingConvention = CallingConvention.Cdecl)]
    public static extern int GetHardwareID(IntPtr context, int device, ref StringBuilder idbuffer, uint buffer_size);

这里是我试图解雇的代码,以获取C#中的设备ID:

     {
        IntPtr context;
        int device;
        Interception.Stroke stroke = new Interception.Stroke();

        context = Interception.CreateContext();
        Interception.SetFilter(context, Interception.IsKeyboard, Interception.Filter.All);
        StringBuilder hardwareID = new StringBuilder();

        while (Interception.Receive(context, device = Interception.Wait(context), ref stroke, 1) > 0)
        {

            int hardwareID_length = Interception.GetHardwareID(context, device, ref hardwareID, Convert.ToUInt32(hardwareID.Length));
            if (hardwareID_length > 0 && hardwareID_length < Convert.ToUInt32(hardwareID.Length))
            Console.WriteLine("ID result: ", hardwareID);

            Console.WriteLine("SCAN CODE: {0}/{1}", stroke.key.code, stroke.key.state);

            if (stroke.key.code == ScanCode.X)
            {
                stroke.key.code = ScanCode.Y;
            }
            Interception.Send(context, device, ref stroke, 1);

            // Hitting escape terminates the program
            if (stroke.key.code == ScanCode.Escape)
            {
                break;
            }
        }

        Interception.DestroyContext(context);
    }
}

if语句中的块永远不会触发 - 它不能正确打印ID,我认为这意味着stringbuilder没有正确填充,就像wchar_t将在C中一样。值为0,这意味着它从未被输入指针正确的信息,对吧?我该怎么做才能解决这个问题?非常感谢任何帮助。

应该注意的是,扫描码工作正常 - 它可以检测击键,甚至可以按预期用y替换x。

1 个答案:

答案 0 :(得分:0)

由于我自己的挖掘和大量的反复试验,以下是如何使其发挥作用:

{
[DllImport("interception.dll", EntryPoint = "interception_get_hardware_id", CallingConvention = CallingConvention.Cdecl)]
    public static unsafe extern int GetHardwareID(IntPtr context, int device, out void* idbuffer, uint buffer_size);

//Note the call is unsafe - because we're using the void* pointer.

{(inside an unsafe block of code)

void* hardwareID;
//call function as follows:
int result = Interception.GetHardwareID(context, device, out hardwareID, 5000);
//i use 5000 because it works. i'm sure there's better ways to do it but i don't want to spend another few months on figuring it out.
IntPtr hardwareConverted = new IntPtr(hardwareID);
//this marshals the void* into an IntPtr that you can play around with as normal, in managed memory.