Thanks for the Cookie.
Here's the updated source. Will now check for "Frozen" servers as well.
I"m working on a cleaner version with a GUI, hopefully i'll be able to figure out how to make it a little more dynamic as well.
Also if someone has this running under WINE on and linux distro, let me know what missing files you have. Most of them are windows files that should be in system32 but have not yet been ported. Please list any other errors as well.
#include <windows.h> // these are our preprocessors. Our API is defined here
#include <iostream>
using namespace std; //include the namespace lib. includes c functions (cout, cin)
STARTUPINFO si = { sizeof(si) }; //This is a pointer to our "Start Up info" typedef
PROCESS_INFORMATION pi; //This is a pointer to our process information (this is where we can grab the pid)
char szExe[] = "C:\\Program Files\\Blockland\\dedicated.bat";
HWND hwnd;
HANDLE phandle; //This is a typedef for the OpenProcess function
DWORD pid; //this will store the PID of blockland.
DWORD result;
void info(); // Just moving the jibberish out of the way
int main(){
info(); //spits out our info and waits for the user
hwnd=FindWindow(0,"C:\\Program Files\\Blockland\\Blockland.exe");
if(hwnd){
GetWindowThreadProcessId(hwnd, &pid);
cout << endl << "the dedicated server is running under the PID:" << pid << endl;
cout << "Running Back checks..." << endl;
}
while(TRUE){
cout << "\nChecking if the server is up..." << endl;
hwnd=FindWindow(0,"C:\\Program Files\\Blockland\\Blockland.exe");
SendMessageTimeout(hwnd,WM_NULL,0,0,SMTO_BLOCK | SMTO_ABORTIFHUNG, 5000, &result);
if(!hwnd){
cout << "It appears that Blockland isn't up. Re-Launching...";
///*
CreateProcess(0, szExe, 0, 0, TRUE, 0, 0, 0, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
Sleep(2500);
hwnd=FindWindow(0,"C:\\Program Files\\Blockland\\Blockland.exe");
if(!hwnd){
MessageBox(0,"Unable to Re-Launch your dedicated server. Please make sure that you put this file into your BLOCKLAND folder",0,MB_OK);
break;
}
}
cout << endl << "Making sure it's not frozen..." << endl;
if(result){
cout << "It appears that Blockland is frozen running. Re-Launching...";
///*
CreateProcess(0, szExe, 0, 0, TRUE, 0, 0, 0, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
Sleep(2500);
hwnd=FindWindow(0,"C:\\Program Files\\Blockland\\Blockland.exe");
if(!hwnd){
MessageBox(0,"Unable to Re-Launch your dedicated server. Please make sure that you put this file into your BLOCKLAND folder",0,MB_OK);
break;
}
else{
GetWindowThreadProcessId(hwnd, &pid);
phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
cout << "\n your dedicated server has been succefully relaunched." << endl;
}
}
else{
cout << endl << "Check passed, everything is fine" << endl;
cout << "Currently the PID of the hooked blockland server is:" << pid << endl;
//*/
}
Sleep(30000);
}
CloseHandle(phandle);
return 0;
}
void info(){
cout << "Dedicated Server Watchdog Program" << endl;
cout << " Created for Blockland V9" << endl;
cout << " 8/24/08 - Version 1.0 " << endl;
cout << "By: Poent" << endl;
cout << "\n\nThis launcher was designed to work with Blockland stored in:\n C:\\Program Files\\Blockland" << endl;
cout << "You will be able to change this is later versions, \nbut until then... leave it there." << endl;
cout << "\n\n By default, this program will check every 30 seconds \nto make sure that blockland is running." << endl;
cout << "If for any reason the dedicated server is \nnot found, it will relaunch 'dedicated.bat' and verify \nthat it successfully relaunched." << endl;
cout << "\n\n The watchdog program is now ready to launch. \nOnce you're ready to begin, Simply hit 'Enter'..." << endl;
cin.get();
}
I tried loading this up in wine and I've only been able to launch it one way:
1. under terminal, run sudo WINE cmd.exe
2. navigate to your BL folder w/ DSWP in it
3. run DSWP from the terminal.
Once again, please report any missing files and errors here so that I can correct them.