danielxu22
驱动中牛
驱动中牛
  • 注册日期2002-11-22
  • 最后登录2014-03-24
  • 粉丝0
  • 关注1
  • 积分2分
  • 威望18点
  • 贡献值0点
  • 好评度7点
  • 原创分0分
  • 专家分0分
阅读:1868回复:5

CreateFile问题

楼主#
更多 发布于:2002-12-09 12:30
如何调用HID驱动,特别是file name该如何设定,帮助上说不可以是 *.sys,然后还有:
extern GUID TestGuid;
HANDLE OpenByInterface(
  GUID* pClassGuid,
  DWORD instance,
  PDWORD pError
)
{
  CDeviceInterfaceClass DevClass(pClassGuid, pError);
  if (*pError != ERROR_SUCCESS)
    return INVALID_HANDLE_VALUE;
  CDeviceInterface DevInterface(&DevClass, instance, pError);
  if (*pError != ERROR_SUCCESS)
    return INVALID_HANDLE_VALUE;
  cout << \"The device path is \"
    << DevInterface.DevicePath()
    << endl;

  HANDLE hDev;
  hDev = CreateFile(
    DevInterface.DevicePath(),
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL
  );
  if (hDev == INVALID_HANDLE_VALUE)
    *pError = GetLastError();
  return hDev;
}
在HID代码中我没有找到相应的GUID,所以上面的CDeviceInterfaceClass DevClass(pClassGuid, pError);
我不能调用
今天我发现我家的金鱼淹死了,:(
tjm
tjm
驱动小牛
驱动小牛
  • 注册日期2002-05-18
  • 最后登录2004-10-01
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2002-12-11 00:49
调用HID驱动要用到许多SETUPDIXXXX服务,最后才能用CREATEFILE得到HANDLE。
danielxu22
驱动中牛
驱动中牛
  • 注册日期2002-11-22
  • 最后登录2014-03-24
  • 粉丝0
  • 关注1
  • 积分2分
  • 威望18点
  • 贡献值0点
  • 好评度7点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2002-12-11 09:24
你能说的详细点吗??有例子吗??
今天我发现我家的金鱼淹死了,:(
tjm
tjm
驱动小牛
驱动小牛
  • 注册日期2002-05-18
  • 最后登录2004-10-01
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地板#
发布于:2002-12-15 23:05
下面是别人的一个例子,里面有多余的东西


bool CUsbhidiocDlg::FindTheHID()
{
//Use a series of API calls to find a HID with a matching Vendor and Product ID.

HIDD_ATTRIBUTES Attributes;
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData;
SP_DEVICE_INTERFACE_DATA devInfoData;
bool LastDevice = FALSE;
int MemberIndex = 0;
bool MyDeviceDetected = FALSE;
LONG Result;


//These are the vendor and product IDs to look for.
//Uses Lakeview Research\'s Vendor ID.
const unsigned int VendorID = 0x601;
const unsigned int ProductID = 320;

Length = 0;
detailData = NULL;
DeviceHandle=NULL;

/*
API function: HidD_GetHidGuid
Get the GUID for all system HIDs.
Returns: the GUID in HidGuid.
*/

HidD_GetHidGuid(&HidGuid);

/*
API function: SetupDiGetClassDevs
Returns: a handle to a device information set for all installed devices.
Requires: the GUID returned by GetHidGuid.
*/

hDevInfo=SetupDiGetClassDevs \\
(&HidGuid, \\
NULL, \\
NULL, \\
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);

devInfoData.cbSize = sizeof(devInfoData);

//Step through the available devices looking for the one we want.
//Quit on detecting the desired device or checking all available devices without success.
MemberIndex = 0;
LastDevice = FALSE;

do
{
MyDeviceDetected=FALSE;

/*
API function: SetupDiEnumDeviceInterfaces
On return, MyDeviceInterfaceData contains the handle to a
SP_DEVICE_INTERFACE_DATA structure for a detected device.
Requires:
The DeviceInfoSet returned in SetupDiGetClassDevs.
The HidGuid returned in GetHidGuid.
An index to specify a device.
*/

Result=SetupDiEnumDeviceInterfaces \\
(hDevInfo, \\
0, \\
&HidGuid, \\
MemberIndex, \\
&devInfoData);

if (Result != 0)
{
//A device has been detected, so get more information about it.

/*
API function: SetupDiGetDeviceInterfaceDetail
Returns: an SP_DEVICE_INTERFACE_DETAIL_DATA structure
containing information about a device.
To retrieve the information, call this function twice.
The first time returns the size of the structure in Length.
The second time returns a pointer to the data in DeviceInfoSet.
Requires:
A DeviceInfoSet returned by SetupDiGetClassDevs
The SP_DEVICE_INTERFACE_DATA structure returned by SetupDiEnumDeviceInterfaces.

The final parameter is an optional pointer to an SP_DEV_INFO_DATA structure.
This application doesn\'t retrieve or use the structure.
If retrieving the structure, set
MyDeviceInfoData.cbSize = length of MyDeviceInfoData.
and pass the structure\'s address.
*/

//Get the Length value.
//The call will return with a \"buffer too small\" error which can be ignored.
Result = SetupDiGetDeviceInterfaceDetail \\
(hDevInfo, \\
&devInfoData, \\
NULL, \\
0, \\
&Length, \\
NULL);

//Allocate memory for the hDevInfo structure, using the returned Length.
detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);

//Set cbSize in the detailData structure.
detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

//Call the function again, this time passing it the returned buffer size.
Result = SetupDiGetDeviceInterfaceDetail \\
(hDevInfo, \\
&devInfoData, \\
detailData, \\
Length, \\
&Required, \\
NULL);

//Open a handle to the device.

/*
API function: CreateFile
Returns: a handle that enables reading and writing to the device.
Requires:
The DevicePath in the detailData structure
returned by SetupDiGetDeviceInterfaceDetail.
*/

DeviceHandle=CreateFile \\
(detailData->DevicePath, \\
GENERIC_READ|GENERIC_WRITE, \\
FILE_SHARE_READ|FILE_SHARE_WRITE, \\
NULL, \\
OPEN_EXISTING, \\
0, \\
NULL);

DisplayLastError(\"CreateFile: \");

/*
API function: HidD_GetAttributes
Requests information from the device.
Requires: the handle returned by CreateFile.
Returns: a HIDD_ATTRIBUTES structure containing
the Vendor ID, Product ID, and Product Version Number.
Use this information to decide if the detected device is
the one we\'re looking for.
*/

//Set the Size to the number of bytes in the structure.
Attributes.Size = sizeof(Attributes);

Result = HidD_GetAttributes \\
(DeviceHandle, \\
&Attributes);

DisplayLastError(\"HidD_GetAttributes: \");

//Is it the desired device?
MyDeviceDetected = FALSE;


if (Attributes.VendorID == VendorID)
{
if (Attributes.ProductID == ProductID)
{
//Both the Product and Vendor IDs match.
MyDeviceDetected = TRUE;
DisplayData(\"Device detected\");
//Get the device\'s capablities.
GetDeviceCapabilities();
//Use this handle for writing reports.
WriteHandle=DeviceHandle;
//ReadFile is a blocking call,
//so get another handle for another thread for reading reports.
ReadHandle=CreateFile \\
(detailData->DevicePath, \\
GENERIC_READ|GENERIC_WRITE, \\
FILE_SHARE_READ|FILE_SHARE_WRITE, \\
NULL, \\
OPEN_EXISTING, \\
0, \\
NULL);
DisplayLastError(\"CreateFile for Read Handle: \");

/*
Create a thread for reading reports from the device.
ReadFile is a blocking call, so it\'s called in a separate program thread.
This keeps the main program thread from hanging while waiting for a
report from the device.
In CreateThread, StaticIO_Thread is a static member that accepts
the \"this\" pointer and casts it to a pointer to the ReadReport routine,
which does the ReadFile.
*/

// ThreadHandle = CreateThread \\
// (NULL, \\
// 0, \\
// (LPTHREAD_START_ROUTINE)StaticIO_Thread, \\
// this, \\
// 0, \\
// &ThreadID);
//
// if (ThreadHandle == NULL)
// CloseHandle(ThreadHandle);
//
// DisplayLastError(\"CreateThread: \");

} //if (Attributes.ProductID == ProductID)
else
//The Product ID doesn\'t match.
CloseHandle(DeviceHandle);
} //if (Attributes.VendorID == VendorID)
else
//The Vendor ID doesn\'t match.
CloseHandle(DeviceHandle);

//Free the memory used by the detailData structure (no longer needed).
free(detailData);
}  //if (Result != 0)
else
//SetupDiEnumDeviceInterfaces returned 0, so there are no more devices to check.
LastDevice=TRUE;

//If we haven\'t found the device yet, and haven\'t tried every available device,
//try the next one.
MemberIndex = MemberIndex + 1;

} //do
while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE));

if (MyDeviceDetected == FALSE)
DisplayData(\"Device not detected\");

SetupDiDestroyDeviceInfoList(hDevInfo);
DisplayLastError(\"SetupDiDestroyDeviceInfoList\");

return MyDeviceDetected;
}
Always!978
驱动小牛
驱动小牛
  • 注册日期2002-05-09
  • 最后登录2005-04-12
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2003-02-12 15:21
  你说的问题我遇到过,解决方法是
用winobj查到你的设备的连接符****,一般在/??下,然后在createfile的第一个参数的地方添入\"\\\\\\\\.\\\\****\"就可以了
Tomorrow Never Die
husht
驱动牛犊
驱动牛犊
  • 注册日期2003-02-11
  • 最后登录2003-02-25
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2003-02-12 16:02
究竟有没有简单方法用createfile打开usb设备
游客

返回顶部