Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to stop a process launched using CreateProcess API

Status
Not open for further replies.

EzehM

Programmer
Feb 27, 2003
86
0
0
GB
I am writing a basic Windows NT service that launches a VB exe via CreateProcess, however when I stop the service via the Service Control Manager (the service is reported to have stopped) the VB exe still runs and I can only kill the process via the Task Manager. Please can anyone tell me if I have missed something in my code. Thanks

////////////CODE//////////////////




#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "Winsvc.h"
#include "time.h"
#include <tchar.h>
#include <wchar.h>
#include "Util.h"

SERVICE_STATUS m_ServiceStatus, ssStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
void CmdInstallService(TCHAR* pszDomainUsername, TCHAR* pszPassword);
void CmdRemoveService();
VOID AddEventSource();
VOID RemoveEventSource();
void ExecPullApp();


// event source
//HANDLE g_hEventSource = NULL;

void main(int argc, TCHAR **argv)
{

SERVICE_TABLE_ENTRY DispatchTable[]={{"FTMSPullService",ServiceMain},{NULL,NULL}};

if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) )
{
if ( _tcsicmp( _T("install"), argv[1]+1 ) == 0 )
{
// get the <domain> <username> <password>
if (argc > 3)
{
CmdInstallService(argv[2], argv[3]);
AddEventSource();
}
else
{
_tprintf(_T("usage: [-install <domain\\username> <password>] | [-remove] | [-debug]"));
exit(1);
}
}

else if ( _tcsicmp( _T("remove"), argv[1]+1 ) == 0 )
{
CmdRemoveService();
RemoveEventSource();
}
else
{
goto dispatch;
}
exit(1);
}

dispatch:
// this is just to be friendly
_tprintf( _T("%s -install <domain\\username> <password> to install the service\n"), "FTMSPull" );
_tprintf( _T("%s -remove to remove the service\n"), "FTMSPull" );
_tprintf( _T("\nStartServiceCtrlDispatcher being called.\n") );
_tprintf( _T("This may take several seconds. Please wait.\n") );

if (!StartServiceCtrlDispatcher(DispatchTable))
_tprintf( _T("StartServiceCtrlDispatcher function failed .....\n") );


/* if (g_hEventSource)
{
DeregisterEventSource(g_hEventSource);
g_hEventSource = NULL;
}
*/

}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{

//DWORD status;
//DWORD specificError;

m_ServiceStatus.dwServiceType = SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;

m_ServiceStatusHandle = RegisterServiceCtrlHandler("FTMSPullService",ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
return;
}

m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
{

}

bRunning=true;
while(bRunning)
{
Sleep(3000);
//Place Your Code to execute the Pull application here....

// Call your function to execute pull application
// aka ExecPullApp()
DebugMsg("About to execute script");
ExecPullApp();
}

return;
}


void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;

case SERVICE_CONTROL_CONTINUE:
DebugMsg("FTMSPull Application Started");
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;

case SERVICE_CONTROL_STOP:
DebugMsg("FTMSPull Application Stopped");
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;

SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);

bRunning=false;

break;

case SERVICE_CONTROL_INTERROGATE:
break;
}
return;
}

void CmdInstallService(TCHAR* pszDomainUsername, TCHAR* pszPassword)
{

TCHAR strDir[512];
HANDLE schSCManager,schService;


if ( GetModuleFileName ( NULL, strDir, 512) == 0)
{
_tprintf(_T("Unable to install %s - %s\n"), "FTMS Pull Service", GetLastError());
return;
}

schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);



if ( schSCManager )
{
schService = CreateService(
schSCManager, // SCManager database
"FTMSPullService", // name of service
"FTMS Pull Service", // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
strDir, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
"", // dependencies
pszDomainUsername, // LocalSystem account
pszPassword); // no password

if ( schService )
{
_tprintf(_T("%s installed.\n"), "FTMS Pull Service" );
DebugMsg("FTMS Pull Service Installed");
CloseServiceHandle(schService);
}
else
{
DebugMsg("CreateService Failed");
_tprintf(_T("CreateService failed - %s\n"), GetLastError());
}

CloseServiceHandle(schSCManager);
}
else
{
DebugMsg("OpenSCManager failed");
_tprintf(_T("OpenSCManager failed - %s\n"), GetLastError());
}

}





