- 注册时间
- 2010-7-27
- 最后登录
- 2017-5-28
- 在线时间
- 6 小时
编程入门
- 魔鬼币
- 559
|
C++通用动态加载驱动源码
#include "stdafx.h"
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
//获取sys文件的完整路径
char szDriverPath[256];
//驱动程序的符号链接
char szLinkName[]="sldevDriverDemo";
char* p;
GetFullPathName("DriverDemo.sys",256,szDriverPath,&p);
//打开SCM管理器
SC_HANDLE hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (hSCM==NULL)
{
printf("打开服务控制管理器失败!");
return -1;
}
//打开或创建服务
SC_HANDLE hService=CreateService(hSCM,szLinkName,szLinkName,SERVICE_ALL_ACCESS,SERVICE_KERNEL_DRIVER,SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,szDriverPath,NULL,0,NULL,NULL,NULL);
if(hService == NULL)
{
int nError = GetLastError();
//如果服务已经存在或者已被标记删除(但是还么被删除)
if(nError == ERROR_SERVICE_EXISTS || nError == ERROR_SERVICE_MARKED_FOR_DELETE)
{
hService = ::OpenService(hSCM, szLinkName, SERVICE_ALL_ACCESS);
}
}
if(hService == NULL)
{
printf(" 创建服务出错!\n");
return -1;
}
//打开服务,开始进入DriverEntry例程
if (!StartService(hService,0,NULL))
{
int error=GetLastError();
if (error!=ERROR_SERVICE_ALREADY_RUNNING)
{
printf("服务早已启动!");
return -1;
}
}
SERVICE_STATUS ss;
//等待服务的结束
ControlService(hService,SERVICE_CONTROL_STOP,&ss);
//删除服务
DeleteService(hService);
//释放服务句柄
CloseServiceHandle(hService);
//释放SCM句柄
CloseServiceHandle(hSCM);
//暂停
system("pause");
return 0;
}
PS:通用的加载器基本的就是上面这样,在同时用个DbgViewer就可以进行调试啦!
|
|