本文整理汇总了C#中DEV_BROADCAST_DEVICEINTERFACE类的典型用法代码示例。如果您正苦于以下问题:C# DEV_BROADCAST_DEVICEINTERFACE类的具体用法?C# DEV_BROADCAST_DEVICEINTERFACE怎么用?C# DEV_BROADCAST_DEVICEINTERFACE使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DEV_BROADCAST_DEVICEINTERFACE类属于命名空间,在下文中一共展示了DEV_BROADCAST_DEVICEINTERFACE类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterDeviceNotifications
public RegisterDeviceNotifications(IntPtr hWnd, Guid gCat)
{
category = gCat;
var di = new DEV_BROADCAST_DEVICEINTERFACE();
// Register to be notified of events of category gCat
di.dbcc_size = Marshal.SizeOf(di);
di.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
di.dbcc_classguid = gCat;
hdevnotify = RegisterDeviceNotification(
hWnd,
di,
DEVICE_NOTIFY_WINDOW_HANDLE
);
// If it failed, throw an exception
if (hdevnotify == IntPtr.Zero)
{
var i = unchecked((int)0x80070000);
i += Marshal.GetLastWin32Error();
throw new COMException("Failed to RegisterDeviceNotifications", i);
}
}
示例2: Start
public Boolean Start(Guid classGuid)
{
if (!this.CreateWindow())
{
return false;
}
DEV_BROADCAST_DEVICEINTERFACE dbi = new DEV_BROADCAST_DEVICEINTERFACE
{
dbch_devicetype = DBT_DEVTYP_DEVICEINTERFACE,
dbch_reserved = 0,
dbcc_classguid = classGuid,
dbcc_name = ""
};
dbi.dbch_size = (UInt32)Marshal.SizeOf(dbi);
IntPtr buffer = Marshal.AllocHGlobal((int)dbi.dbch_size);
Marshal.StructureToPtr(dbi, buffer, true);
this.notificationHandle = RegisterDeviceNotification(this.WindowHandle, buffer, 0);
if (IntPtr.Zero == this.notificationHandle)
{
Tracer.Trace("RegisterDeviceNotification failed with error {0}", Marshal.GetLastWin32Error());
return false;
}
return true;
}
示例3: RemoveQueryHook
public RemoveQueryHook( char driveLetter, IntPtr windowHandle )
{
mDriveLetter = driveLetter;
DEV_BROADCAST_DEVICEINTERFACE data = new DEV_BROADCAST_DEVICEINTERFACE();
data.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
data.dbcc_name = '\0';
// This GUID is for all USB serial host PnP drivers, but you can replace it
// with any valid device class guid.
data.dbcc_classguid = new Guid( 0x25dbce51, 0x6c8f, 0x4a72, 0x8a, 0x6d, 0xb5, 0x4c, 0x2b, 0x4f, 0xc8, 0x35 );
int size = Marshal.SizeOf( data );
data.dbcc_size = (uint)size;
IntPtr buffer = Marshal.AllocHGlobal( size );
try
{
Marshal.StructureToPtr( data, buffer, true );
mRegisterDeviceHandle = Native.RegisterDeviceNotification( windowHandle, buffer, 0 );
if ( mRegisterDeviceHandle == IntPtr.Zero )
throw new Exception( "RegisterDeviceNotification() failed on drive '" + mDriveLetter.ToString() + "'" );
}
finally
{
Marshal.FreeHGlobal( buffer );
}
}
示例4: RegisterNotify
public static Boolean RegisterNotify(IntPtr Form, Guid Class, ref IntPtr Handle, Boolean Window = true)
{
IntPtr devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;
try
{
DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
Int32 Size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = Size;
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devBroadcastDeviceInterface.dbcc_reserved = 0;
devBroadcastDeviceInterface.dbcc_classguid = Class;
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(Size);
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
Handle = RegisterDeviceNotification(Form, devBroadcastDeviceInterfaceBuffer, Window ? DEVICE_NOTIFY_WINDOW_HANDLE : DEVICE_NOTIFY_SERVICE_HANDLE);
Marshal.PtrToStructure(devBroadcastDeviceInterfaceBuffer, devBroadcastDeviceInterface);
return Handle != IntPtr.Zero;
}
catch (Exception ex)
{
Console.WriteLine("{0} {1}", ex.HelpLink, ex.Message);
throw;
}
finally
{
if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
}
}
}
示例5: Form1
//--------------- End of Global Varibles ------------------
//-------------------------------------------------------END CUT AND PASTE BLOCK-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Need to check "Allow unsafe code" checkbox in build properties to use unsafe keyword. Unsafe is needed to
//properly interact with the unmanged C++ style APIs used to find and connect with the USB device.
public unsafe Form1()
{
InitializeComponent();
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------BEGIN CUT AND PASTE BLOCK-----------------------------------------------------------------------------------
//Additional constructor code
//Initialize tool tips, to provide pop up help when the mouse cursor is moved over objects on the form.
ANxVoltageToolTip.SetToolTip(this.ANxVoltage_lbl, "If using a board/PIM without a potentiometer, apply an adjustable voltage to the I/O pin.");
ANxVoltageToolTip.SetToolTip(this.progressBar1, "If using a board/PIM without a potentiometer, apply an adjustable voltage to the I/O pin.");
ToggleLEDToolTip.SetToolTip(this.ToggleLEDs_btn, "Sends a packet of data to the USB device.");
PushbuttonStateTooltip.SetToolTip(this.PushbuttonState_lbl, "Try pressing pushbuttons on the USB demo board/PIM.");
//Register for WM_DEVICECHANGE notifications. This code uses these messages to detect plug and play connection/disconnection events for USB devices
DEV_BROADCAST_DEVICEINTERFACE DeviceBroadcastHeader = new DEV_BROADCAST_DEVICEINTERFACE();
DeviceBroadcastHeader.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
DeviceBroadcastHeader.dbcc_size = (uint)Marshal.SizeOf(DeviceBroadcastHeader);
DeviceBroadcastHeader.dbcc_reserved = 0; //Reserved says not to use...
DeviceBroadcastHeader.dbcc_classguid = InterfaceClassGuid;
//Need to get the address of the DeviceBroadcastHeader to call RegisterDeviceNotification(), but
//can't use "&DeviceBroadcastHeader". Instead, using a roundabout means to get the address by
//making a duplicate copy using Marshal.StructureToPtr().
IntPtr pDeviceBroadcastHeader = IntPtr.Zero; //Make a pointer.
pDeviceBroadcastHeader = Marshal.AllocHGlobal(Marshal.SizeOf(DeviceBroadcastHeader)); //allocate memory for a new DEV_BROADCAST_DEVICEINTERFACE structure, and return the address
Marshal.StructureToPtr(DeviceBroadcastHeader, pDeviceBroadcastHeader, false); //Copies the DeviceBroadcastHeader structure into the memory already allocated at DeviceBroadcastHeaderWithPointer
RegisterDeviceNotification(this.Handle, pDeviceBroadcastHeader, DEVICE_NOTIFY_WINDOW_HANDLE);
//Now make an initial attempt to find the USB device, if it was already connected to the PC and enumerated prior to launching the application.
//If it is connected and present, we should open read and write handles to the device so we can communicate with it later.
//If it was not connected, we will have to wait until the user plugs the device in, and the WM_DEVICECHANGE callback function can process
//the message and again search for the device.
if(CheckIfPresentAndGetUSBDevicePath()) //Check and make sure at least one device with matching VID/PID is attached
{
uint ErrorStatusWrite;
uint ErrorStatusRead;
//We now have the proper device path, and we can finally open read and write handles to the device.
WriteHandleToUSBDevice = CreateFile(DevicePath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
ErrorStatusWrite = (uint)Marshal.GetLastWin32Error();
ReadHandleToUSBDevice = CreateFile(DevicePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
ErrorStatusRead = (uint)Marshal.GetLastWin32Error();
if((ErrorStatusWrite == ERROR_SUCCESS) && (ErrorStatusRead == ERROR_SUCCESS))
{
AttachedState = true; //Let the rest of the PC application know the USB device is connected, and it is safe to read/write to it
AttachedButBroken = false;
StatusBox_txtbx.Text = "Device Found, AttachedState = TRUE";
}
else //for some reason the device was physically plugged in, but one or both of the read/write handles didn't open successfully...
{
AttachedState = false; //Let the rest of this application known not to read/write to the device.
AttachedButBroken = true; //Flag so that next time a WM_DEVICECHANGE message occurs, can retry to re-open read/write pipes
if(ErrorStatusWrite == ERROR_SUCCESS)
WriteHandleToUSBDevice.Close();
if(ErrorStatusRead == ERROR_SUCCESS)
ReadHandleToUSBDevice.Close();
}
}
else //Device must not be connected (or not programmed with correct firmware)
{
AttachedState = false;
AttachedButBroken = false;
}
if (AttachedState == true)
{
StatusBox_txtbx.Text = "Device Found, AttachedState = TRUE";
}
else
{
StatusBox_txtbx.Text = "Device not found, verify connect/correct firmware";
}
ReadWriteThread.RunWorkerAsync(); //Recommend performing USB read/write operations in a separate thread. Otherwise,
//the Read/Write operations are effectively blocking functions and can lock up the
//user interface if the I/O operations take a long time to complete.
//-------------------------------------------------------END CUT AND PASTE BLOCK-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
示例6: registerForDeviceNotifications
/// <summary>
/// registerForDeviceNotification - registers the window (identified by the windowHandle) for
/// device notification messages from Windows
/// </summary>
public Boolean registerForDeviceNotifications(IntPtr windowHandle)
{
Debug.WriteLine("usbGenericHidCommunication:registerForDeviceNotifications() -> Method called");
// A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.
DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
IntPtr devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;
Int32 size = 0;
// Get the required GUID
System.Guid systemHidGuid = new Guid();
HidD_GetHidGuid(ref systemHidGuid);
try
{
// Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.
size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = size;
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devBroadcastDeviceInterface.dbcc_reserved = 0;
devBroadcastDeviceInterface.dbcc_classguid = systemHidGuid;
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
// Register for notifications and store the returned handle
deviceInformation.deviceNotificationHandle = RegisterDeviceNotification(windowHandle, devBroadcastDeviceInterfaceBuffer, DEVICE_NOTIFY_WINDOW_HANDLE);
Marshal.PtrToStructure(devBroadcastDeviceInterfaceBuffer, devBroadcastDeviceInterface);
if ((deviceInformation.deviceNotificationHandle.ToInt32() == IntPtr.Zero.ToInt32()))
{
Debug.WriteLine("usbGenericHidCommunication:registerForDeviceNotifications() -> Notification registration failed");
return false;
}
else
{
Debug.WriteLine("usbGenericHidCommunication:registerForDeviceNotifications() -> Notification registration succeded");
return true;
}
}
catch (Exception)
{
Debug.WriteLine("usbGenericHidCommunication:registerForDeviceNotifications() -> EXCEPTION: An unknown exception has occured!");
}
finally
{
// Free the memory allocated previously by AllocHGlobal.
if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
}
return false;
}
示例7: RegisterForDeviceNotifications
private void RegisterForDeviceNotifications()
{
Log.Debug("Main: Registering for Device Notifications");
var devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
int size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = size;
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
IntPtr devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
_deviceNotificationHandle = RegisterDeviceNotification(Handle, devBroadcastDeviceInterfaceBuffer,
DEVICE_NOTIFY_WINDOW_HANDLE |
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
if (_deviceNotificationHandle == IntPtr.Zero)
{
Log.Warn("Main: Could not register for device notifications");
}
}
示例8: RegisterNotifications
private void RegisterNotifications(IntPtr hWnd)
{
var deviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
deviceInterface.dbcc_size = Marshal.SizeOf(deviceInterface);
deviceInterface.dbcc_devicetype = (int)DeviceType.DeviceInterface;
deviceInterface.dbcc_reserved = 0;
deviceInterface.dbcc_classguid = new byte[16];
IntPtr pointer = Marshal.AllocHGlobal(deviceInterface.dbcc_size);
Marshal.StructureToPtr(deviceInterface, pointer, true);
const int flags = DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES;
_notificationHandle = RegisterDeviceNotification(hWnd, pointer, flags);
Marshal.FreeHGlobal(pointer);
}
示例9: RegisterForDeviceNotifications
/// <summary>
/// Requests to receive a notification when a device is attached or removed.
/// </summary>
///
/// <param name="formHandle"> handle to the window that will receive device events. </param>
/// <param name="classGuid"> device interface GUID. </param>
/// <param name="deviceNotificationHandle"> returned device notification handle. </param>
///
/// <returns>
/// True on success.
/// </returns>
internal Boolean RegisterForDeviceNotifications(IntPtr formHandle, Guid classGuid, ref IntPtr deviceNotificationHandle)
{
// A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.
DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
IntPtr devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;
Int32 size = 0;
try
{
// Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.
// Set the size of this structure
size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = size;
// Request to receive notifications about a class of devices.
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
// Reserved; do not use.
devBroadcastDeviceInterface.dbcc_reserved = 0;
// Specify the interface class to receive notifications about.
devBroadcastDeviceInterface.dbcc_classguid = classGuid;
// Allocate memory for the buffer that holds the DEV_BROADCAST_DEVICEINTERFACE structure.
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
// Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer. We are then ready to call the API function. Set fDeleteOld True to prevent memory leaks.
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
// Call deviceNotificationHandle to request to receive notification messages when a device in an interface class is attached or removed.
deviceNotificationHandle = RegisterDeviceNotification(formHandle, devBroadcastDeviceInterfaceBuffer, DEVICE_NOTIFY_WINDOW_HANDLE);
// Marshal data from the unmanaged block devBroadcastDeviceInterfaceBuffer to the managed object devBroadcastDeviceInterface
Marshal.PtrToStructure(devBroadcastDeviceInterfaceBuffer, devBroadcastDeviceInterface);
if ((deviceNotificationHandle.ToInt32() == IntPtr.Zero.ToInt32()))
return false;
else
return true;
}
catch (Exception)
{
throw;
}
finally
{
if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
}
}
}
示例10: RegisterForDeviceNotifications
public static void RegisterForDeviceNotifications(IntPtr controlHandle, Guid classGuid, ref IntPtr deviceNotificationHandle)
{
DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
IntPtr devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;
try
{
int size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = size;
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devBroadcastDeviceInterface.dbcc_reserved = 0;
devBroadcastDeviceInterface.dbcc_classguid = classGuid;
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
// Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer.
// Set fDeleteOld True to prevent memory leaks.
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
deviceNotificationHandle = RegisterDeviceNotification(controlHandle, devBroadcastDeviceInterfaceBuffer, DEVICE_NOTIFY_WINDOW_HANDLE);
if (deviceNotificationHandle == IntPtr.Zero)
throw APIException.Win32("Failed to register device notification");
// Marshal data from the unmanaged block devBroadcastDeviceInterfaceBuffer to
// the managed object devBroadcastDeviceInterface
Marshal.PtrToStructure(devBroadcastDeviceInterfaceBuffer, devBroadcastDeviceInterface);
}
finally
{
// Free the memory allocated previously by AllocHGlobal.
if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
}
}
示例11: RegisterForDeviceNotification
internal bool RegisterForDeviceNotification(IntPtr devHandle, ref IntPtr devNotificationHandle)
{
//Globally Unique Identifier (GUID). Windows uses GUIDs to identify things.
Guid InterfaceClassGuid = new Guid(0xa5dcbf10, 0x6530, 0x11d2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED); //Globally Unique Identifier (GUID) for USB peripheral devices
// A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.
DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
IntPtr devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;
Int32 size = 0;
try
{
// Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.
// Set the size.
size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = size;
// Request to receive notifications about a class of devices.
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devBroadcastDeviceInterface.dbcc_reserved = 0;
// Specify the interface class to receive notifications about.
devBroadcastDeviceInterface.dbcc_classguid = InterfaceClassGuid;
// Allocate memory for the buffer that holds the DEV_BROADCAST_DEVICEINTERFACE structure.
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
// Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer.
// Set fDeleteOld True to prevent memory leaks.
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
devNotificationHandle = RegisterDeviceNotification(devHandle, devBroadcastDeviceInterfaceBuffer, 0);
if (devNotificationHandle.ToInt32() == IntPtr.Zero.ToInt32())
{
return false;
}
else
{
return true;
}
}
catch
{
throw;
}
finally
{
if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
{
// Free the memory allocated previously by AllocHGlobal.
Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
}
}
}
示例12: RegisterForDeviceNotifications
/// <summary>
/// Request to receive a notification when a device is attached or removed.
/// </summary>
///
/// <param name="devicePathName"> a handle to a device.</param>
/// <param name="formHandle"> a handle to the window that will receive device events. </param>
/// <param name="classGuid"> an interface class GUID. </param>
/// <param name="deviceNotificationHandle"> the retrieved handle. (Used when
/// requesting to stop receiving notifications.) </param>
///
/// <returns>
/// True on success, False on failure.
/// </returns>
public Boolean RegisterForDeviceNotifications(String devicePathName, IntPtr formHandle, Guid classGuid, ref IntPtr deviceNotificationHandle)
{
// A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.
DEV_BROADCAST_DEVICEINTERFACE DevBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
IntPtr devBroadcastDeviceInterfaceBuffer = new System.IntPtr();
Int32 size = 0;
try
{
// Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.
// Set the size.
size = Marshal.SizeOf( DevBroadcastDeviceInterface );
DevBroadcastDeviceInterface.dbcc_size = size;
// Request to receive notifications about a class of devices.
DevBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
DevBroadcastDeviceInterface.dbcc_reserved = 0;
// Specify the interface class to receive notifications about.
DevBroadcastDeviceInterface.dbcc_classguid = classGuid;
// Allocate memory for the buffer that holds the DEV_BROADCAST_DEVICEINTERFACE structure.
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal( size );
// Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer.
// Set fDeleteOld True to prevent memory leaks.
Marshal.StructureToPtr( DevBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true );
// ***
// API function:
// RegisterDeviceNotification
// Purpose:
// Request to receive notification messages when a device in an interface class
// is attached or removed.
// Accepts:
// A handle to the window that will receive device events
// A pointer to a DEV_BROADCAST_DEVICEINTERFACE to specify the type of
// device to send notifications for,
// DEVICE_NOTIFY_WINDOW_HANDLE to indicate that Handle is a window handle.
// Returns:
// A device notification handle or NULL on failure.
// ***
deviceNotificationHandle = RegisterDeviceNotification( formHandle, devBroadcastDeviceInterfaceBuffer, DEVICE_NOTIFY_WINDOW_HANDLE );
// Marshal data from the unmanaged block DevBroadcastDeviceInterfaceBuffer to
// the managed object DevBroadcastDeviceInterface
Marshal.PtrToStructure( devBroadcastDeviceInterfaceBuffer, DevBroadcastDeviceInterface );
// Free the memory allocated previously by AllocHGlobal.
Marshal.FreeHGlobal( devBroadcastDeviceInterfaceBuffer );
// Find out if RegisterDeviceNotification was successful.
if ( ( deviceNotificationHandle.ToInt32() == IntPtr.Zero.ToInt32() ) )
{
Debug.WriteLine( "RegisterDeviceNotification error" );
return false;
}
else
{
return true;
}
}
catch ( Exception ex )
{
DisplayException( MODULE_NAME, ex );
throw ;
}
}
示例13: RegisterDeviceInterface
public static IntPtr RegisterDeviceInterface(IntPtr hwnd, DeviceNotifyType notificationType, Guid device)
{
DebugLogger.WriteLine("Registering {0}", device);
DEV_BROADCAST_DEVICEINTERFACE devInt = new DEV_BROADCAST_DEVICEINTERFACE();
devInt.dbcc_devicetype = dbch_devicetype.DBT_DEVTYP_DEVICEINTERFACE;
devInt.dbcc_classguid = device.ToByteArray();
devInt.dbcc_size = Marshal.SizeOf(devInt);
IntPtr buffer = Marshal.AllocHGlobal(devInt.dbcc_size);
try
{
Marshal.StructureToPtr(devInt, buffer, false);
return RegisterDeviceNotification(hwnd, buffer, notificationType); //| DeviceNotifyType.DEVICE_NOTIFY_SERVICE_HANDLE
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
示例14: GetDeviceNameNew
private static string GetDeviceNameNew(DEV_BROADCAST_DEVICEINTERFACE dvi)
{
string[] Parts = dvi.dbcc_name.Split('#');
if (Parts.Length >= 3)
{
string DevType = Parts[0].Substring(Parts[0].IndexOf(@"?\") + 2);
string DeviceInstanceId = Parts[1];
string DeviceUniqueID = Parts[2];
string RegPath = @"SYSTEM\CurrentControlSet\Enum\" + DevType + "\\" + DeviceInstanceId + "\\" + DeviceUniqueID;
RegistryKey key = Registry.LocalMachine.OpenSubKey(RegPath);
if (key != null)
{
object result = key.GetValue("FriendlyName");
if (result != null)
{
Console.WriteLine("\tNEW Name: {0}", result.ToString());
return result.ToString();
}
result = key.GetValue("DeviceDesc");
if (result != null)
{
Console.WriteLine("\tNEW Desc: {0}", result.ToString());
return result.ToString();
}
}
}
return String.Empty;
}
示例15: Form1_Load
private void Form1_Load(object sender, EventArgs e)
{
// Register device notification in Form_Load
HidD_GetHidGuid(ref myGuid);
frmMy = this;
DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
IntPtr devBroadcastDeviceInterfaceBuffer;
Int32 size = 0;
size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = size;
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devBroadcastDeviceInterface.dbcc_reserved = 0;
devBroadcastDeviceInterface.dbcc_classguid = myGuid;
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
deviceNotificationHandle = RegisterDeviceNotification(frmMy.Handle, devBroadcastDeviceInterfaceBuffer,
(DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES));
Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
}