Solved

how to register windows service?

Posted on 2004-08-26
4
24,191 Views
Last Modified: 2013-11-20

I have created a windows service using visual C++.

Kindly tell me how do i register its .exe file as windows service.

What command will be used and what steps ll be needed.

reply soon,

Thanks
0
Comment
Question by:capriono
[X]
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
4 Comments
 
LVL 6

Expert Comment

by:nabehs
ID: 11910519
If you are building the service using ATL COM then to register the service just build the service and it will automatically be registered on the current machine. If you need to register it on another machine then run:
path\service_name.exe /RegServer

and it will be registered.

To start and stop the service you need to go to Control Panel, Administrative Tools, Services and locate the service after installation and start it or stop it.
0
 
LVL 1

Accepted Solution

by:
hsdhina earned 30 total points
ID: 11910640
Programatically you can register & unregister using the following code.

VOID InstallService ()
{
  SC_HANDLE hSCManager, hService;
  TCHAR szImagePath[MAX_PATH];

  hSCManager = OpenSCManager ( NULL, NULL, SC_MANAGER_ALL_ACCESS );
  if( !hSCManager )
  {
    LogError ( "OpenSCManager Failed" );
    return;
  }
  GetModuleFileName ( NULL, szImagePath, sizeof(szImagePath) );
  hService = CreateService ( hSCManager, "[SERVICE NAME]", "DISPLAY NAME", SERVICE_ALL_ACCESS,
                             SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
                             SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, szImagePath, NULL, NULL, NULL, NULL, NULL );
  if ( !hService )
  {
    LogError ( "CreateService Failed" );
    CloseServiceHandle ( hSCManager );
    return;
  }
  CloseServiceHandle ( hService );
  CloseServiceHandle ( hSCManager );
  HKEY hKey = NULL;
  DWORD dwDisposition = 0;
  LPSTR pDescr = "DESCRIPTION";
  if ( ERROR_SUCCESS != RegCreateKeyEx ( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\[NAME]", 0, NULL,
                                         REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisposition ) )
    return;
  RegSetValueEx ( hKey, (LPCTSTR)"Description", 0, REG_SZ, (CONST BYTE*)pDescr, (DWORD)lstrlen ( pDescr ) );
  RegCloseKey ( hKey );
}


VOID UnInstallService ()
{
  SC_HANDLE hSCManager, hService;

  hSCManager = OpenSCManager ( NULL, NULL, SC_MANAGER_ALL_ACCESS );
  if( !hSCManager )
  {
    LogError ( "OpenSCManager Failed" );
    return;
  }
  hService = OpenService ( hSCManager, "SERVICE NAME", DELETE );
  if ( !hService )
  {
    LogError ( "CreateService Failed" );
    CloseServiceHandle ( hSCManager );
    return;
  }
  DeleteService ( hService );
  CloseServiceHandle ( hService );
  CloseServiceHandle ( hSCManager );
}

void main ( int argc, char** argv )
{
  if ( argc == 2 )
  {
    strlwr ( argv[1] );
    if ( !lstrcmpi ( argv[1], "install" ) )
    {
      InstallService ();
      return;
    }
    if ( !lstrcmpi ( argv[1], "uninstall" ) )
    {
      UnInstallService ();
      return;
    }
}


Hope the above code will help.

Thanks
hsdhina
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Complete beginner needs help making a cron job 9 133
iSeries DB2 Query 2 101
notReplace  challenge 53 152
fizzArray  challenge 1 114
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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