Solved

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

Posted on 2008-10-25
1
1,310 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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 use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

932 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now