• PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 23
    下载次数
  • 2013-08-22 11:46
    上传日期
usb打印机的端口定位,本程序通过遍历设备找到USB打印机,根据注册表确定端口与状态
usbport.rar
  • usbport.cpp
    7.6KB
内容介绍
bool CDllCheckDlg::GetUSBPort(CString &USBPortName) { HDEVINFO hDevInfo = 0L; SP_DEVINFO_DATA spDevInfoData = {0}; short wIndex = 0; HWND _hDlg = 0; USBPortName.Format(_T("")); hDevInfo = SetupDiGetClassDevs(0L, 0L, _hDlg, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_PROFILE); if (hDevInfo == (void *) - 1) { return false; }; wIndex = 0; HKEY hKey; spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA); while (1) { if (SetupDiEnumDeviceInfo(hDevInfo, wIndex, &spDevInfoData)) { TCHAR szBuf[MAX_PATH] = {0}; //short wImageIdx = 0; //short wItem = 0; DWORD dwRequireSize; TCHAR szName[64] = {0}; TCHAR szID[LINE_LEN] = {0}; CString szPath; // if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_CLASS, //SPDRP_DEVICEDESC, 0L, (PBYTE)szBuf, 2048, 0)) { wIndex++; continue; } if (!SetupDiGetClassDescription(&spDevInfoData.ClassGuid, szBuf, MAX_PATH, &dwRequireSize)) { wIndex++; continue; } GetDeviceInstanceID(hDevInfo, &spDevInfoData, szID); GetDeviceInterfaceInfo(hDevInfo, spDevInfoData, szPath); if(!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_FRIENDLYNAME, 0L, (PBYTE)szName, 63, 0)) SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_DEVICEDESC, 0L, (PBYTE)szName, 63, 0); CString str; str.Format(_T("%s"), szName); str.MakeUpper(); if(str.Find(_T("USB PRINTING")) == -1) { wIndex++; continue; } CString cs; str.Format(_T("%s"), szID); str.Replace(_T('\\'), _T('#')); cs.Format(_T("SYSTEM\\CurrentControlSet\\Control\\DeviceClasses\\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\\##?#%s#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\\#"), str); str.Format(_T("%s\\Control"), cs.GetBuffer()); long ret0 =::RegOpenKeyEx(HKEY_LOCAL_MACHINE, str.GetBuffer(), 0, KEY_READ, &hKey); if(ret0 != ERROR_SUCCESS) { wIndex++; continue; } DWORD RegNumVaule = 0; DWORD type_1 = REG_DWORD; DWORD cbData_1 = sizeof(DWORD); long ret1 =::RegQueryValueEx(hKey, _T("Linked"), 0, &type_1, (LPBYTE)(&RegNumVaule), &cbData_1); if((ret1 != ERROR_SUCCESS) | (RegNumVaule != 1)) //不是连接状态 { wIndex++; continue; } str.Format(_T("%s\\Device Parameters"), cs.GetBuffer()); ret0 =::RegOpenKeyEx(HKEY_LOCAL_MACHINE, str.GetBuffer(), 0, KEY_READ, &hKey); if(ret0 != ERROR_SUCCESS) { wIndex++; continue; } ret1 =::RegQueryValueEx(hKey, _T("Port Number"), 0, &type_1, (LPBYTE)(&RegNumVaule), &cbData_1); if((ret1 != ERROR_SUCCESS) | (RegNumVaule == 0)) { wIndex++; continue; } ::RegCloseKey(hKey); SetupDiDestroyDeviceInfoList(hDevInfo); USBPortName.Format(_T("USB%.3d"), RegNumVaule); return true; } else break; wIndex++; } ::RegCloseKey(hKey); SetupDiDestroyDeviceInfoList(hDevInfo); return false; } bool CDllCheckDlg::GetDeviceInstanceID(HDEVINFO hDevInfo, SP_DEVINFO_DATA *pspDevInfoData, LPTSTR szInstanceID) { if (!SetupDiGetDeviceInstanceId(hDevInfo, pspDevInfoData, szInstanceID, LINE_LEN, 0)) return false; return true; } bool CDllCheckDlg::GetDeviceInterfaceInfo(HDEVINFO hDevInfo, SP_DEVINFO_DATA spDevInfoData, CString &szPath) { SP_DEVICE_INTERFACE_DATA spDevInterfaceData = {0}; // spDevInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); if (!SetupDiCreateDeviceInterface(hDevInfo, &spDevInfoData, &spDevInfoData.ClassGuid, 0L, 0L, &spDevInterfaceData)) return false; else { SP_DEVICE_INTERFACE_DETAIL_DATA *pspDevInterfaceDetail = 0L; DWORD dwRequire = 0L; // if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &spDevInterfaceData, 0L, 0, &dwRequire, 0L)) { DWORD dwError = GetLastError(); // if (dwError != ERROR_INSUFFICIENT_BUFFER) { return false; } } // pspDevInterfaceDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA *)LocalAlloc(LPTR, sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) * dwRequire * sizeof(TCHAR)); pspDevInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &spDevInterfaceData, pspDevInterfaceDetail, dwRequire, &dwRequire, 0L)) { DWORD dwError = GetLastError(); // if (dwError != ERROR_INSUFFICIENT_BUFFER) if (pspDevInterfaceDetail) LocalFree(pspDevInterfaceDetail); return false; } else { szPath.Format(_T("%s"), pspDevInterfaceDetail->DevicePath); // switch(spDevInterfaceData. } // if (pspDevInterfaceDetail) LocalFree(pspDevInterfaceDetail); return true; } }
评论
    相关推荐