forked from walterlv/ManipulationDemo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dotnet-campus/t/lindexi_dotnet
加上更加详细的设备插拔信息
- Loading branch information
Showing
5 changed files
with
221 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFrameworks>netcoreapp3.1;net45</TargetFrameworks> | ||
<UseWPF>true</UseWPF> | ||
<StartupObject>ManipulationDemo.Program</StartupObject> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="System.Management" Version="4.5.0" /> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFrameworks>net6.0-windows;net45</TargetFrameworks> | ||
<UseWPF>true</UseWPF> | ||
<LangVersion>latest</LangVersion> | ||
<StartupObject>ManipulationDemo.Program</StartupObject> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="System.Management" Version="4.5.0" Condition="'$(TargetFramework)'=='net45'"/> | ||
<PackageReference Include="System.Management" Version="6.0.2" Condition="'$(TargetFramework)'=='net6.0-windows'"/> | ||
<PackageReference Include="Microsoft.Windows.CsWin32" PrivateAssets="all" Version="0.3.18-beta" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Update="Properties\Settings.Designer.cs"> | ||
<DesignTimeSharedInput>True</DesignTimeSharedInput> | ||
<AutoGen>True</AutoGen> | ||
<DependentUpon>Settings.settings</DependentUpon> | ||
</Compile> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Update="Properties\Settings.settings"> | ||
<Generator>SettingsSingleFileGenerator</Generator> | ||
<LastGenOutput>Settings.Designer.cs</LastGenOutput> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GetPointerDevices |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Runtime.InteropServices; | ||
using System; | ||
|
||
namespace ManipulationDemo | ||
{ | ||
/// <summary> | ||
/// Copy From: https://stackoverflow.com/questions/1976573/using-registerdevicenotification-in-a-net-app | ||
/// </summary> | ||
internal static class UsbNotification | ||
{ | ||
public const int DbtDevicearrival = 0x8000; // system detected a new device | ||
public const int DbtDeviceremovecomplete = 0x8004; // device is gone | ||
public const int WmDevicechange = 0x0219; // device change event | ||
public const int DbtDevtypDeviceinterface = 5; | ||
private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB devices | ||
private static IntPtr _notificationHandle; | ||
|
||
/// <summary> | ||
/// Registers a window to receive notifications when USB devices are plugged or unplugged. | ||
/// </summary> | ||
/// <param name="windowHandle">Handle to the window receiving notifications.</param> | ||
public static void RegisterUsbDeviceNotification(IntPtr windowHandle) | ||
{ | ||
DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface | ||
{ | ||
DeviceType = DbtDevtypDeviceinterface, | ||
Reserved = 0, | ||
ClassGuid = GuidDevinterfaceUSBDevice, | ||
Name = 0 | ||
}; | ||
|
||
dbi.Size = Marshal.SizeOf(dbi); | ||
IntPtr buffer = Marshal.AllocHGlobal(dbi.Size); | ||
Marshal.StructureToPtr(dbi, buffer, true); | ||
|
||
_notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0); | ||
} | ||
|
||
/// <summary> | ||
/// Unregisters the window for USB device notifications | ||
/// </summary> | ||
public static void UnregisterUsbDeviceNotification() | ||
{ | ||
UnregisterDeviceNotification(_notificationHandle); | ||
} | ||
|
||
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] | ||
private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags); | ||
|
||
[DllImport("user32.dll")] | ||
private static extern bool UnregisterDeviceNotification(IntPtr handle); | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
private struct DevBroadcastDeviceinterface | ||
{ | ||
internal int Size; | ||
internal int DeviceType; | ||
internal int Reserved; | ||
internal Guid ClassGuid; | ||
internal short Name; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
public struct DEV_BROADCAST_HDR | ||
{ | ||
public int dbch_size; | ||
public int dbch_devicetype; | ||
public int dbch_reserved; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
public struct DEV_BROADCAST_DEVICEINTERFACE | ||
{ | ||
public int dbcc_size; | ||
public int dbcc_devicetype; | ||
public int dbcc_reserved; | ||
public Guid dbcc_classguid; | ||
//public string dbcc_name; | ||
} | ||
} | ||
} |