Solved

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

Posted on 2008-10-25
1
1,330 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to adjust sheet 1 96
Post a good COM tutorial 1 73
Why use this lambda? 12 97
MDT Create a bootable media for BIOS 2 26
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

752 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