VC安装驱动源码

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,

&buffersize))

{

if (GetLastError() == ERROR_INVALID_DATA)

{

//

// May be a Legacy Device with no HardwareID. Continue.

//

break;

}

else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)

{

//

// We need to change the buffer size.

//

if (buffer)

LocalFree(buffer);

buffer = (char *)LocalAlloc(LPTR,buffersize);

}

else

{

//

// Unknown Failure.

//

DisplayError(TEXT("GetDeviceRegistryProperty"));

goto cleanup_DeviceInfo;

}

}

if (GetLastError() == ERROR_INVALID_DATA)

continue;

//

// Compare each entry in the buffer multi-sz list with our HardwareID.

//

for (p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))

{

//_tprintf(TEXT("Compare device ID: [%s]/n"),p);

if (!_tcscmp(HardwareId,p))

{

//_tprintf(TEXT("Found! [%s]/n"),p);

Found = TRUE;

break;

}

}

if (buffer) LocalFree(buffer);

if (Found) break;

}

if (GetLastError() != NO_ERROR)

{

DisplayError(TEXT("EnumDeviceInfo"));

}

//

//  Cleanup.

//

cleanup_DeviceInfo:

err = GetLastError();

SetupDiDestroyDeviceInfoList(DeviceInfoSet);

SetLastError(err);

return err == NO_ERROR; //???

}

BOOL

InstallRootEnumeratedDriver(IN  LPTSTR HardwareId,

IN  LPTSTR INFFile,

OUT PBOOL  RebootRequired  OPTIONAL

)

{

HDEVINFO DeviceInfoSet = 0;

SP_DEVINFO_DATA DeviceInfoData;

GUID ClassGUID;

TCHAR ClassName[MAX_CLASS_NAME_LEN];

DWORD err;

//

// Use the INF File to extract the Class GUID.

//

if (!SetupDiGetINFClass(INFFile,&ClassGUID,ClassName,sizeof(ClassName),0))

{

return DisplayError(TEXT("GetINFClass"));

}

//

// Create the container for the to-be-created Device Information Element.

//

DeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID,0);

if(DeviceInfoSet == INVALID_HANDLE_VALUE)

{

return DisplayError(TEXT("CreateDeviceInfoList"));

}

//

// Now create the element.

// Use the Class GUID and Name from the INF file.

//

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

if (!SetupDiCreateDeviceInfo(DeviceInfoSet,

ClassName,

&ClassGUID,

NULL,

0,

DICD_GENERATE_ID,

&DeviceInfoData))

{

DisplayError(TEXT("CreateDeviceInfo"));

goto cleanup_DeviceInfo;

}

//

// Add the HardwareID to the Device‘s HardwareID property.

//

if(!SetupDiSetDeviceRegistryProperty(DeviceInfoSet,

&DeviceInfoData,

SPDRP_HARDWAREID,

(LPBYTE)HardwareId,

(lstrlen(HardwareId)+1+1)*sizeof(TCHAR)))

{

DisplayError(TEXT("SetDeviceRegistryProperty"));

goto cleanup_DeviceInfo;

}

//

// Transform the registry element into an actual devnode

// in the PnP HW tree.

//

if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE,

DeviceInfoSet,

&DeviceInfoData))

{

DisplayError(TEXT("CallClassInstaller(REGISTERDEVICE)"));

goto cleanup_DeviceInfo;

}

//

// The element is now registered. We must explicitly remove the

// device using DIF_REMOVE, if we encounter any failure from now on.

//

//

// Install the Driver.

//

if (!UpdateDriverForPlugAndPlayDevices(0,

HardwareId,

INFFile,

INSTALLFLAG_FORCE,

RebootRequired))

{

DWORD err = GetLastError();

DisplayError(TEXT("UpdateDriverForPlugAndPlayDevices"));

if (!SetupDiCallClassInstaller(

DIF_REMOVE,

DeviceInfoSet,

&DeviceInfoData))

{

DisplayError(TEXT("CallClassInstaller(REMOVE)"));

}

SetLastError(err);

}

//

//  Cleanup.

//

cleanup_DeviceInfo:

err = GetLastError();

SetupDiDestroyDeviceInfoList(DeviceInfoSet);

SetLastError(err);

return err == NO_ERROR;

}

int InstallDriver(_TCHAR *InfName, _TCHAR *HardwareID)

{

WIN32_FIND_DATA FindFileData;

BOOL RebootRequired = 0; // Must be cleared.

_TCHAR *FName, *HWID;

FName = InfName;

HWID = HardwareID;

if (FindFirstFile(FName,&FindFileData)==INVALID_HANDLE_VALUE)

{

//_tprintf(TEXT("  File not found./n"));

//_tprintf(TEXT("usage: install <INF_File> <Hardware_ID>/n"));

return 2; // Install Failure

}

//

// Look to see if this device allready exists.

//

if (FindExistingDevice(HWID))

{

//

// No Need to Create a Device Node, just call our API.

//

if (!UpdateDriverForPlugAndPlayDevices(0, // No Window Handle

HWID, // Hardware ID

FName, // FileName

INSTALLFLAG_FORCE,

&RebootRequired))

{

DisplayError(TEXT("UpdateDriverForPlugAndPlayDevices"));

return 2; // Install Failure

}

}

else

{

if (GetLastError()!= ERROR_NO_MORE_ITEMS)

{

//

// An unknown failure from FindExistingDevice()

//

//_tprintf(TEXT("(IERROR_NO_MORE_ITEMS)/n"));

//_tprintf(TEXT("(Install Failure! Code = 2)/n"));

return 2; // Install Failure

}

//

// Driver Does not exist, Create and call the API.

// HardwareID must be a multi-sz string, which argv[2] is.

//

if (!InstallRootEnumeratedDriver(HWID, // HardwareID

FName, // FileName

&RebootRequired))

{

//_tprintf(TEXT("(InstallRootEnumeratedDriver Failure! Code = 2)/n

"));

return 2; // Install Failure

}

}

//_tprintf(TEXT("Driver Installed successfully./n"));

if (RebootRequired)

{

//_tprintf(TEXT("(Reboot Required)/n"));

return 1; // Install Success, reboot required.

}

return 0; // Install Success, no reboot required.

}

int RemoveDriver(_TCHAR *HardwareID)

{

HDEVINFO DeviceInfoSet;

SP_DEVINFO_DATA DeviceInfoData;

DWORD i,err;

DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes

0,

0,

DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system

if (DeviceInfoSet == INVALID_HANDLE_VALUE)

{

DisplayError(TEXT("GetClassDevs(All Present Devices)"));

return 1;

}

//

//  Enumerate through all Devices.

//

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,

&buffersize))

{

if (GetLastError() == ERROR_INVALID_DATA)

{

//

// May be a Legacy Device with no HardwareID. Continue.

//

break;

}

else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)

{

//

// We need to change the buffer size.

//

if (buffer)

LocalFree(buffer);

buffer = (char *)LocalAlloc(LPTR,buffersize);

}

else

{

//

// Unknown Failure.

//

DisplayError(TEXT("GetDeviceRegistryProperty"));

goto cleanup_DeviceInfo;

}

}

if (GetLastError() == ERROR_INVALID_DATA)

continue;

//

// Compare each entry in the buffer multi-sz list with our HardwareID.

//

for (p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))

{

//_tprintf(TEXT("Compare device ID: [%s]/n"),p);

if (!_tcscmp(HardwareID,p))

{

//_tprintf(TEXT("Found! [%s]/n"),p);

//

// Worker function to remove device.

//

if (!SetupDiCallClassInstaller(DIF_REMOVE,

DeviceInfoSet,

&DeviceInfoData))

{

DisplayError(TEXT("CallClassInstaller(REMOVE)"));

}

break;

}

}

if (buffer) LocalFree(buffer);

}

if ((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_MORE_ITEMS))

{

DisplayError(TEXT("EnumDeviceInfo"));

}

//

//  Cleanup.

//

cleanup_DeviceInfo:

err = GetLastError();

SetupDiDestroyDeviceInfoList(DeviceInfoSet);

return err;

}

时间: 2024-11-06 07:27:55

VC安装驱动源码的相关文章

linux驱动开发之蜂鸣器驱动源码分析(一)

蜂鸣器的驱动源码在/driver/char/buzzer/x210-buzzer.c文件中,源码如下 #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/poll.h> #include <asm/irq.

编译 wl18xx驱动源码

