100分,会C的牛牛进来翻译一下这个代码(100分)

  • 主题发起人 主题发起人 pub66cn
  • 开始时间 开始时间
P

pub66cn

Unregistered / Unconfirmed
GUEST, unregistred user!
我对服务安装与启动部份不太熟悉。。
请牛牛们帮忙译一下以下代码为delphi

int main(int argc,char **argv)
{
printf("Load Driver/n");
HANDLE scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
if(scm)
{
printf("Create Service/n");
HANDLE svc=CreateService(scm,argv[1],argv[2],SERVICE_START | DELETE | SERVICE_STOP,SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,SERVICE_ERROR_IGNORE,argv[3],NULL,NULL,NULL,NULL,NULL);

if(!svc) svc=OpenService(scm,argv[1],SERVICE_START | DELETE | SERVICE_STOP);
if(svc)
{
printf("Starting service/n");

StartService(svc,0,NULL);

printf("Press Enter to stop and delete service /n");
getchar();

SERVICE_STATUS status;
ControlService(svc,SERVICE_CONTROL_STOP,&status);
DeleteService(svc);
CloseServiceHandle(svc);
} else printf("error: unable to create/open service/n");
CloseServiceHandle(scm);
} else printf("error: unable to open manager/n");
return 0;
}
 
加上WinSvc单元后
把函数中的NULL改成nil,|改成+,=改成:=基本上就能用了
 
服务类型:SERVICE_START + DELETE + SERVICE_STOP,//DELETE 提示出错;
StartService(svc,0,nil);//启动服务也提示出错;
该怎么改?
 
program svctest;
{$APPTYPE CONSOLE}
uses
Windows, WinSvc;
var
scm, svc: THandle;
sta: SERVICE_STATUS;
p: PChar;
begin
Writeln('Load Driver');

scm := openSCManager(nil, nil, SC_MANAGER_CREATE_SERVICE);
if scm > 0 then
try
Writeln('Create Service');
svc := CreateService(scm, PChar(ParamStr(1)), PChar(ParamStr(2)), SERVICE_START or Windows._DELETE or SERVICE_STOP, SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, PChar(ParamStr(3)), nil, nil, nil, nil, nil);
if svc = 0 then
svc := OpenService(scm, PChar(ParamStr(1)), SERVICE_START or Windows._DELETE or SERVICE_STOP);
if svc > 0 then
try
Writeln('Starting service');
StartService(svc, 0, p);
Writeln('Press Enter to stop and delete service');
Readln;
ControlService(svc, SERVICE_CONTROL_STOP, sta);
DeleteService(svc);
finally
CloseServiceHandle(svc);
end
else
Writeln('error: unable to create/open service');
finally
CloseServiceHandle(scm);
end
else
Writeln('error: unable to open manager');
end.
 
多人接受答案了。
 
后退
顶部