Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to uninstall a service when it is stopped?

How to uninstall a service when it is stopped?

Scheduled Pinned Locked Moved C / C++ / MFC
questionwindows-adminhelptutorial
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharkmouse
    wrote on last edited by
    #1

    hello, i write a service and it can run well,but i have found a problem,that is if the service is running i can uninstall it successfully(stop it ,delete the record in the service list and registry),but if the service is already stoppped,then i can not delete the service(the record is still in the service list and the registry),and the service labeld disabled and the service can not be installed again after reboot the machine,what is the matter?what can i do to solve this problem?

    S G 3 Replies Last reply
    0
    • S sharkmouse

      hello, i write a service and it can run well,but i have found a problem,that is if the service is running i can uninstall it successfully(stop it ,delete the record in the service list and registry),but if the service is already stoppped,then i can not delete the service(the record is still in the service list and the registry),and the service labeld disabled and the service can not be installed again after reboot the machine,what is the matter?what can i do to solve this problem?

      S Offline
      S Offline
      sharkmouse
      wrote on last edited by
      #2

      nobody knows why?

      1 Reply Last reply
      0
      • S sharkmouse

        hello, i write a service and it can run well,but i have found a problem,that is if the service is running i can uninstall it successfully(stop it ,delete the record in the service list and registry),but if the service is already stoppped,then i can not delete the service(the record is still in the service list and the registry),and the service labeld disabled and the service can not be installed again after reboot the machine,what is the matter?what can i do to solve this problem?

        G Offline
        G Offline
        Galatei
        wrote on last edited by
        #3

        Hi, You should post a bit of code, responsible for the process of service uninstall. Regards

        S 1 Reply Last reply
        0
        • G Galatei

          Hi, You should post a bit of code, responsible for the process of service uninstall. Regards

          S Offline
          S Offline
          sharkmouse
          wrote on last edited by
          #4

          the code is below,all code i found to uninstall service like this: void CService::UnInstallService(TCHAR *tcServiceName) { SC_HANDLE schService; SC_HANDLE schSCManager; if(lstrlen(tcServiceName)<=0) return; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = OpenService(schSCManager, tcServiceName, SERVICE_ALL_ACCESS); if (schService) { // try to stop the service if ( ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus ) ) { _tprintf(TEXT("Stopping %s."), tcServiceName); Sleep( 1000 ); while( QueryServiceStatus( schService, &serviceStatus ) ) { if ( serviceStatus.dwCurrentState == SERVICE_STOP_PENDING ) { _tprintf(TEXT(".")); Sleep( 1000 ); } else break; } if ( serviceStatus.dwCurrentState == SERVICE_STOPPED ) _tprintf(TEXT("\n%s stopped.\n"), tcServiceName ); else _tprintf(TEXT("\n%s failed to stop.\n"), tcServiceName ); } // now remove the service if( DeleteService(schService) ) _tprintf(TEXT("%s removed.\n"), tcServiceName ); else _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256)); CloseServiceHandle(schService); } else _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256)); CloseServiceHandle(schSCManager); } else _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256)); } someone can help me or give me the reason?

          G 1 Reply Last reply
          0
          • S sharkmouse

            hello, i write a service and it can run well,but i have found a problem,that is if the service is running i can uninstall it successfully(stop it ,delete the record in the service list and registry),but if the service is already stoppped,then i can not delete the service(the record is still in the service list and the registry),and the service labeld disabled and the service can not be installed again after reboot the machine,what is the matter?what can i do to solve this problem?

            S Offline
            S Offline
            sharkmouse
            wrote on last edited by
            #5

            everybody can download some sample code from here,and tried,almost have the problem.

            G 1 Reply Last reply
            0
            • S sharkmouse

              the code is below,all code i found to uninstall service like this: void CService::UnInstallService(TCHAR *tcServiceName) { SC_HANDLE schService; SC_HANDLE schSCManager; if(lstrlen(tcServiceName)<=0) return; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = OpenService(schSCManager, tcServiceName, SERVICE_ALL_ACCESS); if (schService) { // try to stop the service if ( ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus ) ) { _tprintf(TEXT("Stopping %s."), tcServiceName); Sleep( 1000 ); while( QueryServiceStatus( schService, &serviceStatus ) ) { if ( serviceStatus.dwCurrentState == SERVICE_STOP_PENDING ) { _tprintf(TEXT(".")); Sleep( 1000 ); } else break; } if ( serviceStatus.dwCurrentState == SERVICE_STOPPED ) _tprintf(TEXT("\n%s stopped.\n"), tcServiceName ); else _tprintf(TEXT("\n%s failed to stop.\n"), tcServiceName ); } // now remove the service if( DeleteService(schService) ) _tprintf(TEXT("%s removed.\n"), tcServiceName ); else _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256)); CloseServiceHandle(schService); } else _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256)); CloseServiceHandle(schSCManager); } else _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256)); } someone can help me or give me the reason?

              G Offline
              G Offline
              Galatei
              wrote on last edited by
              #6

              Make sure that SERVICE_STOP_PENDING is reported by service in ServiceCtrl callback function for SERVICE_CONTROL_STOP code as well as proper timeout for service stop completion. Regards

              1 Reply Last reply
              0
              • S sharkmouse

                everybody can download some sample code from here,and tried,almost have the problem.

                G Offline
                G Offline
                Galatei
                wrote on last edited by
                #7

                from where? :confused:

                S 1 Reply Last reply
                0
                • G Galatei

                  from where? :confused:

                  S Offline
                  S Offline
                  sharkmouse
                  wrote on last edited by
                  #8

                  you can download a sample service from the link: http://www.codeproject.com/system/cntservice.asp

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups