Autor Beitrag
Flamefire
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1207
Erhaltene Danke: 31

Win 10
Delphi 2009 Pro, C++ (Visual Studio)
BeitragVerfasst: Mo 09.11.09 23:34 
Habe folgenden Code zum starten/Beenden eines Services (Treiber (sys-Datei))

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
function InstallService(ServiceName: PChar; FileName: String):Boolean;
var
  SCManager: SC_HANDLE;
  Service: SC_HANDLE;
  pArgs:PChar;
begin
  Result:=false;
  SCManager := OpenSCManager(nilnil, SC_MANAGER_ALL_ACCESS);
  if SCManager = 0 then Exit;
  try
    Service := CreateService(SCManager, ServiceName, ServiceName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, PChar(FileName), nilnilnilnilnil);
    if(Service=0then begin
      if(GetLastError()=ERROR_SERVICE_EXISTS) then begin
        Service:=OpenService(SCManager,ServiceName,SERVICE_ALL_ACCESS);
      end;
    end;
    if(Service<>0then begin
      pArgs:=nil;
      if(StartService(Service,0,pArgs)) then Result:=true
      else if(GetLastError()=ERROR_SERVICE_ALREADY_RUNNING) then begin
        Result:=true;
      end;
      CloseServiceHandle(Service);
    end;
  finally
    CloseServiceHandle(SCManager);
  end;
end;

function GetService(ServiceName: PChar):SC_HANDLE;
var SCManager: SC_HANDLE;
begin
  Result:=0;
  SCManager := OpenSCManager(nilnil, SC_MANAGER_ALL_ACCESS);
  if SCManager = 0 then Exit;
  try
    Result := OpenService(SCManager,ServiceName,SERVICE_ALL_ACCESS);
  finally
    CloseServiceHandle(SCManager);
  end;
end;

function UnInstallService(ServiceName: PChar):Boolean;
var
  Service: SC_HANDLE;
  SS:_SERVICE_STATUS;
begin
  Service := GetService(ServiceName);
  if Service = 0 then begin
    if(GetLastError()=ERROR_SERVICE_DOES_NOT_EXIST) then exit(true)
    else exit(false);
  end;
  try
    if(ControlService(Service,SERVICE_CONTROL_STOP,SS)) and (DeleteService(Service)) then Result:=true
    else if(GetLastError()=ERROR_SERVICE_NOT_ACTIVE) then Result:=true
    else Result:=false;
  finally
    CloseServiceHandle(Service);
  end;
end;


Das sollte mir die InstDrv.exe nachahmen (mit der hab ich 4 Buttons: Install, Start, Stop, Remove)

aber anscheinend klappt bei mir das remove/stop nicht (Hab jeweils die 2 in eine funktion gepackt)
denn wenn ich uninstall aufrufe, müsste der service weg sein, und wenn ichs nochmal aufrufe müsste er sagen: Service existiert nicht.
Macht er aber nicht. er gibt immer true zurück
SAiBOT
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 323
Erhaltene Danke: 6

XP SP2; 7
D7; D2009
BeitragVerfasst: Di 10.11.09 15:57 
Das habe ich auch mal gemacht, kannst dir ja mal den source ansehen!

_________________
Debuggers don't remove bugs, they only show them in slow-motion.


Zuletzt bearbeitet von SAiBOT am Di 10.11.09 18:33, insgesamt 1-mal bearbeitet
Flamefire Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1207
Erhaltene Danke: 31

Win 10
Delphi 2009 Pro, C++ (Visual Studio)
BeitragVerfasst: Di 10.11.09 16:41 
hm...
wenn du den source mit anhängst könnte ich das bestimmt ;-)
SAiBOT
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 323
Erhaltene Danke: 6

XP SP2; 7
D7; D2009
BeitragVerfasst: Di 10.11.09 18:34 
Oh sorry, habe die falsche rar hochgeladen :oops: , jetzt gehts :D

_________________
Debuggers don't remove bugs, they only show them in slow-motion.
Flamefire Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1207
Erhaltene Danke: 31

Win 10
Delphi 2009 Pro, C++ (Visual Studio)
BeitragVerfasst: Di 10.11.09 23:07 
hmm...
Danke aber ich sehe keine unterschiede
Hast du mal probiert, ob man bei dir "erfolgreich" einen Dienst 2mal entfernen kann?

Edit: hat sich erledigt...ging nur mal nicht, als ich ne Bug im Treiber hatte...
trotzdem danke ;-)