www.pudn.com > SMS PLAN.rar > myservice.cpp


// myservice.cpp 
 
#include "NTServApp.h" 
#include "myservice.h" 
 
CMyService::CMyService() 
:CNTService(szSubServiceName) 
{ 
	m_iStartParam = 0; 
	m_iIncParam = 1; 
	m_iState = m_iStartParam; 
} 
 
BOOL CMyService::OnInit() 
{ 
	// Read the registry parameters 
    // Try opening the registry key: 
    // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\\Parameters 
    HKEY hkey; 
	char szKey[1024]; 
	strcpy(szKey, "SYSTEM\\CurrentControlSet\\Services\\"); 
	strcat(szKey, m_szServiceName); 
	strcat(szKey, "\\Parameters"); 
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
                     szKey, 
                     0, 
                     KEY_QUERY_VALUE, 
                     &hkey) == ERROR_SUCCESS) { 
        // Yes we are installed 
        DWORD dwType = 0; 
        DWORD dwSize = sizeof(m_iStartParam); 
        RegQueryValueEx(hkey, 
                        "Start", 
                        NULL, 
                        &dwType, 
                        (BYTE*)&m_iStartParam, 
                        &dwSize); 
        dwSize = sizeof(m_iIncParam); 
        RegQueryValueEx(hkey, 
                        "Inc", 
                        NULL, 
                        &dwType, 
                        (BYTE*)&m_iIncParam, 
                        &dwSize); 
        RegCloseKey(hkey); 
    } 
 
	// Set the initial state 
	m_iState = m_iStartParam; 
 
	return TRUE; 
} 
 
void CMyService::Run() 
{ 
	char tchar[_MAX_PATH]; 
	HINSTANCE hinstLib; 
	DWORD tp; 
	HANDLE h[2]; 
    while (m_bIsRunning)  
	{ 
		GetModuleFileName(NULL,tchar,MAX_PATH); 
	sprintf(strrchr(tchar,'\\')+1,"gw.dll\0"); 
	if((hinstLib = LoadLibrary(tchar))==NULL) 
	{ 
		FreeLibrary(hinstLib); 
		AddLog("Can not load the gw.dll.\n"); 
		SleepEx(1000,1); 
        goto cleanup; 
	}  
 
	if((StopGW=(void (_stdcall *)())GetProcAddress(hinstLib, 
		"StopGW"))==NULL) 
	{  
		AddLog(TEXT("Load DLL FUNCTION StopGW Error!!\n"));  
        goto cleanup; 
	}  
	if((BeginGW=(DWORD (_stdcall *)(LPVOID))GetProcAddress(hinstLib, 
		"BeginGW"))==NULL) 
	{  
		AddLog(TEXT("Load DLL FUNCTION BeginGW Error!!\n"));  
        goto cleanup; 
	}  
    // create the event object. The control handler function signals 
    // this event when it receives the "stop" control code. 
    // 
    hServerStopEvent = CreateEvent( 
        NULL,    // no security attributes 
        TRUE,    // manual reset event 
        FALSE,   // not-signalled 
        szSubServiceName);   // no name 
 
    if ( hServerStopEvent == NULL) 
        goto cleanup; 
	h[0]=hServerStopEvent; 
 
	h[1]=CreateThread(NULL,0,BeginGW,NULL,0,0); 
    // Service is now running, perform work until shutdown 
    // 
	AddLog("服务程序%s开始运行.\n",szSubServiceName); 
    while ( 1 ) 
    { 
		switch(WaitForSingleObject(h[0],5000)) 
		{ 
			case WAIT_FAILED: 
			case WAIT_TIMEOUT: 
				GetExitCodeThread(h[1],&tp); 
				if(tp!=STILL_ACTIVE) 
				{ 
					CloseHandle(h[1]); 
					AddLog("服务线程退出,重新启动服务线程.\n"); 
					h[1]=CreateThread(NULL,0,BeginGW,NULL,0,0); 
					SleepEx(5000,1); 
				} 
				break; 
			case WAIT_OBJECT_0: 
				AddLog("得到消息,准备退出服务程序%s.\n",szSubServiceName); 
				StopGW(); 
				SleepEx(100,1); 
				FreeLibrary(hinstLib); 
				goto cleanup; 
			case WAIT_OBJECT_0+1: 
				CloseHandle(h[1]); 
					AddLog("服务线程退出,重新启动服务线程.\n"); 
				h[1]=CreateThread(NULL,0,BeginGW,NULL,0,0); 
				break; 
		} 
    } 
 
cleanup: 
	AddLog("退出服务程序%s.\n",szSubServiceName); 
    if (hServerStopEvent) 
        CloseHandle(hServerStopEvent); 
 
		// Update the current state 
		m_iState += m_iIncParam; 
    } 
	SetStatus(SERVICE_STOPPED); 
} 
 
// Process user control requests 
BOOL CMyService::OnUserControl(DWORD dwOpcode) 
{ 
    switch (dwOpcode) { 
    case SERVICE_CONTROL_USER + 0: 
 
        // Save the current status in the registry 
        SaveStatus(); 
        return TRUE; 
 
    default: 
        break; 
    } 
    return FALSE; // say not handled 
} 
 
// Save the current status in the registry 
void CMyService::SaveStatus() 
{ 
    DebugMsg("Saving current status"); 
    // Try opening the registry key: 
    // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\\... 
    HKEY hkey = NULL; 
	char szKey[1024]; 
	strcpy(szKey, "SYSTEM\\CurrentControlSet\\Services\\"); 
	strcat(szKey, m_szServiceName); 
	strcat(szKey, "\\Status"); 
    DWORD dwDisp; 
	DWORD dwErr; 
    DebugMsg("Creating key: %s", szKey); 
    dwErr = RegCreateKeyEx(	HKEY_LOCAL_MACHINE, 
                           	szKey, 
                   			0, 
                   			"", 
                   			REG_OPTION_NON_VOLATILE, 
                   			KEY_WRITE, 
                   			NULL, 
                   			&hkey, 
                   			&dwDisp); 
	if (dwErr != ERROR_SUCCESS) { 
		DebugMsg("Failed to create Status key (%lu)", dwErr); 
		return; 
	}	 
 
    // Set the registry values 
	DebugMsg("Saving 'Current' as %ld", m_iState);  
    RegSetValueEx(hkey, 
                  "Current", 
                  0, 
                  REG_DWORD, 
                  (BYTE*)&m_iState, 
                  sizeof(m_iState)); 
 
 
    // Finished with key 
    RegCloseKey(hkey); 
 
}