📄 hclient.c
字号:
return (0);
}
//
// Get the function pointers,
//
pfnHidP_GetExtendedAttributes = (PGETEXTATTRIB) GetProcAddress(HIDDLLModuleHandle,
"HidP_GetExtendedAttributes");
pfnHidP_InitializeReportForID = (PINITREPORT) GetProcAddress(HIDDLLModuleHandle,
"HidP_InitializeReportForID");
//
// Try to create the main dialog box. Cannot do much else if it fails
// so we'll throw up a message box and then exit the app
//
if (-1 == DialogBox(hInstance, "MAIN_DIALOG", NULL, bMainDlgProc))
{
MessageBox(NULL,
"Unable to create root dialog!",
"DialogBox failure",
MB_ICONSTOP);
}
FreeLibrary (HIDDLLModuleHandle);
return (0);
}
/*************************************************
* Main Dialog proc *
*************************************************/
//
// This the dialog box procedure for the main dialog display.
//
INT_PTR CALLBACK
bMainDlgProc(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
static HWND hComboCtrl;
static rWriteDataStruct rWriteData[MAX_OUTPUT_ELEMENTS];
static HDEVNOTIFY diNotifyHandle;
INT iIndex;
INT iCount;
CHAR szTempBuff[SMALL_BUFF];
PHID_DEVICE pDevice;
PHIDP_BUTTON_CAPS pButtonCaps;
PHIDP_VALUE_CAPS pValueCaps;
INT iErrorLine;
INT iItemType;
PHID_DEVICE tempDeviceList;
ULONG numberDevices;
PDEVICE_LIST_NODE listNode;
DEV_BROADCAST_DEVICEINTERFACE broadcastInterface;
HID_DEVICE writeDevice;
BOOL status;
HRESULT stringReturn;
switch (message)
{
case WM_INITDIALOG:
//
// Initialize the device list.
// -- PhysicalDeviceList is for devices that are actually attached
// to the HID bus
//
InitializeList(&PhysicalDeviceList);
//
// Begin by finding all the Physical HID devices currently attached to
// the system. If that fails, exit the dialog box.
//
if (!FindKnownHidDevices(&tempDeviceList, &numberDevices))
{
EndDialog(hDlg, 0);
return FALSE;
}
//
// For each device in the newly acquired list, create a device list
// node and add it the the list of physical device on the system
//
pDevice = tempDeviceList;
for (iIndex = 0; (ULONG) iIndex < numberDevices; iIndex++, pDevice++)
{
listNode = malloc(sizeof(DEVICE_LIST_NODE));
if (NULL == listNode) {
//
// When freeing up the device list, we need to kill those
// already in the Physical Device List and close
// that have not been added yet in the enumerated list
//
DestroyListWithCallback(&PhysicalDeviceList, DestroyDeviceListCallback);
CloseHidDevices(pDevice, numberDevices - iIndex);
free(tempDeviceList);
EndDialog(hDlg, 0);
return FALSE;
}
listNode -> HidDeviceInfo = *pDevice;
listNode -> DeviceOpened = TRUE;
//
// Register this device node with the PnP system so the dialog
// window can recieve notification if this device is unplugged.
//
if (!RegisterHidDevice(hDlg, listNode))
{
DestroyListWithCallback(&PhysicalDeviceList, DestroyDeviceListCallback);
CloseHidDevices(pDevice, numberDevices - iIndex);
free(tempDeviceList);
free(listNode);
EndDialog(hDlg, 0);
return FALSE;
}
InsertTail(&PhysicalDeviceList, listNode);
}
//
// Free the temporary device list...It is no longer needed
//
free(tempDeviceList);
//
// Register for notification from the HidDevice class. Doing so
// allows the dialog box to receive device change notifications
// whenever a new HID device is added to the system
//
broadcastInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
broadcastInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
HidD_GetHidGuid(&broadcastInterface.dbcc_classguid);
diNotifyHandle = RegisterDeviceNotification(hDlg,
&broadcastInterface,
DEVICE_NOTIFY_WINDOW_HANDLE
);
if (NULL == diNotifyHandle)
{
DestroyListWithCallback(&PhysicalDeviceList, DestroyDeviceListCallback);
EndDialog(hDlg, 0);
return FALSE;
}
//
// Update the device list box...
//
//
vLoadDevices(GetDlgItem(hDlg, IDC_DEVICES));
//
// Load the types box
//
vLoadItemTypes(GetDlgItem(hDlg, IDC_TYPE));
//
// Post a message that the device changed so the appropriate
// data for the first device in the system can be displayed
//
PostMessage(hDlg,
WM_COMMAND,
IDC_DEVICES + (CBN_SELCHANGE<<16),
(LPARAM) GetDlgItem(hDlg, IDC_DEVICES));
break; // end WM_INITDIALOG case
case WM_COMMAND:
switch(LOWORD(wParam))
{
//
// For a read, simply get the current device instance
// from the DEVICES combo box and call the read procedure
// with the HID_DEVICE block
//
case IDC_READ:
GET_CURRENT_DEVICE(hDlg, pDevice);
if (NULL != pDevice)
{
iIndex = (INT) DialogBoxParam(hGInstance,
"READDATA",
hDlg,
bReadDlgProc,
(LPARAM) pDevice);
}
break;
//
// For a write, the following steps are performed:
// 1) Get the current device data from the combo box
// 2) Prepare the data fields for display based on the data
// output data stored in the device data
// 3) Retrieve the data the from the user that is to be sent
// to the device
// 4) If all goes well and the data parses correctly, send the
// the new data values to the device
//
case IDC_WRITE:
GET_CURRENT_DEVICE(hDlg, pDevice);
if (NULL != pDevice)
{
//
// In order to write to the device, need to get a
// writable handle to the device. In this case, the
// write will be a synchronous write. Begin by
// trying to open a second instance of this device with
// write access
//
status = OpenHidDevice(pDevice -> DevicePath,
FALSE,
TRUE,
FALSE,
FALSE,
&writeDevice);
if (!status)
{
MessageBox(hDlg,
"Couldn't open device for write access",
HCLIENT_ERROR,
MB_ICONEXCLAMATION);
}
else
{
iCount = iPrepareDataFields(writeDevice.OutputData,
writeDevice.OutputDataLength,
rWriteData,
MAX_OUTPUT_ELEMENTS);
if (bGetData(rWriteData, iCount, hDlg, "WRITEDATA"))
{
if (bParseData(writeDevice.OutputData, rWriteData, iCount, &iErrorLine))
{
Write(&writeDevice);
}
else
{
stringReturn = StringCbPrintf(szTempBuff,
SMALL_BUFF,
"Unable to parse line %x of output data",
iErrorLine);
MessageBox(hDlg,
szTempBuff,
HCLIENT_ERROR,
MB_ICONEXCLAMATION);
}
}
CloseHidDevice(&writeDevice);
}
}
break; //end case IDC_WRITE//
//
// For processing features, get the current device data and call
// the Features dialog box, This dialog box will deal with
// sending and retrieving the features.
//
case IDC_FEATURES:
GET_CURRENT_DEVICE(hDlg, pDevice);
if (NULL != pDevice)
{
iIndex = (INT) DialogBoxParam(hGInstance,
"FEATURES",
hDlg,
bFeatureDlgProc,
(LPARAM) pDevice);
}
break;
//
// Likewise with extended calls dialog box. This procedure
// passes the address to the device data structure and lets
// the dialog box procedure manipulate the data however it
// wants to.
//
case IDC_EXTCALLS:
GET_CURRENT_DEVICE(hDlg, pDevice);
if (NULL != pDevice)
{
iIndex = (INT) DialogBoxParam(hGInstance,
"EXTCALLS",
hDlg,
bExtCallDlgProc,
(LPARAM) pDevice);
}
break;
//
// If there was a device change, issue an IDC_TYPE
// change to insure that the currently displayed types are
// updated to reflect the values of the device that has
// been selected
//
case IDC_DEVICES:
switch (HIWORD(wParam))
{
case CBN_SELCHANGE:
GET_CURRENT_DEVICE(hDlg, pDevice);
EnableWindow(GetDlgItem(hDlg, IDC_READ),
(pDevice != NULL) &&
(pDevice -> Caps.InputReportByteLength));
EnableWindow(GetDlgItem(hDlg, IDC_WRITE),
(pDevice != NULL) &&
(pDevice -> Caps.OutputReportByteLength));
EnableWindow(GetDlgItem(hDlg, IDC_FEATURES),
(pDevice != NULL) &&
(pDevice -> Caps.FeatureReportByteLength));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -