jasonpang / Interceptor

C# wrapper for a Windows keyboard driver. Can simulate keystrokes and mouse clicks in protected areas like the Windows logon screen (and yes, even in games). Wrapping http://oblita.com/Interception
MIT License
286 stars 84 forks source link

How to get Hardware ID of Keyboard? #15

Closed VollRahm closed 4 years ago

VollRahm commented 5 years ago

I want to get the Hardware ID of the Keyboard in the OnKeyPressed Event. In interception there's a method called interception_get_hardware_id and there's an example for it. How can I use it?

calico-crusade commented 4 years ago

This might be a tad late, but here is the solution I used to get the HWID from Interception. It requires changing the data type of "hardwareIdentifier" on the external method used to fetch the HWID. Using a string causes a memory access violation, however, using a byte[] worked fine for me.

Here is the modification to the InterceptionDriver.cs class:

[DllImport("interception.dll", EntryPoint = "interception_get_hardware_id", CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 GetHardwareId(IntPtr context, Int32 device, byte[] hardwareIdentifier, UInt32 sizeOfString);

Then you would use it like so:

public string GetHwid(int device, IntPtr context, int inputLength = 500)
{
    var bytes = new byte[inputLength];
    int length = InterceptionDriver.GetHardwareId(context, device, bytes, (uint)bytes.Length);
    if (length > inputLength)
        return null;

    int al = length / 2;
    return Encoding.Unicode.GetString(bytes).Substring(0, al);
}

I'm using 500 for the input length as shown in the sample: here.

I hope this helped! If you have figured out a better method, please let me know!

VollRahm commented 4 years ago

Thanks, this worked