在做beagleboneblack移植的时候,wl18xx的驱动源码是自动编译的.但是移植到其他平台优越平台不一样,所以就不能自动编译 所以用其他方式编译.http://e2e.ti.com/support/embedded/android/f/509/p/272074/951251  在TI找到的解决方案 修改源码里面的Makefile的内容. TI源码里面的驱动根目录下的Makefile ....................................................

Linux安装yum源码包及相关操作

一.使用场景 很多时候下载的rpm包所提供的功能并不符合我们的需要,或者只需要使用其中一部分功能的时候,就需要进行源码安装.源码安装前,首先使用 yum grouplist 确认 Development tools 和 Server Platform Development这两个组已经被安装. 二.相关知识详解 1.make工具 所有的程序都需要gcc编译后才能执行,项目的开发者为了简化使用者的编译步骤,当时用make工具后,make会调用gcc对程序进行编译,所以说make是一种项目编译工具,

hadoop2.5发布:最新编译 32位、64位安装、源码包、API以及新特性

hadoop2.5发布:最新编译 32位.64位安装.源码包.API以及新特性 http://www.aboutyun.com/thread-8751-1-1.html (出处: about云开发) 问题导读:1.如何获取Hadoop安装包?2.编译Hadoop过程中,需要注意哪些问题?3.如何寻找API?4.如何获取Hadoop源码? 上述问题有的在本文,有的则在本文链接,感兴趣,可以找找答案 2014年08月06日 Hadoop2.5发布 官网下载地址 对Hadoop2.5进行了编译,编译的

hadoop2.6.0汇总:新增功能最新编译 32位、64位安装、源码包、API下载及部署文档

相关内容: hadoop2.5.2汇总:新增功能最新编译 32位.64位安装.源码包.API.eclipse插件下载Hadoop2.5 Eclipse插件制作.连接集群视频.及hadoop-eclipse-plugin-2.5.0插件下载hadoop2.5.1汇总:最新编译 32位.64位安装.源码包.API下载及新特性等 新手指导:hadoop官网介绍及如何下载hadoop(2.4)各个版本与查看hadoop API介绍 从零教你在Linux环境下(ubuntu 12.04)如何编译hadoo

安装apached源码包以及编写shell脚本使其启动更加方便,并且和启动系统服务相同

实验目的:安装apached源码包,编写apached启动脚本,使其像启动系统服务一样 实验要求:用shell编写启动脚本 实验环境:Linux系统以及apached源码包       httpd-2.2.25.tar.gz 源码包需要编译安装所以在安装源码包时,查看系统中是否已安装编译工具 make gcc gcc-c++  "开发工具" 等软件包. 解压软件包,解压在当前目录下. 编译源码包并且指定安装目录为/usr/local/apached目录下. 3.安装源码包. 4.查看端

karloop介绍--hello world大家好,今天为大家介绍一款非常轻量级的的web开发框架,karloop框架。使用python开发 首先我们下载karloop源码进行安装。 源码地址 下载成

大家好,今天为大家介绍一款非常轻量级的的web开发框架,karloop框架.使用python开发 首先我们下载karloop源码进行安装. 源码地址 下载成功后解压,进入解压后的路径,进入终端,运行命令:sudo python setup.py install 如果是window系统,则打开cmd,运行:python setup.py install 下载安装成功后,我们写一个hello.py 内容如下: # coding=utf-8 from karloop.KarlBaseApplicati

用VC编译lua源码,生成lua语言的解释器和编译器

用VC编译lua源码,生成lua语言的解释器和编译器 1.去网址下载源码 http://www.lua.org/download.html 2.装一个VC++,我用的是VC6.0 3.接下来我们开始编译源码,我们需要编译: 一个静态库 一个动态库 一个lua语言解释器 一个lua编译器 建立一个工静态库工程 打开VC-->文件-->(点击)新建--(弹出框中选择)工程-->(win32 static library) 创一个空的工程 工程名为luaLib 把lua中所有的源码添加,去掉其

linux软件包的安装之----源码编译安装

前提:必须装好前面说过的开发环境,rhel6上的3个开发包组 Rhel5/centos5上面安装: 1)Development Tools (2)Server Platform Development (3)Develogmentlibraries Rhel6/centos6上面安装: 1)  Development Tools (2)Server Platform Development (3)Desktop PlatformDevelopment **由于tarball中的c程序源码文件之间