Skip to content

Commit be1f841

Browse files
committed
Reintegrated Windows Service functions into main iss file
1 parent b9baf39 commit be1f841

4 files changed

+201
-215
lines changed

src/HackWindowsInstaller.iss

Lines changed: 181 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,186 @@ Type: files; Name: "{app}\Log*.txt"
404404

405405
[Code]
406406
407-
//Include type definition for Service Control Manager
408-
#include "incl_ServiceControlManager-definition.pas";
407+
//-------------------------------------------------------
408+
//Required definitions for Windows Service handling
409+
//from http://www.vincenzo.net/isxkb/index.php?title=Service_-_Functions_to_Start%2C_Stop%2C_Install%2C_Remove_a_Service
410+
411+
type
412+
SERVICE_STATUS = record
413+
dwServiceType : cardinal;
414+
dwCurrentState : cardinal;
415+
dwControlsAccepted : cardinal;
416+
dwWin32ExitCode : cardinal;
417+
dwServiceSpecificExitCode : cardinal;
418+
dwCheckPoint : cardinal;
419+
dwWaitHint : cardinal;
420+
end;
421+
HANDLE = cardinal;
422+
423+
424+
const
425+
SC_MANAGER_ALL_ACCESS = $f003f;
426+
SERVICE_QUERY_CONFIG = $1;
427+
SERVICE_CHANGE_CONFIG = $2;
428+
SERVICE_QUERY_STATUS = $4;
429+
SERVICE_START = $10;
430+
SERVICE_STOP = $20;
431+
SERVICE_ALL_ACCESS = $f01ff;
432+
SERVICE_WIN32_OWN_PROCESS = $10;
433+
SERVICE_WIN32_SHARE_PROCESS = $20;
434+
SERVICE_WIN32 = $30;
435+
SERVICE_INTERACTIVE_PROCESS = $100;
436+
SERVICE_BOOT_START = $0;
437+
SERVICE_SYSTEM_START = $1;
438+
SERVICE_AUTO_START = $2;
439+
SERVICE_DEMAND_START = $3;
440+
SERVICE_DISABLED = $4;
441+
SERVICE_DELETE = $10000;
442+
SERVICE_CONTROL_STOP = $1;
443+
SERVICE_CONTROL_PAUSE = $2;
444+
SERVICE_CONTROL_CONTINUE = $3;
445+
SERVICE_CONTROL_INTERROGATE = $4;
446+
SERVICE_STOPPED = $1;
447+
SERVICE_START_PENDING = $2;
448+
SERVICE_STOP_PENDING = $3;
449+
SERVICE_RUNNING = $4;
450+
SERVICE_CONTINUE_PENDING = $5;
451+
SERVICE_PAUSE_PENDING = $6;
452+
SERVICE_PAUSED = $7;
453+
454+
function OpenSCManager(lpMachineName, lpDatabaseName: string; dwDesiredAccess :cardinal): HANDLE;
455+
external 'OpenSCManagerA@advapi32.dll stdcall';
456+
457+
function OpenService(hSCManager :HANDLE;lpServiceName: string; dwDesiredAccess :cardinal): HANDLE;
458+
external 'OpenServiceA@advapi32.dll stdcall';
459+
460+
function CloseServiceHandle(hSCObject :HANDLE): boolean;
461+
external 'CloseServiceHandle@advapi32.dll stdcall';
462+
463+
function StartNTService(hService :HANDLE;dwNumServiceArgs : cardinal;lpServiceArgVectors : cardinal) : boolean;
464+
external 'StartServiceA@advapi32.dll stdcall';
465+
466+
function ControlService(hService :HANDLE; dwControl :cardinal;var ServiceStatus :SERVICE_STATUS) : boolean;
467+
external 'ControlService@advapi32.dll stdcall';
468+
469+
function QueryServiceStatus(hService :HANDLE;var ServiceStatus :SERVICE_STATUS) : boolean;
470+
external 'QueryServiceStatus@advapi32.dll stdcall';
471+
472+
473+
function OpenServiceManager() : HANDLE;
474+
begin
475+
if UsingWinNT() = true then begin
476+
Result := OpenSCManager('','ServicesActive',SC_MANAGER_ALL_ACCESS);
477+
if Result = 0 then
478+
MsgBox('the servicemanager is not available', mbError, MB_OK)
479+
end
480+
else begin
481+
MsgBox('only nt based systems support services', mbError, MB_OK)
482+
Result := 0;
483+
end
484+
end;
485+
486+
function IsServiceInstalled(ServiceName: string) : boolean;
487+
var
488+
hSCM : HANDLE;
489+
hService: HANDLE;
490+
begin
491+
hSCM := OpenServiceManager();
492+
Result := false;
493+
if hSCM <> 0 then begin
494+
hService := OpenService(hSCM,ServiceName,SERVICE_QUERY_CONFIG);
495+
if hService <> 0 then begin
496+
Result := true;
497+
CloseServiceHandle(hService)
498+
end;
499+
CloseServiceHandle(hSCM)
500+
end
501+
end;
502+
503+
function StartService(ServiceName: string) : boolean;
504+
var
505+
hSCM : HANDLE;
506+
hService: HANDLE;
507+
begin
508+
hSCM := OpenServiceManager();
509+
Result := false;
510+
if hSCM <> 0 then begin
511+
hService := OpenService(hSCM,ServiceName,SERVICE_START);
512+
if hService <> 0 then begin
513+
Result := StartNTService(hService,0,0);
514+
CloseServiceHandle(hService)
515+
end;
516+
CloseServiceHandle(hSCM)
517+
end;
518+
end;
519+
520+
function StopService(ServiceName: string) : boolean;
521+
var
522+
hSCM : HANDLE;
523+
hService: HANDLE;
524+
Status : SERVICE_STATUS;
525+
begin
526+
hSCM := OpenServiceManager();
527+
Result := false;
528+
if hSCM <> 0 then begin
529+
hService := OpenService(hSCM,ServiceName,SERVICE_STOP);
530+
if hService <> 0 then begin
531+
Result := ControlService(hService,SERVICE_CONTROL_STOP,Status);
532+
CloseServiceHandle(hService)
533+
end;
534+
CloseServiceHandle(hSCM)
535+
end;
536+
end;
537+
538+
function IsServiceRunning(ServiceName: string) : boolean;
539+
var
540+
hSCM : HANDLE;
541+
hService: HANDLE;
542+
Status : SERVICE_STATUS;
543+
begin
544+
hSCM := OpenServiceManager();
545+
Result := false;
546+
if hSCM <> 0 then begin
547+
hService := OpenService(hSCM,ServiceName,SERVICE_QUERY_STATUS);
548+
if hService <> 0 then begin
549+
if QueryServiceStatus(hService,Status) then begin
550+
Result :=(Status.dwCurrentState = SERVICE_RUNNING)
551+
end;
552+
CloseServiceHandle(hService)
553+
end;
554+
CloseServiceHandle(hSCM)
555+
end
556+
end;
557+
558+
//Returns TRUE if a service was started
559+
function StartNTService2(serviceName:string):boolean;
560+
begin
561+
if IsServiceInstalled(serviceName) then begin
562+
if IsServiceRunning(serviceName)=false then begin
563+
log('Starting service ' + serviceName);
564+
StartService(serviceName);
565+
sleep(1500); //give the service some seconds
566+
result:=true;
567+
end;
568+
end;
569+
end;
570+
571+
//Stops a service and returns TRUE if it was stopped
572+
function StopNTService2(serviceName:string):boolean;
573+
begin
574+
if IsServiceInstalled(serviceName) then begin
575+
if IsServiceRunning(serviceName) then begin
576+
log('Stopping service ' + serviceName);
577+
StopService(serviceName);
578+
sleep(1500);
579+
result:=true;
580+
end;
581+
end;
582+
end;
583+
584+
//Windows Service Handling END
585+
//-------------------------------------------------------
586+
409587
410588
411589
var
@@ -436,8 +614,7 @@ var
436614
FontStateBuffer: array of string;
437615
438616
439-
//Include functions for Service Control Manager
440-
#include "incl_ServiceControlManager-functions.pas";
617+
441618
442619
443620

