USB应用程序调用驱动程序的例子

这是一个很简单的例子, 大家不要见笑,给入门者一点启发吧!!#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>

#include <devioctl.h>
#include <winioctl.h>

#include <setupapi.h>
#include <basetyps.h>
#include "..Ioctl.h"
#include "..GuidUDK.h"

ddk的例子bulkusb里有


#include <usbdi.h>


#define NOISY(_x_) printf _x_ ;
#define BUF_SIZE (32)
#define DATA_SIZE (10*1024*1024)

char inPipe[32] = "PIPE00"; // pipe name for bulk input pipe on our test board
char outPipe[32] = "PIPE01"; // pipe name for bulk output pipe on our test board
char completeDeviceName[256] = ""; //generated from the GUID registered by the driver itself

BOOL fDumpUsbConfig = FALSE; // flags set in response to console command line switches
BOOL fDumpReadData = FALSE;
BOOL fRead = FALSE;
BOOL fWrite = FALSE;


int gDebugLevel = 1; // higher == more verbose, default is 1, 0 turns off all

ULONG IterationCount = 1; //count of iterations of the test we are to perform
int WriteLen = 0; // #bytes to write
int ReadLen = 0; // #bytes to read

// functions


HANDLE
OpenOneDevice (
IN HDEVINFO HardwareDeviceInfo,
IN PSP_INTERFACE_DEVICE_DATA DeviceInfoData,
IN char *devName
)
/*++
Routine Description:

Given the HardwareDeviceInfo, representing a handle to the plug and
play information, and deviceInfoData, representing a specific usb device,
open that device and fill in all the relevant information in the given
USB_DEVICE_DESCRIPTOR structure.

Arguments:

HardwareDeviceInfo: handle to info obtained from Pnp mgr via SetupDiGetClassDevs()
DeviceInfoData: ptr to info obtained via SetupDiEnumInterfaceDevice()

Return Value:

return HANDLE if the open and initialization was successfull,
else INVLAID_HANDLE_VALUE.

--*/
{
PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
ULONG predictedLength = 0;
ULONG requiredLength = 0;
HANDLE hOut = INVALID_HANDLE_VALUE;

//
// allocate a function class device data structure to receive the
// goods about this particular device.
//
SetupDiGetDeviceInterfaceDetail (
HardwareDeviceInfo,
DeviceInfoData,
NULL, // probing so no output buffer yet
0, // probing so output buffer length of zero
&requiredLength,
NULL); // not interested in the specific dev-node


predictedLength = requiredLength;
// sizeof (SP_FNCLASS_DEVICE_DATA) + 512;

functionClassDeviceData = malloc (predictedLength);
functionClassDeviceData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA);

//
// Retrieve the information from Plug and Play.
//
if (! SetupDiGetDeviceInterfaceDetail (
HardwareDeviceInfo,
DeviceInfoData,
functionClassDeviceData,
predictedLength,
&requiredLength,
NULL)) {
free( functionClassDeviceData );
return INVALID_HANDLE_VALUE;
}

strcpy( devName,functionClassDeviceData->DevicePath) ;
printf( "Attempting to open %sn", devName );

hOut = CreateFile (
functionClassDeviceData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
NULL); // No template file

if (INVALID_HANDLE_VALUE == hOut) {
printf( "FAILED to open %sn", devName );
}
free( functionClassDeviceData );
return hOut;
}


HANDLE
OpenUsbDevice( LPGUID pGuid, char *outNameBuf)
/*++
Routine Description:

Do the required PnP things in order to find
the next available proper device in the system at this time.

Arguments:

pGuid: ptr to GUID registered by the driver itself
outNameBuf: the generated name for this device

Return Value:

return HANDLE if the open and initialization was successful,
else INVLAID_HANDLE_VALUE.
--*/
{
ULONG NumberDevices;
HANDLE hOut = INVALID_HANDLE_VALUE;
HDEVINFO hardwareDeviceInfo;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
ULONG i;
BOOLEAN done;
PUSB_DEVICE_DESCRIPTOR usbDeviceInst;
PUSB_DEVICE_DESCRIPTOR *UsbDevices = &usbDeviceInst;

*UsbDevices = NULL;
NumberDevices = 0;

//
// Open a handle to the plug and play dev node.
// SetupDiGetClassDevs() returns a device information set that contains info on all
// installed devices of a specified class.
//
printf("entering the open_usb_device()n");

hardwareDeviceInfo = SetupDiGetClassDevs (
pGuid,
NULL, // Define no enumerator (global)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值