First Of All, I want to talk about the processing of Windows for each detection of a new device: 1. First Windows divide various devices into different device classes, such as the USB Storage store device, and these types of devices have a GUID Under the registry hkey_local_machine / controlset001 / control / class, in this button, you see the 128-bit length node name as a node. 2. When a new device is detected, Windows OS will go to key_local_machine / controlset001, if this type of device is already registered, then add a subkey under this subkey, the name of this subkey is increasing, if current The maximum subkey name in this class is 0005, then the new device is 0006, 0000,000, etc., the device's serial number. If this device is not registered, the OS will create a key with the ClassGUID in the driver corresponding to the device. The classGUID in the device is created, and the serial number of the device detected is 0, under this button Create a subkey and name it to 0000 to store the relevant information of the device, which is named 0000 because the device is the first of this class device. 3. Some equipment, such as our common COM1, COM2, when we have to access it, you can use CreateFile ("COM1" ...), you can open the serial port. The COM1, COM2 is the symbol name, and some devices have used symbols. Name, for example, some USB devices virtually become a COM port, such as Com3, COM4, COM5, etc. In the program, we only need to access COM3, COM4, COM5, and access the device. How do we access the device according to the device driver and device installation files according to the device driver and device installation files ?inf We can do this: 1. First, we find the classguid of the device class in the symbolic name of this device, which is certain. 2. Then we go to HKEY_LOCAL_MACHINE / CONTROLSET001 / Control / Class to find this device class. After finding it, we will find its sub-keys, find the serial number corresponding to the device, if it is 0000 or 0001, get these two After the data, the ClassGUID and the device serial number are good. Below I write a code, use to access a USB device on my machine and list all devices under this device category on the current machine in Listbox.
extern "C" {# include "setupapi.h"} void __fastcall TForm1 :: Button1Click (TObject * Sender) {// First of all, I will enumurate all the devices under the specified deviceclass HKEY m_hKey, m_hSubKey; long m_lResult = 0 ; // using for return = 0, m_nvalueindex = 0; char ckeyname [255], cvalue [255]; unsigned char PBData [255]; BOOL BOUTTER = true, binter = true; char * croot = "system / / ControlSet001 // Control // Class // {4D36E96D-E325-11CE-BFC1-08002BE10318} "; AnsiString m_sAttached (" "); m_lResult = :: RegOpenKeyEx (HKEY_LOCAL_MACHINE, cRoot, 0, KEY_ALL_ACCESS, & m_hKey); if (m_lResult ! = ERROR_SUCCESS) return FALSE; // Enum Keys while (bOutter) {m_lResult = :: RegEnumKey (m_hKey, m_nKeyIndex, cKeyName, 255); bInter = TRUE;! if (m_lResult = ERROR_SUCCESS) bOutter = FALSE; else {m_lResult = :: RegOpenKeyEx (m_hKey, cKeyName, 0, KEY_ALL_ACCESS, & m_hSubKey); if (m_lResult = ERROR_SUCCESS!) {:: RegCloseKey (m_hKey); return FALSE;} while (bInter) {unsigned long m_nDataSize = 255; unsigned long m_nVal ueNameSize = 255; unsigned long m_nType; m_lResult = :: RegEnumValue (m_hSubKey, m_nValueIndex, cValue, & m_nValueNameSize, 0, & m_nType, pbData, & m_nDataSize); if (! m_lResult = ERROR_SUCCESS) bInter = FALSE; else {if (strcmp (cValue! , "Attachedto")) {m_sattached = (ansistring) (char *) PBDATA;} if (! Strcmp (cvalue, "driverDesc")) {m_lstdevice-> items-> add (ansistring) (char *) PBDATA "" m_sattached);} m_nvalueindex ;}} m_nvalueindex = 0; m_nkeyindex ;}} :: regclosekey; :: regclosekey (m_hsubkey);
file: // Next Step, I will access one of the device.I know its device serialno: 0001 DWORD ReqLength; DWORD Flags = DIGCF_PRESENT | DIGCF_DEVICEINTERFACE; GUID CardGuid = {4D36E96D-E325-11CE-BFC1-08002BE10318}; HANDLE hCard = 0; PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceDetailData; SP_DEVICE_INTERFACE_DATA DeviceInterfaceData; DeviceInterfaceData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA); hCard = SetupDiGetClassDevs (& CardGuid, NULL, NULL, Flags); "! Invalid Parameters" if (hCard == INVALID_HANDLE_VALUE) {:: MessageBox (0,, "Error", MB_OK | MB_ICONERROR); return;} BOOL status = SetupDiEnumDeviceInterfaces (hCard, NULL, & CardGuid, Index, & DeviceInterfaceData, & ReqLength, NULL); // Index i.e., serial devices, Index here is 1. if (status! ) {:: MessageBox (0, "Failed to enumurate the specified device!", "Error", MB_OK MB_ICONERROR); :: CloseHandle (hCard); return;} SetupDiGetInterfaceDeviceDetail (hCard, & DeviceInterfaceData, NULL, 0, & ReqLength, NULL ); DeviceDetailData = (psp_interface_device_detail_data) ne w char [ReqLength]; if (DeviceDetailData) {:: MessageBox ( "! ERROR NOT ENOUGH MEMORY", "Error", MB_OK MB_ICONERROR); :: CloseHandle (hCard); return;} status = SetupDiGetInterfaceDeviceDetail (hCard, & DeviceInterfaceData, DeviceDetailData, ReqLength, & ReqLength, NULL);! if (status) {:: MessageBox (0, "Failed to get interface detailed data", "Error", MB_OK MB_ICONERROR); delete DeviceDetailData; DeviceDetailData = NULL; return;} ShowMessage (DeviceDetAildata-> DevicePath ()); // Getting DevicePath here is like getting a symbolist, then follow,
You can write the program like a serial operation, ie, handle husb = :: createfile (deviceDetails.DevicePath (), ..............); file: // Readfile, Writefile and So .....} All the Note section of the Note: for the prefix, this declaration, so as not to misunderstand, this is the 9CBS document editor Auto. Due to recent learning-drive development, DriverStudio is now developing a USB MODEM driver, there is a little learning experience, hereby exchange.