Solved

Cannot get the function "OpenService" to return a valid handle

Posted on 2008-10-25
1
1,323 Views
Last Modified: 2013-12-04
Hi All,

I cannot get the OpenService function to return a valid file handle (returns 0).

Below is some code which illustrates the point and is self explanatory.

Any help with this would be greatly appreciated.

I am using Borland Delphi 2005 on a XP Professional SP2 PC.

Thanks in advance.

Ken.

var
  hdlOpenSCManager,
  hdlOpenService : SC_Handle;
  ServStat : TServiceStatus;
  dwStatus : DWORD;
begin
  dwStatus := DWORD (-1);

  hdlOpenSCManager := OpenSCManager (Nil, Nil, SC_MANAGER_ALL_ACCESS);

  if (hdlOpenSCManager > 0) then
    begin
      hdlOpenService := OpenService(hdlOpenSCManager, PChar('Windows Management Instrumentation'), SC_MANAGER_ALL_ACCESS);

  if hdlOpenService = 0 then
    ShowMessage(SysErrorMessage(GetLastError));

  // hdlOpenService always equals zero despite the fact the the service is both installed and running. Other services I have tested have given me the same result.

  // The error message is; "The specified service does not exist as an installed service"
0
Comment
Question by:systemsgo
1 Comment
 

Accepted Solution

by:
systemsgo earned 0 total points
ID: 22806352
Well, it appears that it isn't the display name of the service I should be using but the short version stored in the registry - well you learn something new everyday.

The working code is;

function ServiceGetStatus(sMachine, sService : string ) : DWORD;
var
  hdlOpenSCManager,
  hdlOpenService : SC_Handle;
  ServStat : TServiceStatus;
begin
  Result := DWORD (-1);

  hdlOpenSCManager := OpenSCManager(Nil, Nil, SC_MANAGER_ALL_ACCESS);

  if hdlOpenSCManager = 0 then
    exit;

  hdlOpenService := OpenService(hdlOpenSCManager, PChar(sService), SC_MANAGER_ALL_ACCESS);

  if hdlOpenService = 0 then
    begin
      CloseServiceHandle(hdlOpenSCManager);
      Exit;
    end;

  if QueryServiceStatus(hdlOpenService, ServStat) then
    Result := ServStat.dwCurrentState;

  CloseServiceHandle(hdlOpenService);
  CloseServiceHandle(hdlOpenScManager);
end;


So all the points go to me LOL.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question