/// <summary> /// Initializes a new instance of the <see cref="DirectInput"/> class. /// </summary> private DirectInput(IntPtr hwnd) { IntPtr dinput8; var iid = SafeNativeMethods.IID_IDirectInput8W; var clsid = SafeNativeMethods.CLSID_DirectInput8; var hinst = SafeNativeMethods.GetModuleHandleW(IntPtr.Zero); SafeNativeMethods.CoCreateInstance(ref clsid, IntPtr.Zero, 1, ref iid, out dinput8); this.directInput = dinput8; this.hwnd = hwnd; unsafe { DirectInput8Inst *inst = (DirectInput8Inst *)dinput8.ToPointer(); var initialize = (InitializeInputProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->Initialize, typeof(InitializeInputProc)); var res = initialize(dinput8, hinst, 0x0800); this.release = (NoParamProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->Release, typeof(NoParamProc)); this.enumDevices = (EnumDevicesProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->EnumDevices, typeof(EnumDevicesProc)); this.createDevice = (CreateDeviceProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->CreateDevice, typeof(CreateDeviceProc)); } this.releaseHandle = GCHandle.Alloc(this.release, GCHandleType.Normal); }
/// <summary> /// Initializes a new instance of the <see cref="DirectInputDevice"/> class. /// </summary> /// <param name="device">The native device interface.</param> /// <param name="input">The DirectInput instance which owns this device.</param> internal DirectInputDevice(IntPtr device, DirectInput input) { this.device = device; this.input = input; unsafe { var inst = (DirectInputDevice8Inst *)device.ToPointer(); this.release = (NoParamProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->Release, typeof(NoParamProc)); var setCoopLevel = (SetCooperativeLevelProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->SetCooperativeLevel, typeof(SetCooperativeLevelProc)); var res = setCoopLevel(device, input.WindowHandle, 6); var setDataFormat = (SetDataFormatProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->SetDataFormat, typeof(SetDataFormatProc)); var dataFormat = new DIDATAFORMAT() { dwSize = (uint)sizeof(DIDATAFORMAT), dwObjSize = (uint)sizeof(DIOBJECTDATAFORMAT), dwFlags = 1, dwDataSize = (uint)sizeof(DIJOYSTATE), dwNumObjs = 6, rgodf = DeviceObjectTypes.DataFormat }; setDataFormat(device, new IntPtr(&dataFormat)); this.getDeviceState = (GetDeviceStateProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->GetDeviceState, typeof(GetDeviceStateProc)); this.acquire = (NoParamProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->Acquire, typeof(NoParamProc)); this.unacquire = (NoParamProc)Marshal.GetDelegateForFunctionPointer(inst->Vtbl->Unacquire, typeof(NoParamProc)); } this.releaseHandle = GCHandle.Alloc(release, GCHandleType.Normal); }