0

我一直在让 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 没有像 C 中的 wchar_t 那样正确填充。值为 0,这意味着它从未提供过正确的信息指针,对吧?我能做些什么来解决这个问题?任何帮助深表感谢。

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

4

1 回答 1

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.
于 2013-11-21T22:43:24.310 回答