void CmdRemoveService()
{
SC_HANDLE schService;
SC_HANDLE schSCManager;

schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if ( schSCManager )
{
schService = OpenService(schSCManager, "FTMSPullService", SERVICE_ALL_ACCESS);

if (schService)
{
// try to stop the service first before removing it
if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
{
DebugMsg("Stopping FTMS Pull Service");
_tprintf(_T("Stopping %s."), "FTMS Pull Service");
Sleep( 1000 );

while( QueryServiceStatus( schService, &ssStatus ) )
{
if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
{
_tprintf(_T("."));
Sleep( 1000 );
}
else
break;
}

if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
{
DebugMsg("FTMS Pull Service Stopped");
_tprintf(_T("\n%s stopped.\n"), "FTMS Pull Service" );
}
else
{
DebugMsg("FTMS Pull Service failed to stop");
_tprintf(_T("\n%s failed to stop.\n"), "FTMS Pull Service" );
}

}

// now remove the service
if( DeleteService(schService) )
{
DebugMsg("FTMS Pull Service Removed");
_tprintf(_T("%s removed.\n"), "FTMS Pull Service" );
}
else
{
DebugMsg("FTMS Pull Service Failed to be removed");
_tprintf(_T("CmdRemove failed - %s\n"), GetLastError());
}


CloseServiceHandle(schService);
}
else
{
DebugMsg("OpenService Failed");
_tprintf(_T("OpenService failed - %s\n"), GetLastError());
}

CloseServiceHandle(schSCManager);
}
else
{
DebugMsg("OpenSCManager Failed");
_tprintf(_T("OpenSCManager failed - %s\n"), GetLastError());
}

}

void AddEventSource()
{
HKEY hKey = NULL;
HKEY hSubKey = NULL;

TCHAR szFilename[_MAX_PATH];
DWORD dwBuf;

if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, _T("System\\CurrentControlSet\\Services\\EventLog\\Application"), &hKey))
{

dwBuf = 8;
if (ERROR_SUCCESS == RegCreateKey(hKey, "FTMSPullService", &hSubKey))
{
RegSetValueEx( hSubKey,
_T("CategoryCount"),
0,
REG_DWORD,
(LPBYTE)&dwBuf,
sizeof(dwBuf));

dwBuf = 7;
RegSetValueEx( hSubKey,
_T("TypesSupported"),
0,
REG_DWORD,
(LPBYTE)&dwBuf,
sizeof(dwBuf));

GetModuleFileName(NULL, szFilename, sizeof(szFilename));

RegSetValueEx( hSubKey,
_T("CategoryMessageFile"),
0,
REG_SZ,
(LPBYTE)szFilename,
_tcslen(szFilename) * sizeof(TCHAR));


RegSetValueEx( hSubKey,
_T("EventMessageFile"),
0,
REG_SZ,
(LPBYTE)szFilename,
_tcslen(szFilename) * sizeof(TCHAR));
}
}

if (hKey)
{
RegCloseKey(hKey);
hKey = NULL;
}

if (hSubKey)
{
RegCloseKey(hSubKey);
hSubKey = NULL;
}
}



//
void RemoveEventSource()
{
HKEY hKey = NULL;

if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, _T("System\\CurrentControlSet\\Services\\EventLog\\Application"), &hKey))
{
RegDeleteKey(hKey, "FTMSPullService");
}

if (hKey)
{
RegCloseKey(hKey);
hKey = NULL;
}
}


void ExecPullApp()
{
char cmdLine[30];
strcpy(cmdLine, "C:\\FTMS\\Software\\EDG4FTP-Pull.exe");

STARTUPINFO si = { sizeof si };
PROCESS_INFORMATION pi = { 0 };

DebugMsg("Running command: \"%s\"", cmdLine );

CreateProcess(NULL,
(LPSTR)(LPCSTR) cmdLine,
NULL,
NULL,
TRUE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&si,
&pi);

::WaitForSingleObject( pi.hProcess, INFINITE );

DWORD ExitCode = 0;

::GetExitCodeProcess(pi.hProcess, &ExitCode);

if(ExitCode != 0)
{
DebugMsg("Unable to get Exit Code");
}
else
{
DebugMsg("Got Exit Code");
}

if(pi.hProcess != NULL)
{
DebugMsg("Closing Process");
::CloseHandle(pi.hProcess);
}

if(pi.hThread != NULL)
{
DebugMsg("Closing Thread");
::CloseHandle(pi.hThread);
}




}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top