BOOL CService::Init() { BOOL bRet = FALSE; __try { m_hModule = LoadLibrary(_T("Kernel32.dll")); if (!m_hModule) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "LoadLibrary failed. (%d)", GetLastError()); __leave; } m_pfWow64DisableWow64FsRedirection = (WOW64_DISABLE_WOW64_FS_REDIRECTION)GetProcAddress(m_hModule, "Wow64DisableWow64FsRedirection"); m_pfWow64RevertWow64FsRedirection = (WOW64_REVERT_WOW64_FS_REDIRECTION)GetProcAddress(m_hModule, "Wow64RevertWow64FsRedirection"); bRet = TRUE; } __finally { if (!bRet) { if (!Unload()) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "Unload failed"); } } return bRet; }
BOOL Test() { BOOL bRet = TRUE; __try { if (!InitMod(NULL)) { printfEx(MOD_MAIN, PRINTF_LEVEL_ERROR, "InitMod failed"); bRet = FALSE; } if (!UnloadMod()) { printfEx(MOD_MAIN, PRINTF_LEVEL_ERROR, "UnloadMod failed"); bRet = FALSE; } } __finally { CPrintfEx::ReleaseInstance(); } return bRet; }
BOOL CService::Delete( __in LPWSTR lpServiceName ) { BOOL bRet = FALSE; SC_HANDLE hScManager = NULL; SC_HANDLE hService = NULL; __try { if (!DeleteFileInDrivers(lpServiceName)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "DeleteFileInDrivers failed. %S", lpServiceName); __leave; } hScManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hScManager) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OpenSCManager failed. %S (%d)", lpServiceName, GetLastError()); __leave; } hService = OpenService(hScManager, lpServiceName, SERVICE_ALL_ACCESS); if (!hService) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OpenService failed. %S (%d)", lpServiceName, GetLastError()); __leave; } if (!DeleteService(hService)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "DeleteService failed. %S (%d)", lpServiceName, GetLastError()); __leave; } bRet = TRUE; } __finally { if (hService) { CloseServiceHandle(hService); hService = NULL; } if (hScManager) { CloseServiceHandle(hScManager); hScManager = NULL; } } return bRet; }
BOOL CService::Stop( __in LPWSTR lpServiceName ) { BOOL bRet = FALSE; SC_HANDLE hScManager = NULL; SC_HANDLE hService = NULL; SERVICE_STATUS ServiceStatus = {0}; __try { hScManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hScManager) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OpenSCManager failed. %S (%d)", lpServiceName, GetLastError()); __leave; } hService = OpenService(hScManager, lpServiceName, SERVICE_ALL_ACCESS); if (!hService) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OpenService failed. %S (%d)", lpServiceName, GetLastError()); __leave; } if (!ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "ControlService failed. %S (%d)", lpServiceName, GetLastError()); __leave; } bRet = TRUE; } __finally { if (hService) { CloseServiceHandle(hService); hService = NULL; } if (hScManager) { CloseServiceHandle(hScManager); hScManager = NULL; } } return bRet; }
//Fonction appelée en cas de nouvelle partie void jeu_init(Tptpartie partie, Tptjoueur joueur1, Tptjoueur joueur2) { Tptjoueur joueur_selectionne; Tptjoueur autre_joueur; if(!joueur1 -> est_ordinateur) menu_demander_nom_joueur(joueur1 -> nom, 1); if(!joueur2 -> est_ordinateur) menu_demander_nom_joueur(joueur2 -> nom, 2); cartes_deck_init(partie -> deck); cartes_distribuer(partie -> deck, joueur1 -> deck, CARTES_MAIN); cartes_distribuer(partie -> deck, joueur2 -> deck, CARTES_MAIN); printf("\nLe jeu a été mélangé et les cartes ont été distribuées. "); //On choisit aléatoirement lequel des deux joueurs va commencer. joueur_selectionne = qui_commence(joueur1, joueur2); if(joueur_selectionne == joueur1) autre_joueur = joueur2; else autre_joueur = joueur1; partie -> joueur_selectionne = joueur_selectionne; partie -> autre_joueur = autre_joueur; printf("Le tirage au sort a décidé que "); printfEx(COULEUR_PARADES, "'%s' commence la partie", partie -> joueur_selectionne -> nom); printf(".\n"); demander_appuyez_sur_une_touche_pour_continuer(); }
// // Purpose: // Entry point for the service // // Parameters: // dwArgc - Number of arguments in the lpszArgv array // lpszArgv - Array of strings. The first string is the name of // the service and subsequent strings are passed by the process // that called the StartService function to start the service. // // Return value: // None. // VOID WINAPI CService::Main( DWORD dwArgc, LPTSTR * lpszArgv ) { HANDLE hDataMgr = NULL; HANDLE hRpcServer = NULL; CHAR DbPath[MAX_PATH] = {0}; printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "begin"); __try { // Register the handler function for the service CService::GetInstance()->m_SvcStatusHandle = RegisterServiceCtrlHandlerEx(CService::GetInstance()->m_tchServiceName, CtrlHandler, NULL); if (!CService::GetInstance()->m_SvcStatusHandle ) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegisterServiceCtrlHandler failed. (%d)", GetLastError()); __leave; } // These SERVICE_STATUS members remain as set here CService::GetInstance()->m_SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; CService::GetInstance()->m_SvcStatus.dwServiceSpecificExitCode = 0; // Report initial status to the SCM CService::GetInstance()->ReportSvcStatus(SERVICE_START_PENDING, NO_ERROR, 3000); // Perform service-specific initialization and work. if (!CService::GetInstance()->Init(dwArgc, lpszArgv)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "Init failed"); __leave; } } __finally { ; } printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "end"); return ; }
BOOL CService::Register( __in LPTSTR lpServiceName, __in INITMOD InitMod, __in_opt LPINIT_MOD_ARGUMENTS lpInitModArguments, __in UNLOADMOD UnloadMod ) { BOOL bRet = FALSE; SERVICE_TABLE_ENTRY ServiceTableEntry[] = { {lpServiceName, (LPSERVICE_MAIN_FUNCTION)Main}, {NULL, NULL} }; printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "begin"); __try { if (!lpServiceName || !InitMod || !UnloadMod) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "input arguments error. 0x%p 0x%p 0x%p",lpServiceName, InitMod, UnloadMod); __leave; } m_pfInitMod = InitMod; m_pfUnloadMod = UnloadMod; if (lpInitModArguments) { if (_tcslen(lpInitModArguments->tchModuleName)) _tcscat_s(m_InitModArguments.tchModuleName, _countof(m_InitModArguments.tchModuleName), lpInitModArguments->tchModuleName); m_InitModArguments.hWindow = lpInitModArguments->hWindow; m_InitModArguments.lpfnWndProc = lpInitModArguments->lpfnWndProc; m_InitModArguments.bCreateMassageLoop = lpInitModArguments->bCreateMassageLoop; } _tcscat_s(m_tchServiceName, _countof(m_tchServiceName), lpServiceName); printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "StartServiceCtrlDispatcher begin"); if (!StartServiceCtrlDispatcher(ServiceTableEntry)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "StartServiceCtrlDispatcher failed. (%d)", GetLastError()); __leave; } printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "StartServiceCtrlDispatcher end"); bRet = TRUE; } __finally { ; } printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "end"); return bRet; }
// // Purpose: // The service code // // Parameters: // dwArgc - Number of arguments in the lpszArgv array // lpszArgv - Array of strings. The first string is the name of // the service and subsequent strings are passed by the process // that called the StartService function to start the service. // // Return value: // None // BOOL CService::Init( DWORD dwArgc, LPTSTR * lpszArgv ) { BOOL bRet = FALSE; printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "begin"); __try { // TO_DO: Declare and set any required variables. // Be sure to periodically call ReportSvcStatus() with // SERVICE_START_PENDING. If initialization fails, call // ReportSvcStatus with SERVICE_STOPPED. // Create an event. The control handler function, SvcCtrlHandler, // signals this event when it receives the stop control code. m_hSvcStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (!m_hSvcStopEvent) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "CreateEvent failed. (%d)", GetLastError()); ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0); __leave; } // Report running status when initialization is complete. ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0); // TO_DO: Perform work until service stops. if (!m_pfInitMod(&m_InitModArguments)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "ms_Test failed"); __leave; } // Check whether to stop the service. printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "开始等待"); WaitForSingleObject(m_hSvcStopEvent, INFINITE); printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "等待成功"); ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0); bRet = TRUE; } __finally { ; } printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "end"); return bRet; }
int _tmain(int argc, _TCHAR* argv[]) { OS_VERSION_USER_DEFINED OsVerAndProcType = OS_VERSION_UNKNOWN; __try { printfEx(MOD_MAIN, PRINTF_LEVEL_INFORMATION, "日志模块初始化完毕,按任意键继续"); _getch(); if (!CService::GetInstance()->Install( _T("test"), _T("testDisplayName"), _T("testDescription"), SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, _T("G:\\GitHub\\Test\\Debug\\Test.exe"), NULL, NULL, TRUE )) { printfEx(MOD_MAIN, PRINTF_LEVEL_ERROR, "Service.Install failed"); __leave; } } __finally { CService::ReleaseInstance(); printfEx(MOD_MAIN, PRINTF_LEVEL_INFORMATION, "按任意键退出"); CPrintfEx::ReleaseInstance(); _getch(); } return 0; }
CService::~CService() { if (!Unload()) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "Unload failed"); ZeroMemory(m_tchServiceName, sizeof(m_tchServiceName)); m_SvcStatusHandle = NULL; ZeroMemory(&m_SvcStatus, sizeof(m_SvcStatus)); m_hSvcStopEvent = NULL; m_dwCheckPoint = 1; m_pfInitMod = NULL; m_pfUnloadMod = NULL; ZeroMemory(&m_InitModArguments, sizeof(m_InitModArguments)); }
BOOL InitMod( __in_opt LPINIT_MOD_ARGUMENTS lpInitModArguments ) { BOOL bRet = FALSE; CRUSH_HANDLER_INFO CrushHandlerInfo; TCHAR tchLogPath[MAX_PATH] = {0}; LPTSTR lpPosition = NULL; __try { ZeroMemory(&CrushHandlerInfo, sizeof(CrushHandlerInfo)); CrushHandlerInfo.EhType = EH_TYPE_S; CrushHandlerInfo.bFirstHandler = TRUE; CrushHandlerInfo.MiniDumpType = MiniDumpWithFullMemory; CSimpleDump::GetInstance()->RegisterCrushHandler(&CrushHandlerInfo); if (GetModuleFileName(NULL, tchLogPath, _countof(tchLogPath))) { lpPosition = _tcsrchr(tchLogPath, _T('.')); if (lpPosition) { *(lpPosition + 1) = _T('\0'); _tcscat_s(tchLogPath, _countof(tchLogPath), _T("log")); if (!CSimpleLog::GetInstance(tchLogPath)) printfEx(MOD_MAIN, PRINTF_LEVEL_ERROR, "SimpleLog.Init failed"); } } bRet = TRUE; } __finally { ; } return bRet; }
//Cette fonction est la fonction globale qui gère une partie (reprise en cours ou nouvellement commencée). //Grâce aux paramètres joueur_selectionne et autre_joueur, on sait à qui c'est de jouer. void jeu(Tptpartie partie) { int partie_est_terminee = partie_terminee(partie); int choix_carte = -1, choix_jeter = -1; int resultat_jouer = -1, resultat_jouer_passer_tour = -1; char nomFichier[TAILLE_MAX_NOM_FICHIER]; char raison_refus[TAILLE_MAX_REFUS] = ""; char raison_refus2[TAILLE_MAX_REFUS] = ""; Tptjoueur gagnant = NULL; Tptcarte carte_piochee = NULL; Tptdeck les_coups_possibles = NULL; while(!partie_est_terminee && choix_carte != ARRETER_PARTIE && choix_carte != ENREGISTRER) { choix_carte = -1; choix_jeter = -1; printf("\nIl reste %d cartes dans le deck principal.\n", partie -> deck -> taille); printf("\nC'est à '%s' de jouer.\n", partie -> joueur_selectionne -> nom); if(partie -> deck -> taille > 0) { //On fait piocher une carte au joueur carte_piochee = cartes_changer_deck(partie -> deck, partie -> deck -> premier, partie -> joueur_selectionne -> deck); //On affiche la carte que le joueur vient de piocher que si ça n'est pas un ordinateur if(!partie -> joueur_selectionne -> est_ordinateur) { printf("'%s' vient de piocher une carte '", partie -> joueur_selectionne -> nom); cartes_type2francais(carte_piochee -> valeur); //On affiche le nom de la carte en français printf("'.\n"); demander_appuyez_sur_une_touche_pour_continuer(); } else { printf("'%s' vient de piocher une carte.\n", partie -> joueur_selectionne -> nom); } } //On n'affiche les infos que si le joueur n'est pas un ordinateur. if(!partie -> joueur_selectionne -> est_ordinateur) { printf("\nÉTAT DE VOTRE ADVERSAIRE :"); joueur_afficher_infos_utiles(partie -> autre_joueur); demander_appuyez_sur_une_touche_pour_continuer(); printf("\nVOTRE ÉTAT :"); joueur_afficher_infos_utiles(partie -> joueur_selectionne); demander_appuyez_sur_une_touche_pour_continuer(); //Affiche la main du joueur printf("\nVOTRE MAIN :\n"); printf("Vous avez %d cartes en main : \n", partie -> joueur_selectionne -> deck -> taille); cartes_deck_afficher(partie -> joueur_selectionne -> deck); printf("\n"); demander_appuyez_sur_une_touche_pour_continuer(); les_coups_possibles = lister_coups_possibles(partie -> joueur_selectionne, partie -> autre_joueur); printf("\nIl y a %d COUPS POSSIBLES :\n", les_coups_possibles -> taille); cartes_deck_afficher(les_coups_possibles); printf("\n"); } //Demande au joueur de choisir une carte à jouer (si c'est un humain seulement) if(!partie -> joueur_selectionne -> est_ordinateur) { resultat_jouer = -1; while(resultat_jouer != 1) { menu_demander_choix_carte(&choix_carte, les_coups_possibles -> taille); if(choix_carte != ARRETER_PARTIE && choix_carte != ENREGISTRER) { if(choix_carte != PASSER_SON_TOUR) { //Appel de la fonction jouer resultat_jouer = jouer(partie, choix_carte, 0, raison_refus, raison_refus2); switch(resultat_jouer) { case ERREUR_COUP_NON_PERMIS: printf("\nCe coup n'est pas permis ! Voici une explication : \n\n"); if(strlen(raison_refus) > 0) { printf("%s", raison_refus); if(strlen(raison_refus2) > 0) printf("%s", raison_refus2); } memset (raison_refus, 0, sizeof (raison_refus)); memset (raison_refus2, 0, sizeof (raison_refus2)); printf("\n\nVeuillez choisir une autre carte.\n\n"); break; case ERREUR_CARTE_PAS_DANS_MAIN: printf("\nErreur, carte invalide. Soit ce type de carte n'existe pas, soit vous n'avez pas de carte de ce type dans votre main.\n"); printf("Merci de choisir une carte valide.\n\n"); break; } } else { resultat_jouer_passer_tour = -1; while(resultat_jouer_passer_tour != 1) { menu_demander_choix_carte_jeter(&choix_jeter); if(choix_jeter != ANNULER_PASSER_SON_TOUR) { resultat_jouer_passer_tour = jouer(partie, choix_jeter, 1, raison_refus, raison_refus2); if(resultat_jouer_passer_tour == ERREUR_CARTE_PAS_DANS_MAIN) { printf("\nErreur, carte invalide. Soit ce type de carte n'existe pas, soit vous n'avez pas de carte de ce type dans votre main.\n"); printf("Merci de choisir une carte valide.\n\n"); } } else resultat_jouer_passer_tour = 1; } if(choix_jeter != ANNULER_PASSER_SON_TOUR) { printf("'%s' passe son tour et jette une carte.\n", partie -> joueur_selectionne -> nom); resultat_jouer = 1; } else if(les_coups_possibles -> taille > 0) { choix_jeter = -1; printf("\nVous avez changé d'avis et ne souhaitez plus passer votre tour.\n"); printf("Vous allez être réinvité à sélectionner la carte que vous désirez jouer.\n\n"); } } } else resultat_jouer = 1; } } //C'est un ordinateur, on demande à l'IA de jouer else { switch(partie -> joueur_selectionne -> difficulte_ordinateur) { case ARCHI_DEBUTANT: ia_archi_debutant(partie -> joueur_selectionne, partie -> autre_joueur, &choix_carte, &choix_jeter); break; case DEBUTANT: ia_debutant(partie -> joueur_selectionne, partie -> autre_joueur, &choix_carte, &choix_jeter); break; case COURSE: ia_course(partie -> joueur_selectionne, partie -> autre_joueur, &choix_carte, &choix_jeter); break; case AGRESSIF: ia_agressif(partie -> joueur_selectionne, partie -> autre_joueur, &choix_carte, &choix_jeter); break; case DEFENSIF: ia_defensif(partie -> joueur_selectionne, partie -> autre_joueur, &choix_carte, &choix_jeter); break; case EXPERT: ia_expert(partie -> joueur_selectionne, partie -> autre_joueur, &choix_carte, &choix_jeter); break; default: printf("Erreur, la difficulté de l'IA est invalide (jeu.c)."); exit(0); } if(DEBUG_IA) { printf("Voici sa main : \n"); cartes_deck_afficher(partie -> joueur_selectionne -> deck); } /* Le choix de la carte a été fait par l'ordinateur. On joue sa carte. */ if(choix_carte == PASSER_SON_TOUR) { resultat_jouer = jouer(partie, choix_jeter, 1, raison_refus, raison_refus2); printf("\n'%s' passe son tour et jette une carte.\n", partie -> joueur_selectionne -> nom); if(DEBUG_IA) { printf("La carte qu'il vient de JETER est : '"); cartes_type2francais(choix_jeter); printf("'.\n"); } demander_appuyez_sur_une_touche_pour_continuer(); } else resultat_jouer = jouer(partie, choix_carte, 0, raison_refus, raison_refus2); if(resultat_jouer != 1) { printf("Erreur : l'ordinateur a choisi une carte qui n'est pas valide.\n"); printf("Code de l'erreur : %d. ", resultat_jouer); printf("L'ordinateur avait essayé de jouer : '"); cartes_type2francais(choix_carte); printf(" %d", choix_carte); printf("'.\n"); exit(0); } } if( choix_carte != ENREGISTRER && choix_carte != ARRETER_PARTIE && choix_jeter != ANNULER_PASSER_SON_TOUR && resultat_jouer == 1) { if(choix_carte != PASSER_SON_TOUR) { printf("\n'%s' vient de jouer une carte '", partie -> joueur_selectionne -> nom); cartes_type2francais(choix_carte); printf("'.\n"); demander_appuyez_sur_une_touche_pour_continuer(); } //« Y a-t-il coup fourré ? » if( choix_carte == PANNE_ESSENCE && partie -> autre_joueur -> en_panne_dessence && joueur_possede_carte(partie -> autre_joueur, CITERNE) != NULL) { menu_demander_coup_fourre(partie, PANNE_ESSENCE, CITERNE, raison_refus, raison_refus2); } else if(choix_carte == CREVE && partie -> autre_joueur -> est_creve && joueur_possede_carte(partie -> autre_joueur, INCREVABLE) != NULL) { menu_demander_coup_fourre(partie, CREVE, INCREVABLE, raison_refus, raison_refus2); } else if(choix_carte == ACCIDENT && partie -> autre_joueur -> a_accident && joueur_possede_carte(partie -> autre_joueur, AS_DU_VOLANT) != NULL) { menu_demander_coup_fourre(partie, ACCIDENT, AS_DU_VOLANT, raison_refus, raison_refus2); } else if(choix_carte == LIMITE_VITESSE && partie -> autre_joueur -> est_limite_par_vitesse && joueur_possede_carte(partie -> autre_joueur, PRIORITAIRE) != NULL) { menu_demander_coup_fourre(partie, LIMITE_VITESSE, PRIORITAIRE, raison_refus, raison_refus2); } else if(choix_carte == STOP && partie -> autre_joueur -> est_arrete && joueur_possede_carte(partie -> autre_joueur, PRIORITAIRE) != NULL) { menu_demander_coup_fourre(partie, STOP, PRIORITAIRE, raison_refus, raison_refus2); } /* Si le joueur vient de jouter une carte 'botte', il a le droit de rejouer Il ne faut donc pas switcher les joueurs dans ce cas */ if(choix_carte >= CITERNE && choix_carte <= PRIORITAIRE) { printf("\n"); printfEx(COULEUR_BOTTES, "'%s' vient de jouer une carte 'botte', c'est donc encore une fois à lui de jouer !\n", partie -> joueur_selectionne -> nom); } else switch_tour(partie); partie_est_terminee = partie_terminee(partie); } } //On souhaite enregistrer la partie pour la reprendre plus tard if(choix_carte == ENREGISTRER) { //Le joueur vient de piocher une carte et en a sept. On annule sa pioche et on replace la carte dans le dessus du deck. //Lorsqu'il reprendra la partie plus tard, il repiochera cette carte puisqu'elle sera restée au début du deck. :) cartes_changer_deck(partie -> joueur_selectionne -> deck, carte_piochee, partie -> deck); menu_enregistrer_partie(nomFichier); if(enregistrer_partie(nomFichier, partie)) printf("La partie a été sauvegardée avec succès !\n"); else printf("Une erreur est survenue lors de l'enregistrement de la partie.\n"); } else if(choix_carte == ARRETER_PARTIE) { printf("On arrête la partie !\n"); } //La partie est réellement terminée else if(partie_est_terminee) { printf("\n\nPARTIE TERMINÉE !\n\n"); gagnant = detecter_gagnant(partie -> joueur_selectionne, partie -> autre_joueur); if(gagnant != NULL) printf("'%s' gagne la partie !", gagnant -> nom); else printf("Match nul, vous avez fait le même nombre de points !\n"); } //Nettoyage de fin de partie. partie_vider(partie); }
BOOL CService::Enable( __in LPWSTR lpServiceName ) { BOOL bRet = FALSE; SC_HANDLE hScManager = NULL; SC_HANDLE hService = NULL; __try { hScManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hScManager) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OpenSCManager failed. (%d)", GetLastError()); __leave; } hService = OpenService(hScManager, lpServiceName, SERVICE_ALL_ACCESS); if (!hService) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OpenService failed. (%d)", GetLastError()); __leave; } if (!ChangeServiceConfig( hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL )) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "ChangeServiceConfig failed. (%d)", GetLastError()); __leave; } bRet = TRUE; } __finally { if (hService) { CloseServiceHandle(hService); hService = NULL; } if (hScManager) { CloseServiceHandle(hScManager); hScManager = NULL; } } return bRet; }
// // Purpose: // Sets the current service status and reports it to the SCM. // // Parameters: // dwCurrentState - The current state (see SERVICE_STATUS) // dwWin32ExitCode - The system error code // dwWaitHint - Estimated time for pending operation, // in milliseconds // // Return value: // None // VOID CService::ReportSvcStatus( DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint ) { OS_VERSION_USER_DEFINED OsVerAndProcType = OS_VERSION_UNKNOWN; __try { // Fill in the SERVICE_STATUS structure. m_SvcStatus.dwCurrentState = dwCurrentState; m_SvcStatus.dwWin32ExitCode = dwWin32ExitCode; m_SvcStatus.dwWaitHint = dwWaitHint; if (SERVICE_START_PENDING == dwCurrentState) m_SvcStatus.dwControlsAccepted = 0; else { OsVerAndProcType = COperationSystemVersion::GetInstance()->GetOSVersion(); switch (OsVerAndProcType) { case OS_VERSION_WINDOWS_XP: case OS_VERSION_WINDOWS_XP_SP1: case OS_VERSION_WINDOWS_XP_SP2: case OS_VERSION_WINDOWS_XP_SP3: { if (OS_PROCESSOR_TYPE_X86 != COperationSystemVersion::GetInstance()->GetOSProcessorType()) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "GetOSProcessorType error. %d", OsVerAndProcType); __leave; } m_SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | // 0x00000001 SERVICE_ACCEPT_PAUSE_CONTINUE | // 0x00000002 SERVICE_ACCEPT_SHUTDOWN | // 0x00000004 SERVICE_ACCEPT_PARAMCHANGE | // 0x00000008 SERVICE_ACCEPT_NETBINDCHANGE | // 0x00000010 SERVICE_ACCEPT_HARDWAREPROFILECHANGE | // 0x00000020 SERVICE_ACCEPT_POWEREVENT | // 0x00000040 SERVICE_ACCEPT_SESSIONCHANGE; // 0x00000080 break; } case OS_VERSION_WINDOWS_7: case OS_VERSION_WINDOWS_7_SP1: case OS_VERSION_WINDOWS_8: case OS_VERSION_WINDOWS_8_POINT1: case OS_VERSION_WINDOWS_10: { m_SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | // 0x00000001 SERVICE_ACCEPT_PAUSE_CONTINUE | // 0x00000002 SERVICE_ACCEPT_SHUTDOWN | // 0x00000004 SERVICE_ACCEPT_PARAMCHANGE | // 0x00000008 SERVICE_ACCEPT_NETBINDCHANGE | // 0x00000010 SERVICE_ACCEPT_HARDWAREPROFILECHANGE | // 0x00000020 SERVICE_ACCEPT_POWEREVENT | // 0x00000040 SERVICE_ACCEPT_SESSIONCHANGE | // 0x00000080 SERVICE_ACCEPT_PRESHUTDOWN | // 0x00000100 SERVICE_ACCEPT_TIMECHANGE | // 0x00000200 SERVICE_ACCEPT_TRIGGEREVENT; // 0x00000400 break; } default: { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OsVerAndProcType error. %d", OsVerAndProcType); __leave; } } } if ((SERVICE_RUNNING == dwCurrentState) || (SERVICE_STOPPED == dwCurrentState)) m_SvcStatus.dwCheckPoint = 0; else m_SvcStatus.dwCheckPoint = m_dwCheckPoint++; // Report the status of the service to the SCM. if (!SetServiceStatus(m_SvcStatusHandle, &m_SvcStatus)) { if (6 != GetLastError()) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "SetServiceStatus failed. (%d)", GetLastError()); __leave; } } __finally { ; } return ; }
BOOL CService::Install( __in LPWSTR lpServiceName, __in_opt LPWSTR lpDisplayName, __in_opt LPWSTR lpDescription, __in DWORD dwServiceType, __in DWORD dwStartType, __in_opt DWORD dwErrorControl, __in LPWSTR lpPath, __in_opt LPWSTR lpLoadOrderGroup, __in_opt LPWSTR lpDependencies, __in_opt BOOL bInteractWithTheDesktop ) { BOOL bRet = FALSE; SC_HANDLE hScManager = NULL; SC_HANDLE hService = NULL; WCHAR wchTemp[MAX_PATH] = {0}; HKEY hkResult = NULL; DWORD dwData = 0; LONG lResult = 0; WCHAR wchPath[MAX_PATH] = {0}; LPWSTR lpPosition = NULL; PVOID pOldValue = NULL; BOOL bWow64DisableWow64FsRedirection = FALSE; OS_PROCESSOR_TYPE_USER_DEFINED OsProcType = OS_PROCESSOR_TYPE_UNKNOWN; WCHAR wchSubKey[MAX_PATH] = {0}; __try { if (!lpServiceName || !lpPath || !dwServiceType) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "input arguments error. 0x%p 0x%p %d", lpServiceName, lpPath, dwServiceType); __leave; } OsProcType = COperationSystemVersion::GetInstance()->GetOSProcessorType(); switch (OsProcType) { case OS_PROCESSOR_TYPE_X86: break; case OS_PROCESSOR_TYPE_X64: { if (!m_pfWow64DisableWow64FsRedirection(&pOldValue)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "Wow64DisableWow64FsRedirection failed. (%d)", GetLastError()); __leave; } bWow64DisableWow64FsRedirection = TRUE; break; } default: { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OsProcType error. %d", OsProcType); __leave; } } hScManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hScManager) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OpenSCManager failed. (%d)", GetLastError()); __leave; } switch (dwStartType) { case SERVICE_BOOT_START: case SERVICE_SYSTEM_START: { if (!(SERVICE_FILE_SYSTEM_DRIVER & dwServiceType || SERVICE_KERNEL_DRIVER & dwServiceType || SERVICE_RECOGNIZER_DRIVER & dwServiceType)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "dwServiceType and dwStartType not match. 0x%x 0x%x", dwServiceType, dwStartType); __leave; } if (!GetSystemDirectory(wchPath, _countof(wchPath))) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "GetSystemDirectory failed. (%d)", GetLastError()); __leave; } wcscat_s(wchPath, _countof(wchPath), L"\\drivers\\"); if (0 == _wcsnicmp(wchPath, lpPath, wcslen(wchPath))) wcscpy_s(wchPath, _countof(wchPath), lpPath); else { lpPosition = wcsrchr(lpPath, L'\\'); if (!lpPosition) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "lpPath error. %S", lpPath); __leave; } wcscat_s(wchPath, _countof(wchPath), lpPosition + 1); if (!CopyFile(lpPath, wchPath, TRUE)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "CopyFile failed. %S -> %S", lpPath, wchPath); __leave; } } break; } case SERVICE_AUTO_START: case SERVICE_DEMAND_START: case SERVICE_DISABLED: { wcscat_s(wchPath, _countof(wchPath), lpPath); break; } default: { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "dwStartType error. 0x%x", dwStartType); __leave; } } switch (dwErrorControl) { case SERVICE_ERROR_IGNORE: case SERVICE_ERROR_NORMAL: case SERVICE_ERROR_SEVERE: case SERVICE_ERROR_CRITICAL: break; default: { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "dwErrorControl error. 0x%x", dwErrorControl); __leave; } } if (SERVICE_WIN32_OWN_PROCESS == dwServiceType || SERVICE_WIN32_SHARE_PROCESS == dwServiceType) { if (bInteractWithTheDesktop) dwServiceType |= SERVICE_INTERACTIVE_PROCESS; } hService = CreateService( hScManager, lpServiceName, lpDisplayName ? lpDisplayName : lpServiceName, SERVICE_ALL_ACCESS, dwServiceType, dwStartType, dwErrorControl, wchPath, lpLoadOrderGroup, NULL, lpDependencies, NULL, NULL ); if (!hService) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "CreateService failed. (%d)", GetLastError()); __leave; } switch (dwServiceType) { case SERVICE_KERNEL_DRIVER: { /* dwData = sizeof(wchTemp); lResult = RegGetValue( HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e967-e325-11ce-bfc1-08002be10318}", L"UpperFilters", RRF_RT_REG_MULTI_SZ, NULL, wchTemp, &dwData ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegGetValue failed. (0x%x)", lResult); __leave; } lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e967-e325-11ce-bfc1-08002be10318}", 0, KEY_ALL_ACCESS, &hkResult ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegOpenKeyEx failed. (0x%x)", lResult); __leave; } if (!hkResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "hkResult error"); __leave; } wcscpy_s(wchTemp + dwData / sizeof(WCHAR) - 1, _countof(wchTemp) - dwData / sizeof(WCHAR) + 1, lpServiceName); *(wchTemp + dwData / sizeof(WCHAR) + wcslen(lpServiceName)) = L'\0'; lResult = RegSetValueEx( hkResult, L"UpperFilters", 0, REG_MULTI_SZ, (const BYTE*)wchTemp, dwData + (wcslen(lpServiceName) + 1) * sizeof(WCHAR) ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegSetValueEx failed. (0x%x)", lResult); __leave; } lResult = RegFlushKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegFlushKey failed. (0x%x)", lResult); __leave; } */ break; } case SERVICE_FILE_SYSTEM_DRIVER: { wcscat_s(wchTemp, _countof(wchTemp), L"SYSTEM\\CurrentControlSet\\services\\"); wcscat_s(wchTemp, _countof(wchTemp), lpServiceName); wcscat_s(wchTemp, _countof(wchTemp), L"\\Instances"); lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE, wchTemp, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkResult, NULL ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegCreateKeyEx failed. (0x%x)", lResult); __leave; } if (!hkResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "hkResult error"); __leave; } lResult = RegFlushKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegFlushKey failed. (0x%x)", lResult); __leave; } ZeroMemory(wchTemp, sizeof(wchTemp)); wcscat_s(wchTemp, _countof(wchTemp), lpServiceName); wcscat_s(wchTemp, _countof(wchTemp), L" Instance"); lResult = RegSetValueEx( hkResult, L"DefaultInstance", 0, REG_SZ, (const BYTE*)wchTemp, wcslen(wchTemp) * sizeof(WCHAR) ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegSetValueEx failed. (0x%x)", lResult); __leave; } lResult = RegFlushKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegFlushKey failed. (0x%x)", lResult); __leave; } lResult = RegCloseKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegCloseKey failed. (0x%x)", lResult); __leave; } hkResult = NULL; ZeroMemory(wchTemp, sizeof(wchTemp)); wcscat_s(wchTemp, _countof(wchTemp), L"SYSTEM\\CurrentControlSet\\services\\"); wcscat_s(wchTemp, _countof(wchTemp), lpServiceName); wcscat_s(wchTemp, _countof(wchTemp), L"\\Instances\\"); wcscat_s(wchTemp, _countof(wchTemp), lpServiceName); wcscat_s(wchTemp, _countof(wchTemp), L" Instance"); lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE, wchTemp, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkResult, NULL ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegCreateKeyEx failed. (0x%x)", lResult); __leave; } if (!hkResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "hkResult error"); __leave; } lResult = RegFlushKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegFlushKey failed. (0x%x)", lResult); __leave; } ZeroMemory(wchTemp, sizeof(wchTemp)); wcscat_s(wchTemp, _countof(wchTemp), L"370030"); lResult = RegSetValueEx( hkResult, L"Altitude", 0, REG_SZ, (const BYTE*)wchTemp, wcslen(wchTemp) * sizeof(WCHAR) ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegSetValueEx failed. (0x%x)", lResult); __leave; } lResult = RegFlushKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegFlushKey failed. (0x%x)", lResult); __leave; } dwData = 0x0; lResult = RegSetValueEx( hkResult, L"Flags", 0, REG_DWORD, (const BYTE*)&dwData, sizeof(DWORD) ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegSetValueEx failed. (0x%x)", lResult); __leave; } lResult = RegFlushKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegFlushKey failed. (0x%x)", lResult); __leave; } break; } case SERVICE_WIN32_OWN_PROCESS: case SERVICE_WIN32_SHARE_PROCESS: { wcscat_s(wchSubKey, _countof(wchSubKey), L"SYSTEM\\CurrentControlSet\\services\\"); wcscat_s(wchSubKey, _countof(wchSubKey), lpServiceName); lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, wchSubKey, 0, KEY_ALL_ACCESS, &hkResult ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegOpenKeyEx failed. (0x%x)", lResult); __leave; } if (!hkResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "hkResult error"); __leave; } lResult = RegSetValueEx( hkResult, L"Description", 0, REG_SZ, (const BYTE *)(lpDescription ? lpDescription : (lpDisplayName ? lpDisplayName : lpServiceName)), wcslen(lpDescription ? lpDescription : (lpDisplayName ? lpDisplayName : lpServiceName)) * sizeof(WCHAR) ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegSetValueEx failed. (0x%x)", lResult); __leave; } lResult = RegFlushKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegFlushKey failed. (0x%x)", lResult); __leave; } break; } default: { if (SERVICE_WIN32_OWN_PROCESS & dwServiceType || SERVICE_WIN32_SHARE_PROCESS & dwServiceType) { wcscat_s(wchSubKey, _countof(wchSubKey), L"SYSTEM\\CurrentControlSet\\services\\"); wcscat_s(wchSubKey, _countof(wchSubKey), lpServiceName); lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, wchSubKey, 0, KEY_ALL_ACCESS, &hkResult ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegOpenKeyEx failed. (0x%x)", lResult); __leave; } if (!hkResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "hkResult error"); __leave; } lResult = RegSetValueEx( hkResult, L"Description", 0, REG_SZ, (const BYTE *)(lpDescription ? lpDescription : (lpDisplayName ? lpDisplayName : lpServiceName)), wcslen(lpDescription ? lpDescription : (lpDisplayName ? lpDisplayName : lpServiceName)) * sizeof(WCHAR) ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegSetValueEx failed. (0x%x)", lResult); __leave; } lResult = RegFlushKey(hkResult); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegFlushKey failed. (0x%x)", lResult); __leave; } break; } printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "dwServiceType error. 0x%x", dwServiceType); __leave; } } bRet = TRUE; } __finally { if (hkResult) { RegCloseKey(hkResult); hkResult = NULL; } if (hService) { CloseServiceHandle(hService); hService = NULL; } if (hScManager) { CloseServiceHandle(hScManager); hScManager = NULL; } if (bWow64DisableWow64FsRedirection) { m_pfWow64RevertWow64FsRedirection(pOldValue); pOldValue = NULL; } } return bRet; }
BOOL CService::CanInteractWithTheDesktop( __in LPTSTR lpServiceName ) { BOOL bRet = FALSE; TCHAR tchServiceKey[MAX_PATH] = {0}; LONG lResult = 0; HKEY hKey = NULL; DWORD dwType = 0; DWORD dwData = 0; DWORD dwDataLen = 0; _try { if (!lpServiceName) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "input argument error"); __leave; } swprintf_s(tchServiceKey, _countof(tchServiceKey), _T("SYSTEM\\CurrentControlSet\\services\\%s"), lpServiceName); lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, tchServiceKey, 0, KEY_ALL_ACCESS, &hKey ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegOpenKeyEx failed. (%d)", lResult); __leave; } dwDataLen = sizeof(DWORD); lResult = RegQueryValueEx( hKey, _T("Type"), NULL, &dwType, (BYTE *)&dwData, &dwDataLen ); if (ERROR_SUCCESS != lResult) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegSetValueEx failed. (%d)", lResult); __leave; } if (dwData & SERVICE_INTERACTIVE_PROCESS) bRet = TRUE; } __finally { if (hKey) { RegCloseKey(hKey); hKey = NULL; } } return bRet; }
BOOL CService::DeleteFileInDrivers( __in LPTSTR lpServiceName ) { BOOL bRet = FALSE; LONG lRet = ERROR_SUCCESS; TCHAR tchKey[MAX_PATH] = { 0 }; HKEY hKey = NULL; DWORD dwcbData = 0; DWORD dwType = 0; TCHAR tchData[MAX_PATH] = {0}; DWORD dwData = 0; TCHAR tchFilePath[MAX_PATH] = { 0 }; PVOID pOldValue = NULL; BOOL bWow64DisableWow64FsRedirection = FALSE; OS_PROCESSOR_TYPE_USER_DEFINED OsProcType = OS_PROCESSOR_TYPE_UNKNOWN; __try { OsProcType = COperationSystemVersion::GetInstance()->GetOSProcessorType(); switch (OsProcType) { case OS_PROCESSOR_TYPE_X86: break; case OS_PROCESSOR_TYPE_X64: { if (!m_pfWow64DisableWow64FsRedirection(&pOldValue)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "Wow64DisableWow64FsRedirection failed. (%d)", GetLastError()); __leave; } bWow64DisableWow64FsRedirection = TRUE; break; } default: { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "OsProcType error. %d", OsProcType); __leave; } } if (!lpServiceName) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "input argument error"); __leave; } _tcscat_s(tchKey, _countof(tchKey), _T("SYSTEM\\CurrentControlSet\\Services\\")); _tcscat_s(tchKey, _countof(tchKey), lpServiceName); lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, tchKey, 0, KEY_QUERY_VALUE, &hKey ); if (ERROR_SUCCESS != lRet) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegOpenKeyEx failed. (%d)", lRet); __leave; } dwcbData = sizeof(DWORD); lRet = RegQueryValueEx( hKey, _T("start"), NULL, &dwType, (LPBYTE)dwData, &dwcbData ); if (ERROR_SUCCESS != lRet) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegQueryValueEx failed. (%d)", lRet); __leave; } if (SERVICE_BOOT_START != dwData && SERVICE_SYSTEM_START != dwData) { bRet = TRUE; __leave; } dwcbData = sizeof(tchData); lRet = RegQueryValueEx( hKey, _T("ImagePath"), NULL, &dwType, (LPBYTE)tchData, &dwcbData ); if (ERROR_SUCCESS != lRet) { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "RegQueryValueEx failed. (%d)", lRet); __leave; } if (_T('%') == tchData[0]) { // %systemroot%\system32\svchost.exe _tcscat_s(tchFilePath, _countof(tchFilePath), tchData); } else if (_T('\\') == tchData[0]) { if (_T('?') == tchData[1]) { // \??\C:\Windows\system32\drivers\360reskit64.sys _tcscat_s(tchFilePath, _countof(tchFilePath), tchData + 4); } else { // \SystemRoot\system32\drivers\aliide.sys _tcscat_s(tchFilePath, _countof(tchFilePath), _T("%systemroot%\\")); _tcscat_s(tchFilePath, _countof(tchFilePath), tchData + 12); } } else if (_T('s') == tchData[0] || _T('S') == tchData[0]) { // system32\DRIVERS\360netmon.sys _tcscat_s(tchFilePath, _countof(tchFilePath), _T("%systemroot%\\")); _tcscat_s(tchFilePath, _countof(tchFilePath), tchData); } else { // C:\Windows\SysWOW64\Macromed\Flash\FlashPlayerUpdateService.exe _tcscat_s(tchFilePath, _countof(tchFilePath), tchData); } if (!DeleteFile(tchFilePath)) { printfEx(MOD_SERVICE, PRINTF_LEVEL_WARNING, "DeleteFile failed. %S (%d)", tchFilePath, GetLastError()); // __leave; } bRet = TRUE; } __finally { if (hKey) { RegCloseKey(hKey); hKey = NULL; } if (bWow64DisableWow64FsRedirection) { m_pfWow64RevertWow64FsRedirection(pOldValue); pOldValue = NULL; } } return bRet; }
// // Purpose: // Called by SCM whenever a control code is sent to the service // using the ControlService function. // // Parameters: // dwCtrl - control code // // Return value: // None // DWORD WINAPI CService::CtrlHandler( _In_ DWORD dwControl, _In_ DWORD dwEventType, _In_ LPVOID lpEventData, _In_ LPVOID lpContext ) { DWORD dwRet = NO_ERROR; __try { // Handle the requested control code. switch (dwControl) { case SERVICE_CONTROL_STOP: { printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "SERVICE_CONTROL_STOP"); if (!CService::GetInstance()->m_pfUnloadMod()) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "[SERVICE_CONTROL_STOP] ms_UnloadMod failed"); CService::GetInstance()->ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0); // Signal the service to stop. if (!SetEvent(CService::GetInstance()->m_hSvcStopEvent)) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "[SERVICE_CONTROL_STOP] SetEvent failed. (%d)", GetLastError()); CService::GetInstance()->ReportSvcStatus(CService::GetInstance()->m_SvcStatus.dwCurrentState, NO_ERROR, 0); CPrintfEx::ReleaseInstance(); break; } case SERVICE_CONTROL_PAUSE: case SERVICE_CONTROL_CONTINUE: case SERVICE_CONTROL_INTERROGATE: break; case SERVICE_CONTROL_SHUTDOWN: { printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "SERVICE_CONTROL_SHUTDOWN"); if (!CService::GetInstance()->m_pfUnloadMod()) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "[SERVICE_CONTROL_SHUTDOWN] ms_UnloadMod failed"); if (!SetEvent(CService::GetInstance()->m_hSvcStopEvent)) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "[SERVICE_CONTROL_SHUTDOWN] SetEvent failed. (%d)", GetLastError()); CPrintfEx::ReleaseInstance(); break; } case SERVICE_CONTROL_PARAMCHANGE: case SERVICE_CONTROL_NETBINDADD: case SERVICE_CONTROL_NETBINDREMOVE: case SERVICE_CONTROL_NETBINDENABLE: case SERVICE_CONTROL_NETBINDDISABLE: case SERVICE_CONTROL_DEVICEEVENT: case SERVICE_CONTROL_HARDWAREPROFILECHANGE: break; case SERVICE_CONTROL_POWEREVENT: { printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "SERVICE_CONTROL_POWEREVENT"); break; } case SERVICE_CONTROL_SESSIONCHANGE: break; case SERVICE_CONTROL_PRESHUTDOWN: { printfEx(MOD_SERVICE, PRINTF_LEVEL_INFORMATION, "SERVICE_CONTROL_PRESHUTDOWN"); if (!CService::GetInstance()->m_pfUnloadMod()) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "[SERVICE_CONTROL_PRESHUTDOWN] ms_UnloadMod failed"); if (!SetEvent(CService::GetInstance()->m_hSvcStopEvent)) printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "[SERVICE_CONTROL_PRESHUTDOWN] SetEvent failed. (%d)", GetLastError()); CPrintfEx::ReleaseInstance(); break; } case SERVICE_CONTROL_TIMECHANGE: case SERVICE_CONTROL_TRIGGEREVENT: break; default: { printfEx(MOD_SERVICE, PRINTF_LEVEL_ERROR, "dwControl error. 0x%x", dwControl); __leave; } } } __finally { ; } return dwRet; }