Skip to content

Instantly share code, notes, and snippets.

@9re
Created April 29, 2011 08:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9re/948052 to your computer and use it in GitHub Desktop.
Save 9re/948052 to your computer and use it in GitHub Desktop.
A Simple Win32 TaskManager
kill
Tsukkomeya.exe
kill
1-Server.exe
wait
100
start
\\Users\\admin\\Dropbox\\YsmtHarisensor\\deploy\\Tsukkomeya.exe
\\Users\\admin\\Dropbox\\YsmtHarisensor\\deploy
wait
10
start
\\Users\\admin\\Dropbox\\PinkExhibition\\server\\_bin\\Release\\1-Server.exe
\\Users\\admin\\Dropbox\\PinkExhibition\\server\\_bin\\Release\\
#include <windows.h>
#include <stdio.h>
#include <tlhelp32.h>
#include <tchar.h>
#define CONFIG_FILE "restart.cfg"
#define KILL "kill"
#define WAIT "wait"
#define START "start"
#define isEqual(a, b) (strcmp(a, b) == 0)
#define getNextToken(a) (a = strtok(NULL, "\r\n"))
DWORD getProcessIdByName(const char* exename) {
HANDLE hSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0) ;
PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) };
if (hSnapshot == INVALID_HANDLE_VALUE) {
printf("failed to take process snapshot\n");
return 0;
}
if (Process32First (hSnapshot, &pe32)) {
do {
if (isEqual(exename, pe32.szExeFile)) {
printf ("found process:%ld for exe name:%s\n", pe32.th32ProcessID, pe32.szExeFile);
return pe32.th32ProcessID;
}
} while (Process32Next (hSnapshot, &pe32));
}
CloseHandle (hSnapshot);
return 0;
}
void killProcessByName(const char* exename) {
DWORD dwProcessId = getProcessIdByName(exename);
HANDLE hProcess;
BOOL fResult = FALSE;
if (dwProcessId == 0) {
printf("failed to get process id for %s\n", exename);
}
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
if(hProcess != NULL){
printf("going to terminate process: %ld\n", dwProcessId);
fResult = TerminateProcess(hProcess, 0);
WaitForSingleObject(hProcess, 500);
CloseHandle(hProcess);
}
}
void startProcess(const char* path, const char* directory) {
HINSTANCE hInstance;
hInstance = ShellExecute(NULL, "open", path, NULL, directory, SW_SHOWNORMAL);
switch ((int)hInstance) {
case ERROR_FILE_NOT_FOUND:
printf("error: file not found: %s!\n", path);
break;
case ERROR_PATH_NOT_FOUND:
printf("error: path not found: %s!\n", path);
default:
if (hInstance > 32) {
printf("the file %s has been opened successfully!\n", path);
}
}
}
int _tmain(int argc, _TCHAR* argv[]) {
HANDLE hFile;
LPSTR strFile;
char* pch;
char* prev;
DWORD dwFileSize;
DWORD dwReadSize;
hFile = CreateFile(_T(CONFIG_FILE), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("couldn't open config file: %s\n", CONFIG_FILE);
return -1;
}
dwFileSize = GetFileSize(hFile, NULL);
strFile = (LPSTR) malloc(dwFileSize);
ReadFile(hFile, strFile, dwFileSize, &dwReadSize, NULL);
CloseHandle(hFile);
pch = strtok(strFile, "\r\n");
while (pch) {
if (isEqual(pch, KILL)) {
getNextToken(pch);
killProcessByName(pch);
} else if (isEqual(pch, WAIT)) {
getNextToken(pch);
printf("going to sleep: %d\n", atoi(pch));
Sleep(atoi(pch));
} else if (isEqual(pch, START)) {
getNextToken(prev);
getNextToken(pch);
printf("start process %s with base directory %s\n", prev, pch);
startProcess(prev, pch);
}
getNextToken(pch);
}
free(strFile);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment