阅读:2489回复:10
CreateFile??
我的打印机接USB口,这样在应用程序中我不知怎样得到设备句柄,下面的不再适用:
HANDLE hDeviceIo = CreateFile(\"\\\\\\\\.\\\\LPT1\", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 各位,我该咋办? |
|
|
沙发#
发布于:2002-08-15 08:36
查看设备管理器,看看有什么新的设备没。再查看注册表的Enum下相应的键值,可以得到Symbol Link。
|
|
|
板凳#
发布于:2002-08-15 08:37
试试\\\\\\\\.\\\\USB-0
|
|
|
地板#
发布于:2002-08-15 08:50
有GUID没有?
|
|
|
地下室#
发布于:2002-08-15 08:53
查看设备管理器,看看有什么新的设备没。再查看注册表的Enum下相应的键值,可以得到Symbol Link。 tigerzd先生:Enum下相应的键值是 0:REG_SZ:USB\\Vid_04b8&Pid_0001\\5&f1546e9&0&1 Count:REG_DWORD:0x1 NextInstance:REG_DWORD:0x1 什么是Symbol Link呢 |
|
|
5楼#
发布于:2002-08-15 08:56
有GUID没有? 我不知道,总该有吧 |
|
|
6楼#
发布于:2002-08-15 09:04
我在注册表里找到了,是USB001,但能保证在其它机器上也是USB001吗?
|
|
|
7楼#
发布于:2002-08-15 09:42
HANDLE hDeviceIo = CreateFile(\"\\\\\\\\.\\\\USB001\", GENERIC_READ|GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 为什么不行呢? |
|
|
8楼#
发布于:2002-08-15 10:55
USB\\Vid_04b8&Pid_0001\\5&f1546e9&0&1是硬件ID。它下面没有\\DosDevices\\0000000000000010#{a5dcbf10-6530-11d2-901f-00c04fb951ed}(2000下是\\??\\***)之类的东东吗?
没有的话看它的HardwareID。如果USB001是HardwareID却打不开的话,那就是它的驱动程序可能根本没有应用层接口(很多miniport driver,比如Video、网卡等)! |
|
|
9楼#
发布于:2002-08-15 11:11
SymbolicName:REG_SZ:\\??\\USB#Vid_04b8&Pid_0001#5&f1546e9&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
那么怎样替代下面的LPT1呢 HANDLE hDeviceIo = CreateFile(\"\\\\\\\\.\\\\LPT1\", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 多谢了 |
|
|
10楼#
发布于:2002-08-15 12:01
// {259467E2-3CAD-11d6-97AB-00055D064551}//相应换成
// {a5dcbf10-6530-11d2-901f-00c04fb951ed},你自己替换一下: DEFINE_GUID(USB_GUID, 0x259467e2, 0x3cad, 0x11d6, 0x97, 0xab, 0x0, 0x5, 0x5d, 0x6, 0x45, 0x51); //看看Chris Cant的GUID打开设备的例子吧。 然后: HANDLE hDevice = GetDeviceViaInterface((LPGUID)USB_GUID,0); HANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance) { // Get handle to relevant device information set HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); if(info==INVALID_HANDLE_VALUE) { printf(\"No HDEVINFO available for this GUID\\n\"); return NULL; } // Get interface data for the requested instance SP_INTERFACE_DEVICE_DATA ifdata; ifdata.cbSize = sizeof(ifdata); while(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, instance, &ifdata)) { if(GetLastError()==ERROR_NO_MORE_ITEMS) { printf(\"No SP_INTERFACE_DEVICE_DATA available for this GUID instance\\n\"); SetupDiDestroyDeviceInfoList(info); return NULL; } else { instance++; } } // Get size of symbolic link name DWORD ReqLen; SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL); PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]); if( ifDetail==NULL) { SetupDiDestroyDeviceInfoList(info); return NULL; } // Get symbolic link name ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL)) { SetupDiDestroyDeviceInfoList(info); delete ifDetail; return NULL; } printf(\"Symbolic link is %s\\n\",ifDetail->DevicePath); // Open file HANDLE rv = CreateFile( ifDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); if( rv==INVALID_HANDLE_VALUE) { rv = NULL; } delete ifDetail; SetupDiDestroyDeviceInfoList(info); return rv; } |
|
|