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_PROCES
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
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
Main Topics
Browse All Topics





by: nabehsPosted on 2004-08-27 at 00:43:22ID: 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.