关于MFC错误

markfilan 2010-09-02 07:02:54
我写的程序是检测USB的,但是在写完后怎么都检测不到要检测的设备。不知道是怎么回事,但是编译时是可以编译的,请大家帮我看看:
initUSB:

#include"StdAfx.h"
#include"Hid.h"
#include<windows.h>
#include<tchar.h>

#define VID 0x040B
#define PID 0X6515
extern "C" {
// This file is in the Windows DDK available from Microsoft.
#include "hidsdi.h"
#include "setupapi.h"
}
CHid::CHid()
{
m_bMyDeviceDetected = FALSE;
}
CHid::~CHid()
{
}
BOOL CHid::OpenHidDevice(HANDLE *HidDevHandle, USHORT vid, USHORT pid)
{
HANDLE *devHandle;
static GUID HidGuid;
HDEVINFO HidDevInfo;
SP_DEVICE_INTERFACE_DATA devInfoData;
BOOLEAN Result;
DWORD Index;
DWORD DataSize;
BOOLEAN GotRequiredSize;
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData;
DWORD RequiredSize;
HIDD_ATTRIBUTES HIDAttrib;
BOOL DIDResult;
GotRequiredSize = FALSE;
HidD_GetHidGuid(&HidGuid);
HidDevInfo = SetupDiGetClassDevs(&HidGuid,NULL,0,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
Index = 0;
devInfoData.cbSize =sizeof(devInfoData);
do
{
Result = SetupDiEnumDeviceInterfaces(HidDevInfo,0,&HidGuid,Index,&devInfoData);
if(Result ==FALSE)
if(detailData!=NULL)
SetupDiDestroyDeviceInfoList(HidDevInfo);
return FALSE;
if(GotRequiredSize == FALSE)
DIDResult =SetupDiGetDeviceInterfaceDetail(HidDevInfo,&devInfoData,NULL,0,&DataSize,NULL);
GotRequiredSize =TRUE;
detailData =(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(DataSize);
detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
DIDResult = SetupDiGetDeviceInterfaceDetail(HidDevInfo,&devInfoData,detailData,DataSize,&RequiredSize,NULL);
*devHandle = CreateFile(detailData->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
if(*devHandle!=INVALID_HANDLE_VALUE)
{
HIDAttrib.Size = sizeof(HIDAttrib);
HidD_GetAttributes(*devHandle,&HIDAttrib);
if((HIDAttrib.VendorID=VID)&&(HIDAttrib.ProductID==PID))
{
if(detailData!=NULL)
free(&detailData);
SetupDiDestroyDeviceInfoList(&HidDevInfo);
return TRUE;
}
CloseHandle(*devHandle);
}
Index++;
}while(Result==TRUE);
if(detailData!=NULL)
free(detailData);
SetupDiDestroyDeviceInfoList(HidDevInfo);
return FALSE;
}
void CHid::ReadHid(unsigned char ucDataBuffer[],unsigned char ucDataLength)
{
HANDLE hDevice;
unsigned long NumBytesReturned;
unsigned char inBuffer[128];
BOOL bResult;
HIDP_CAPS Capabilities;
PHIDP_PREPARSED_DATA HidParsedData;
OVERLAPPED HidOverlapped;
HANDLE ReportEvent;
if(!m_bMyDeviceDetected)
return;
if(OpenHidDevice(&hDevice,VID,PID)==TRUE)
{
HidD_GetPreparsedData(&hDevice,&HidParsedData);
HidP_GetCaps(HidParsedData,&Capabilities);
HidD_FreePreparsedData(HidParsedData);
ReportEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
HidOverlapped.hEvent = ReportEvent;
HidOverlapped.Offset = 0;
HidOverlapped.OffsetHigh = 0;
bResult = ReadFile(&hDevice,&inBuffer[0],Capabilities.InputReportByteLength,&NumBytesReturned,(LPOVERLAPPED)&HidOverlapped);
bResult = WaitForSingleObject(&hDevice,600);
if(bResult==WAIT_TIMEOUT||bResult==WAIT_ABANDONED)
CancelIo(&hDevice);
if(bResult==WAIT_OBJECT_0)
for(int i=0;i<ucDataLength;i++)
{
ucDataBuffer[i] = inBuffer[i+1];
//TRACE("read data successful!");
}
TRACE("\n");
}
CloseHandle(&hDevice);
}
void CHid::WriteHid(unsigned char ucTxBuffer[], unsigned char ucTxLength)
{
HANDLE hDevice;
unsigned long numBytesReturned;
unsigned char outbuffer[128]; /* output buffer */
BOOL bResult;
HIDP_CAPS Capabilities;
PHIDP_PREPARSED_DATA HidParsedData;
OVERLAPPED HidOverlapped;
HANDLE ReportEvent;
if(!m_bMyDeviceDetected)
return;

/* Open the HID device handle */
if(OpenHidDevice( &hDevice, VID, PID) == TRUE)
{
/* get a handle to a buffer that describes the device's capabilities. This
line plus the following two lines of code extract the report length the
device is claiming to support */
HidD_GetPreparsedData(hDevice, &HidParsedData);

/* extract the capabilities info */
HidP_GetCaps( HidParsedData ,&Capabilities);

/* Free the memory allocated when getting the preparsed data */
HidD_FreePreparsedData(HidParsedData);

/* Create a new event for report capture */
ReportEvent = CreateEvent(NULL, TRUE, TRUE, "");

/* fill the HidOverlapped structure so that Windows knows which
event to cause when the device sends an IN report */
HidOverlapped.hEvent = ReportEvent;
HidOverlapped.Offset = 0;
HidOverlapped.OffsetHigh = 0;

/* Use WriteFile to send an output report to the HID device. Firt we should
fill the outbuffer*/
outbuffer[0] = 0; /* this is used as the report ID */
for(int i=0;i<ucTxLength;i++) //copy data form ucDataBuffer
outbuffer[i+1] = ucTxBuffer[i];

bResult = WriteFile( hDevice,
&outbuffer[0],
Capabilities.OutputReportByteLength,
// ucTxLength+1,
&numBytesReturned,
(LPOVERLAPPED) &HidOverlapped);


bResult = WaitForSingleObject(ReportEvent, 200);

/* close the HID device handle */
CloseHandle(hDevice);

ResetEvent(ReportEvent);
}

// TMThreadActive = FALSE;

}

BOOL CHid::FindHid()
{
HANDLE HidDevHandle; //The handle of the hid device return by OpenHidDevice

USHORT usPid,usVid;
usPid = PID;
usVid = VID;

BOOL bFindHid = OpenHidDevice(&HidDevHandle,usVid,usPid);
if(bFindHid)
{
TRACE("My Device detected.");

m_bMyDeviceDetected = TRUE;

CloseHandle(HidDevHandle);

return TRUE;
}
else
{
TRACE("My Device not detected.");

// if(m_bMyDeviceDetected)
// CloseHandle(HidDevHandle); //未找到指定设备,故设备指针没有用,可以释放

m_bMyDeviceDetected = FALSE;

return FALSE;
}
}




然后再MFC中使用的:

void CHidSendDataDlg::OnDeviceChanged()
{
Chid m_MyHidDevice;
if(m_MyHidDevice.FindHid())
{
AfxMessageBox("Find device!");
}
}
但是不能检测出来。这是怎么回事呢?请大家帮忙看看,感激不尽。










...全文
119 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
markfilan 2010-09-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 djjlove_2008 的回复:]

还是端口通信来写吧,这样太复杂了,搞不出来,超出了我能力范围之外。
[/Quote]
?没看懂。
markfilan 2010-09-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 djjlove_2008 的回复:]

还是端口通信来写吧,这样太复杂了,搞不出来,超出了我能力范围之外。
[/Quote]
?没看懂。
markfilan 2010-09-02
  • 打赏
  • 举报
回复
我看不出有什么错误。。。。。。。。晕。。。。。。逻辑都对啊。
djjlove_2008 2010-09-02
  • 打赏
  • 举报
回复
还是端口通信来写吧,这样太复杂了,搞不出来,超出了我能力范围之外。
失落的凡凡 2010-09-02
  • 打赏
  • 举报
回复
能通过编译 和 逻辑正确远远不是一回事。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