DELPHI的代码:
function load_driver(): boolean;
var
temp: dword;
begin
{
temp := LoadLibrary('advapi32.dll');
if temp <> 0 then
begin
@CreateService1 := GetProcAddress(temp, 'CreateServiceA');
@OpenService1 := GetProcAddress(temp, 'OpenServiceA');
if hService = 0 then
begin
{
OpenService1(hSCManager,'VirtToPhys',SERVICE_START + SERVICE_STOP + _DELETE);
}
OpenService(hSCManager,'VirtToPhys',SERVICE_START + SERVICE_STOP + _DELETE); //注:这句一点效果都没有。
end;
if hService <> 0 then
begin
{驱动程序的DriverEntry过程将被调用}
if StartService(hService, 0, lpTemp) = true then
begin
{驱动程序将接收IRP_MJ_CREATE I/O请求包(IRP)}
hDevice := CreateFile('\\.\slVirtToPhys', GENERIC_READ + GENERIC_WRITE,
0, nil, OPEN_EXISTING, 0, 0);
if hDevice = INVALID_HANDLE_VALUE then
begin
unload_driver();
ShowMessage('打开设备出错');
Exit;
end;
end
else begin
unload_driver();
ShowMessage('运行驱动出错');
Exit;
end;
end
else begin
unload_driver();
ShowMessage('创建驱动出错');
Exit;
end;
end;
end;
function unload_driver(): boolean;
begin
try
if hSCManager <> 0 then
begin
if hService <> 0 then
begin
if hDevice <> 0 then CloseHandle(hDevice);
ControlService(hService, SERVICE_CONTROL_STOP, _ss);
DeleteService(hService);
CloseServiceHandle(hService);
end;
CloseServiceHandle(hSCManager);
end;
except
end;
end;
C的代码:
#include <windows.h>
#include <stdio.h>
int main(int argc,char **argv)
{
printf("Load Driver\n载入驱动\n");
HANDLE scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);