Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Uninstall Windows Service Delphi

Posted on 2012-03-28
Medium Priority
Last Modified: 2013-12-03
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

Question by:ChrisJonesLycos
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
  • 3
  • 2
  • 2
LVL 38

Expert Comment

by:Geert Gruwez
ID: 37779274
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

Author Comment

ID: 37780423
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.

Expert Comment

ID: 37781120
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 37781122
Actually it is
for cnt := 0 to M.count- 1 do
  S:= M.services[cnt];

Author Comment

ID: 37781151
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?
LVL 38

Accepted Solution

Geert Gruwez earned 2000 total points
ID: 37781190
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);

Author Closing Comment

ID: 37781216
Brilliant! That works.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

604 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