代码如下(victor老大的代码几乎没有改动)
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
#include
#include
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// 下面必须为驱动程序的 GUID 值
DEFINE_GUID(USB_DRIVER_GUID, 0x36FC9E60,0xC465,0x11CF,0x80,0x56,0x44,0x45,0x53,0x54,0x00,0x00);
HANDLE OpenOneDevice(HDEVINFO hDvcInfo, PSP_INTERFACE_DEVICE_DATA DvcInfoData, char *sDevNameBuf)
{
HANDLE hOut = INVALID_HANDLE_VALUE;
ULONG iReqLen = 0;
SetupDiGetInterfaceDeviceDetail(hDvcInfo, DvcInfoData, NULL, 0,&iReqLen, NULL);
ULONG iDevDataLen = iReqLen; //sizeof(SP_FNCLASS_DEVICE_DATA) + 512;
PSP_INTERFACE_DEVICE_DETAIL_DATA pDevData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(iDevDataLen);
pDevData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if(SetupDiGetInterfaceDeviceDetail(hDvcInfo, DvcInfoData, pDevData, iDevDataLen, &iReqLen, NULL))
{
strcpy(sDevNameBuf, pDevData->DevicePath);
hOut = CreateFile(pDevData->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
}
free(pDevData);
return hOut;
}
//---------------------------------------------------------------------------
HANDLE OpenUsbDevice(const GUID *pGuid, char *sDevNameBuf)
{
HANDLE hOut = INVALID_HANDLE_VALUE;
HDEVINFO hDevInfo = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
SP_INTERFACE_DEVICE_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);
ULONG nGuessCount = MAXLONG;
for(ULONG iDevIndex=0; iDevIndex {
if(SetupDiEnumDeviceInterfaces(hDevInfo, 0, pGuid, iDevIndex, &deviceInfoData))
{
if((hOut=OpenOneDevice(hDevInfo, &deviceInfoData, sDevNameBuf)) != INVALID_HANDLE_VALUE)
break;
}
else if(GetLastError() == ERROR_NO_MORE_ITEMS) //No more items
{
break;
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return hOut;
}
//---------------------------------------------------------------------------
bool GetUsbDeviceFileName(const GUID *pGuid, char *sDevNameBuf)
{
HANDLE hDev = OpenUsbDevice(pGuid, sDevNameBuf);
if(hDev != INVALID_HANDLE_VALUE)
{
CloseHandle(hDev);
return true;
}
return false;
}
//---------------------------------------------------------------------------
HANDLE OpenMyDevice()
{
char DeviceName[MAXPATH] = "";
return OpenUsbDevice (&USB_DRIVER_GUID, DeviceName);
}
//---------------------------------------------------------------------------
HANDLE OpenMyDevPipe(const char *PipeName)
{
char DeviceName[MAXPATH] = "";
if(GetUsbDeviceFileName(&USB_DRIVER_GUID, DeviceName))
{
strcat(DeviceName,"\\");
strcat(DeviceName,PipeName);
return CreateFile(DeviceName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
}
return INVALID_HANDLE_VALUE;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
HANDLE hDevice=OpenMyDevice();
if(hDevice !=INVALID_HANDLE_VALUE)
{ ShowMessage("Usb Read success");
}
else {
ShowMessage("Usb Read fail");
return; }
CloseHandle(hDevice);
}
//---------------------------------------------------------------------------
最后我的运行结果都是"USB READ FAIL",也就是hDevice无法返回正确的值,高手们帮帮俺.快崩溃了.
--------------------next---------------------
阅读(1218) | 评论(0) | 转发(0) |