Uninstall Windows Service Delphi

I'm using the following code (somewhat simplified) to uninstall a Windows service.

Each time it returns an error of ERROR_INVALID_HANDLE. Is there something wrong with the code? Is there a better way to do it? I know there should be a few more checks and try-finally's but OpenSCManager and OpenService both return valid values whereas DeleteService always returns a False.

  schm: SC_Handle;
  dwResult, dwLastError: DWord;
  ss: TServiceStatus;
  bResult: Boolean;
  dwResult := 0;

  schm := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if schm > 0 then
    dwResult := OpenService(schm, ServiceName, SERVICE_ALL_ACCESS);    
    bResult := DeleteService(schm);
    if not bResult then
      dwLastError:= GetLastError;
      if dw = ERROR_ACCESS_DENIED then
      else if dw = ERROR_INVALID_HANDLE then
      else if dw = ERROR_SERVICE_MARKED_FOR_DELETE then

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Geert GOracle dbaCommented:
have you tried ?

sc delete servicename

from services.msc, right click on the properties to get the name

you need to run your program as administrator if uac is enabled (vista, win7, win2k8r2)

having services.msc open, may cause a problem too
close and open it to refresh
ChrisJonesLycosAuthor Commented:
This is something that has to be done in code. I already have the working code for Install, Start, Stop Service  but for some reason can't get the Uninstall to work for the above reasons.
I understand you are providing service name - hardcoded or perhaps passed using a parameter.
The reason of you getting handle error is service name as registered is different from service name as assigned by you.
Why dont you enumerate service name and search for service name which you wants to delete.
The following are attributed to christian wimmer and i am quoting it.

You can use file JclSvcCtrl.pas from JEDI Components Library (JCL)
M := TJclSCManager.Create;
For i := 0 to M.Count -1 do
  S := M.Services; // TJclNtService
  if CompareText(S.ServiceName, 'bla') then
  showmessage('service found');
and this will confirm that you are passing proper service name.
Because there definitely is a service name mismatch.
There is another function in this component
you can use either to confirm.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Actually it is
for cnt := 0 to M.count- 1 do
  S:= M.services[cnt];
ChrisJonesLycosAuthor Commented:
The service name is the same as the one I'm using in Install, Start and Stop Service functions and these all work fine. Why would it not be the right name for Uninstall?
Geert GOracle dbaCommented:
OpenService returns the handle to the service
you are trying to delete using the handle of the service manager ...

    hService := OpenService(schm, ServiceName, SERVICE_ALL_ACCESS);
    bResult := DeleteService(hService);

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
ChrisJonesLycosAuthor Commented:
Brilliant! That works.
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.