private ushort RegisterWndClass(string name) { NativeMethods.WNDCLASS newClass = new NativeMethods.WNDCLASS { lpszClassName = name, hInstance = hInstance, style = 0x8, lpfnWndProc = wndProcDelegate }; return(NativeMethods.RegisterClass(ref newClass)); }
private void CreateWindow() { this.m_proc = new NativeMethods.WndProc(this.CustomProc); var wndClass = new NativeMethods.WNDCLASS(); wndClass.lpszClassName = this.m_uniqueName; wndClass.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(this.m_proc); var resRegister = NativeMethods.RegisterClass(ref wndClass); var resError = Marshal.GetLastWin32Error(); if (resRegister == 0 && resError != NativeMethods.ERROR_CLASS_ALREADY_EXISTS) throw new Exception(); this.m_customHwnd = NativeMethods.CreateWindowEx(0, this.m_uniqueName, null, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); }
/// <summary> /// Once the classname and style bits have been set, this can be called to register the class. /// </summary> private unsafe void RegisterClass() { NativeMethods.WNDCLASS windowClass = new NativeMethods.WNDCLASS(); string localClassName = _className; if (localClassName == null) { // If we don't use a hollow brush here, Windows will "pre paint" us with COLOR_WINDOW which // creates a little bit if flicker. This happens even though we are overriding wm_erasebackgnd. // Make this hollow to avoid all flicker. windowClass.hbrBackground = Gdi32.GetStockObject(Gdi32.StockObject.HOLLOW_BRUSH); windowClass.style = _classStyle; _defaultWindProc = DefaultWindowProc; localClassName = "Window." + Convert.ToString((int)_classStyle, 16); } else { // A system defined Window class was specified, get its info if (!UnsafeNativeMethods.GetClassInfoW(NativeMethods.NullHandleRef, _className, ref windowClass)) { throw new Win32Exception(Marshal.GetLastWin32Error(), SR.InvalidWndClsName); } localClassName = _className; _defaultWindProc = windowClass.lpfnWndProc; } _windowClassName = GetFullClassName(localClassName); _windProc = new User32.WNDPROC(Callback); windowClass.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_windProc); windowClass.hInstance = Kernel32.GetModuleHandleW(null); fixed(char *c = _windowClassName) { windowClass.lpszClassName = c; if (UnsafeNativeMethods.RegisterClassW(ref windowClass) == 0) { _windProc = null; throw new Win32Exception(); } } }
private void CreateWindow() { this.m_proc = new NativeMethods.WndProc(this.CustomProc); var wndClass = new NativeMethods.WNDCLASS(); wndClass.lpszClassName = this.m_uniqueName; wndClass.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(this.m_proc); var resRegister = NativeMethods.RegisterClass(ref wndClass); var resError = Marshal.GetLastWin32Error(); if (resRegister == 0 && resError != NativeMethods.ERROR_CLASS_ALREADY_EXISTS) { throw new Exception(); } this.m_customHwnd = NativeMethods.CreateWindowEx(0, this.m_uniqueName, null, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); }
public static extern short RegisterClass(NativeMethods.WNDCLASS wc);
protected override void Run(Action readyCallback) { const string className = "HidSharpDeviceMonitor"; NativeMethods.WindowProc windowProc = DeviceMonitorWindowProc; var wc = new NativeMethods.WNDCLASS() { ClassName = className, WindowProc = windowProc }; RunAssert(0 != NativeMethods.RegisterClass(ref wc), "HidSharp RegisterClass failed."); var hwnd = NativeMethods.CreateWindowEx(0, className, className, 0, NativeMethods.CW_USEDEFAULT, NativeMethods.CW_USEDEFAULT, NativeMethods.CW_USEDEFAULT, NativeMethods.CW_USEDEFAULT, NativeMethods.HWND_MESSAGE, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); RunAssert(hwnd != IntPtr.Zero, "HidSharp CreateWindow failed."); var hidNotifyHandle = RegisterDeviceNotification(hwnd, NativeMethods.HidD_GetHidGuid()); var bleNotifyHandle = RegisterDeviceNotification(hwnd, NativeMethods.GuidForBluetoothLEDevice); #if BLUETOOTH_NOTIFY var bleHandles = new List <BleRadio>(); // FIXME: We don't handle the removal of USB Bluetooth dongles here, as far as notifications go. IntPtr searchHandle, radioHandle; var searchParams = new NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS() { Size = Marshal.SizeOf(typeof(NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS)) }; searchHandle = NativeMethods.BluetoothFindFirstRadio(ref searchParams, out radioHandle); if (searchHandle != IntPtr.Zero) { do { var radio = new BleRadio(); radio.RadioHandle = radioHandle; radio.NotifyHandle = RegisterDeviceNotification(hwnd, radioHandle); bleHandles.Add(radio); }while (NativeMethods.BluetoothFindNextRadio(searchHandle, out radioHandle)); NativeMethods.BluetoothFindRadioClose(searchHandle); } if (bleHandles.Count > 0) { HidSharpDiagnostics.Trace("Found {0} Bluetooth radio(s).", bleHandles.Count); } #endif //_bleDiscoveryThread = new Thread(BleDiscoveryThread) { IsBackground = true, Name = "HidSharp BLE Discovery" }; //_bleDiscoveryThread.Start(); _serialWatcherShutdownEvent = NativeMethods.CreateManualResetEventOrThrow(); _serialWatcherThread = new Thread(SerialWatcherThread) { IsBackground = true, Name = "HidSharp Serial Watcher" }; _serialWatcherThread.Start(); _hidNotifyObject = new object(); _serNotifyObject = new object(); _bleNotifyObject = new object(); _notifyThread = new Thread(DeviceMonitorEventThread) { IsBackground = true, Name = "HidSharp RaiseChanged" }; _notifyThread.Start(); readyCallback(); NativeMethods.MSG msg; while (true) { int result = NativeMethods.GetMessage(out msg, hwnd, 0, 0); if (result == 0 || result == -1) { break; } NativeMethods.TranslateMessage(ref msg); NativeMethods.DispatchMessage(ref msg); } //lock (_bleDiscoveryThread) { _bleDiscoveryShuttingDown = true; Monitor.Pulse(_bleDiscoveryThread); } lock (_notifyThread) { _notifyThreadShuttingDown = true; Monitor.Pulse(_notifyThread); } NativeMethods.SetEvent(_serialWatcherShutdownEvent); //_bleDiscoveryThread.Join(); _notifyThread.Join(); _serialWatcherThread.Join(); UnregisterDeviceNotification(hidNotifyHandle); UnregisterDeviceNotification(bleNotifyHandle); #if BLUETOOTH_NOTIFY foreach (var bleHandle in bleHandles) { UnregisterDeviceNotification(bleHandle.NotifyHandle); NativeMethods.CloseHandle(bleHandle.RadioHandle); } #endif RunAssert(NativeMethods.DestroyWindow(hwnd), "HidSharp DestroyWindow failed."); RunAssert(NativeMethods.UnregisterClass(className, IntPtr.Zero), "HidSharp UnregisterClass failed."); GC.KeepAlive(windowProc); }