Can you advice me how to get "Log On As" parameter of specific windows service? I need to re-register service in our upgrade project and it needs to be run under the same account as it was set up originally. I've found QueryServiceConfig in advapi32.dll with lpServiceStartName in returned structure but I am not able to make it work from Inno Setup.
Inno Setup Service Log On As
1.8k Views Asked by zdenok At
2
There are 2 best solutions below
4

I didn't liked the idea of linking external library so I finally solved the problem this way:
function GetServiceLogonAs():string;
var
res : Integer;
TmpFileName, FileContent: String;
begin
TmpFileName := ExpandConstant('{tmp}') + '\Service_Info.txt';
Exec('cmd.exe', '/C sc qc "MyServiceName" > "' + TmpFileName + '"', '', SW_HIDE, ewWaitUntilTerminated, res);
if LoadStringFromFile(TmpFileName, FileContent) then
begin
Result := Trim(Copy(FileContent,Pos('SERVICE_START_NAME', FileContent)+20,Length(FileContent)-(Pos('SERVICE_START_NAME', FileContent)+21)));
DeleteFile(TmpFileName);
end
else
begin
ShowErrorMsg('Error calling: GetServiceLogonAs(" + MYSERVICE + ")', res);
Result := '';
end;
end;
You cannot use
QueryServiceConfig
function from InnoSetup script. To use this function, you would have to allocate buffer from heap and that's impossible in InnoSetup. Instead you can use WMI, or to be more specific, theWin32_Service
WMI class, which contains theStartName
property, that you've asked for. In InnoSetup script it might look like this:Quite easier (and probably faster) would be to make an external library and call it from the script. If you have Delphi or Lazarus, you can use the following function, which uses the
QueryServiceConfig
function to get thelpServiceStartName
member, that you asked for: