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

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.


  hdlOpenService : SC_Handle;
  ServStat : TServiceStatus;
  dwStatus : DWORD;
  dwStatus := DWORD (-1);

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

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

  if hdlOpenService = 0 then

  // 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"
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

systemsgoAuthor Commented:
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;
  hdlOpenService : SC_Handle;
  ServStat : TServiceStatus;
  Result := DWORD (-1);

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

  if hdlOpenSCManager = 0 then

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

  if hdlOpenService = 0 then

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


So all the points go to me LOL.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.