WDM式驱动源码:
#include "stdafx.h"
#include <tchar.h> // Make all functions UNICODE safe.
#include <newdev.h> // for the API UpdateDriverForPlugAndPlayDevices().
#include <setupapi.h> // for SetupDiXxx functions.
#include "install.h"
int DisplayError(TCHAR * ErrorName)
{
DWORD Err = GetLastError();
LPVOID lpMessageBuffer = NULL;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
Err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMessageBuffer,
0,
NULL ))
NULL; //_tprintf(TEXT("%s FAILURE: %s/n"),ErrorName,(TCHAR *)lpMessag
eBuffer);
else
NULL; //_tprintf(TEXT("%s FAILURE: (0xx)/n"),ErrorName,Err);
if (lpMessageBuffer) LocalFree( lpMessageBuffer ); // Free system buffer
SetLastError(Err);
return FALSE;
}
BOOL FindExistingDevice(IN LPTSTR HardwareId)
{
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i,err;
BOOL Found;
//
// Create a Device Information Set with all present devices.
//
DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
0,
0,
DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
return DisplayError(TEXT("GetClassDevs(All Present Devices)"));
}
//_tprintf(TEXT("Search for Device ID: [%s]/n"),HardwareId);
//
// Enumerate through all Devices.
//
Found = FALSE;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR p,buffer = NULL;
DWORD buffersize = 0;
//
// We won‘t know the size of the HardwareID buffer until we call
// this function. So call it with a null to begin with, and then
// use the required buffer size to Alloc the nessicary space.
// Keep calling we have success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(
DeviceInfoSet,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)buffer,
buffersize,