Solved

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

Posted on 2008-10-25
1
1,347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

622 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