[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Service Handle

hi, I need to find out how to get the Service handle.

on forms, I use Application.handle, in units, I use form1.handle.

how can I show the handle of the service app?

Sal.
0
SaLz
Asked:
SaLz
4 Solutions
 
geobulCommented:
Hi,

Service1.ServiceThread.Handle

is the handle of your service thread.

Regards, Geo
0
 
SaLzAuthor Commented:
ok, another problem, in a form, for the location of the exe, I normally put Application.ExeName to get the path and exe name,

how can I get this in a service?
0
 
vaceroseCommented:
This is a very inefficient function but it works.

function GetAppPath: String;
Var
  Test : String;
  Res  : Longint;
  CurSize : Longint;
begin
  CurSize := 1024;
  SetLength(Test, CurSize);
  Res := GetModuleFilename(GetModuleHandle(nil), PChar(test), CurSize);
  If (res > curSize) then
  begin
    CurSize := res + 10;
    SetLength(Test, CurSize);
    Res := GetModuleFilename(GetModuleHandle(nil), PChar(test), CurSize);
  end;
  Setlength(Test, Res);
  test := ExtractFilePath(Test);
  Result := test;
end;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
geobulCommented:
TServiceApplication object doesn't have such property. I haven't tried that but there is such information (lpBinaryPathName) in QUERY_SERVICE_CONFIG structure defined in WinSvc unit. You have to:
- open the service manager using OpenSCManager,
- then open the service in interest using OpenService API (dwDesiredAccess = SERVICE_QUERY_CONFIG) which will return a handle for the next step,
- request that info using QueryServiceConfig API,
- get that element 'lpBinaryPathName' from the returned structure,
- and finally close both handles (service handle and SCManager handle).

Regards, Geo
0
 
geobulCommented:
If vacerose's code works then there is a short version:

function GetAppExeName: string;
var
  FN: array[0..MAX_PATH - 1] of char;
begin
  SetString(result, FN, GetModuleFileName(GetModuleHandle(nil), FN, SizeOf(FN)));
end;

Regards, Geo
0
 
TheRealLokiSenior DeveloperCommented:
I'm guessing just paramstr(0) doesn't work in a service then? That's how i get the current file's path and name
0
 
SaLzAuthor Commented:
lol, u know what, I totally forgot all about paramstr(0) for exe location, it works in services.
0
 
geobulCommented:
I also forgot about that :-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now