www.pudn.com > MailSoftware.rar > Myservice.cpp


// myservice.cpp 
 
#include "NTServApp.h" 
#include "myservice.h" 
 
 
CMyService::CMyService() 
:CNTService("InetServ Windows NT Service") 
{ 
	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 szPath[1024]; 
	::GetModuleFileName(NULL, szPath, sizeof(szPath)); 
 
	for (int i = strlen(szPath);i>=0;i--) 
	{ 
	 if (szPath[i] == '\\')  
	 { 
		 szPath[i] = '\0'; 
		 break; 
	 } 
 
	} 
 
 
 
 
 
	  tdoPID  =0; 
 
 	  PROCESS_INFORMATION pi = {0}; 
      STARTUPINFO si = {0}; 
	  char tempstr[255]; 
	  sprintf(tempstr,"inetserv.exe"); 
      si.cb = sizeof(STARTUPINFO); 
      si.lpReserved = NULL; 
      si.lpReserved2 = NULL; 
      si.cbReserved2 = 0; 
      si.lpDesktop = NULL; 
      si.dwFlags = 0; 
 	   
 
 
 
 
      if (CreateProcess( NULL,tempstr,NULL,NULL,TRUE, 
		  NORMAL_PRIORITY_CLASS,NULL,szPath,&si,&pi )  != 0) 
	  { 
		tdoPID= pi.dwProcessId; 
	  } 
	  else 
	  { 
		m_bIsRunning = 0; 
	  } 
    
      
    
 
while (m_bIsRunning)  
	{ 
  
        Sleep(1000); 
 
			if (GetProcessVersion(tdoPID) == 0)  
			{ 
			Run(); 
			} 
 
		m_iState += m_iIncParam; 
    } 
} 
 
// 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() 
{ 
  
} 
 
void CMyService::OnStop() 
{ 
HANDLE hPr = OpenProcess(PROCESS_TERMINATE, TRUE,tdoPID ); 
TerminateProcess(hPr, 0); 
CloseHandle(hPr); 
}