Solved

Uninstall Windows Service Delphi

Posted on 2012-03-28
7
1,012 Views
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.

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

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

    CloseServiceHandle(schm);
  end;
end;
0
Comment
Question by:ChrisJonesLycos
  • 3
  • 2
  • 2
7 Comments
 
LVL 36

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
0
 

Author Comment

by:ChrisJonesLycos
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.
0
 
LVL 9

Expert Comment

by:rinfo
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
begin
  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
m.findservice(servicename);
you can use either to confirm.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 9

Expert Comment

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

Author Comment

by:ChrisJonesLycos
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?
0
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 500 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);
0
 

Author Closing Comment

by:ChrisJonesLycos
ID: 37781216
Brilliant! That works.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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 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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

23 Experts available now in Live!

Get 1:1 Help Now