src/HackWindowsInstaller_TEMP_Preprocessed.iss

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ Type: files; Name: "{app}\Log*.txt"
285285

286286
[Code]
287287
288-
//--- START incl_ServiceControlManager-definition.iss ---
289288
290289
type
291290
SERVICE_STATUS = record
@@ -329,30 +328,6 @@ const
329328
SERVICE_CONTINUE_PENDING = $5;
330329
SERVICE_PAUSE_PENDING = $6;
331330
SERVICE_PAUSED = $7;
332-
333-
//--- END incl_ServiceControlManager-definition.iss ---
334-
335-
336-
337-
var
338-
339-
FontFiles: array of string;
340-
FontFilesHashes: array of string;
341-
FontFilesNames: array of string;
342-
343-
InstalledFontsHashes: array of string;
344-
345-
FontCacheService_Stopped:boolean;
346-
FontCache30Service_Stopped:boolean;
347-
348-
BeforeInstallActionWasRun:boolean;
349-
350-
ChangesRequired:boolean;
351-
352-
FontStateBuffer: array of string;
353-
354-
355-
//--- START incl_ServiceControlManager-functions.iss ---
356331
357332
function OpenSCManager(lpMachineName, lpDatabaseName: string; dwDesiredAccess :cardinal): HANDLE;
358333
external 'OpenSCManagerA@advapi32.dll stdcall';
@@ -484,8 +459,26 @@ end;
484459
485460
486461
487-
//--- END incl_ServiceControlManager-functions.iss ---
488-
462+
463+
var
464+
465+
FontFiles: array of string;
466+
FontFilesHashes: array of string;
467+
FontFilesNames: array of string;
468+
469+
InstalledFontsHashes: array of string;
470+
471+
FontCacheService_Stopped:boolean;
472+
FontCache30Service_Stopped:boolean;
473+
474+
BeforeInstallActionWasRun:boolean;
475+
476+
ChangesRequired:boolean;
477+
478+
FontStateBuffer: array of string;
479+
480+
481+
489482
490483
491484

src/incl_ServiceControlManager-definition.pas

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)