这是一个很简单的例子, 大家不要见笑,给入门者一点启发吧!!#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)