reefbarman
June 9th, 2009, 05:10 AM
Hi Guys,
I'm currently making an App that allows restricted access to a PC, without having to write windows dll files to replace the windows log on stuff. So my application opens full screen, blocks all ways of closing this app, so then the only way to log on, is by entering a username and password that authenticates with a mysql database. This logs the user onto the computer, i then want when the user logs off (brings my app back up) to close all the open applications, internet explorer windows, and windows explorer windows cleanly. (Note this is for a internet cafe I'm putting together, and is not malicious in anyway).
Now i currently have some code, that does what I want in a brute force kind of way. But its not very stable and sometimes even closes my application while leaving other apps open.
I was wondering if you mind looking over what im doing and can suggest a better and more fool proof way to do things. Cheers
void CleanupSystem()
{
m_cleaningUp = true;
//Close all the windows open
EnumWindows(EnumWindowsProc, (LPARAM)m_hWnd);
//Make sure iexplorer is dead because it always seems to stick around
//system("taskkill /f /t /im iexplorer.exe");
//Close down windows and any open windows but this sometimes cause everything to close even my app
system("taskkill /f /t /im explorer.exe");
//Start windows again
system("start explorer.exe");
//Once everything is close make sure im the topmost active window
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
//Run cleanup script (any temp file cleaning)
system("cleanup.bat");
m_cleaningUp = false;
}
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
DWORD dwThreadId, dwProcessId;
HINSTANCE hInstance;
char String[255];
HANDLE hProcess;
if (!hWnd)
return TRUE; // Not a window
if (!::IsWindowVisible(hWnd))
return TRUE; // Not visible
if (!SendMessage(hWnd, WM_GETTEXT, sizeof(String), (LPARAM)String))
return TRUE; // No window title
if (hWnd == (HANDLE)lParam)
return TRUE; //This Window
hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
dwThreadId = GetWindowThreadProcessId(hWnd, &dwProcessId);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
if (GetModuleFileNameExA(hProcess, hInstance, String, sizeof(String)))
{
OutputDebugStringA(String);
OutputDebugStringA("\n");
string exe = String;
if (exe.find(".exe") != string::npos || exe.find(".dll") != string::npos && exe.find("system32") == string::npos && exe.find("msenv") == string::npos)
{
string exeToKill = exe.substr(exe.find_last_of("\\") + 1, exe.size());
OutputDebugStringA(exeToKill.c_str());
OutputDebugStringA("\n");
char PID[32];
itoa((int)dwProcessId, PID, 10);
string pid = PID;
string command = "taskkill /f /t /PID " + pid;
OutputDebugStringA(PID);
OutputDebugStringA("\n");
system(command.c_str());
}
}
CloseHandle(hProcess);
return TRUE;
}
I'm currently making an App that allows restricted access to a PC, without having to write windows dll files to replace the windows log on stuff. So my application opens full screen, blocks all ways of closing this app, so then the only way to log on, is by entering a username and password that authenticates with a mysql database. This logs the user onto the computer, i then want when the user logs off (brings my app back up) to close all the open applications, internet explorer windows, and windows explorer windows cleanly. (Note this is for a internet cafe I'm putting together, and is not malicious in anyway).
Now i currently have some code, that does what I want in a brute force kind of way. But its not very stable and sometimes even closes my application while leaving other apps open.
I was wondering if you mind looking over what im doing and can suggest a better and more fool proof way to do things. Cheers
void CleanupSystem()
{
m_cleaningUp = true;
//Close all the windows open
EnumWindows(EnumWindowsProc, (LPARAM)m_hWnd);
//Make sure iexplorer is dead because it always seems to stick around
//system("taskkill /f /t /im iexplorer.exe");
//Close down windows and any open windows but this sometimes cause everything to close even my app
system("taskkill /f /t /im explorer.exe");
//Start windows again
system("start explorer.exe");
//Once everything is close make sure im the topmost active window
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
//Run cleanup script (any temp file cleaning)
system("cleanup.bat");
m_cleaningUp = false;
}
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
DWORD dwThreadId, dwProcessId;
HINSTANCE hInstance;
char String[255];
HANDLE hProcess;
if (!hWnd)
return TRUE; // Not a window
if (!::IsWindowVisible(hWnd))
return TRUE; // Not visible
if (!SendMessage(hWnd, WM_GETTEXT, sizeof(String), (LPARAM)String))
return TRUE; // No window title
if (hWnd == (HANDLE)lParam)
return TRUE; //This Window
hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
dwThreadId = GetWindowThreadProcessId(hWnd, &dwProcessId);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
if (GetModuleFileNameExA(hProcess, hInstance, String, sizeof(String)))
{
OutputDebugStringA(String);
OutputDebugStringA("\n");
string exe = String;
if (exe.find(".exe") != string::npos || exe.find(".dll") != string::npos && exe.find("system32") == string::npos && exe.find("msenv") == string::npos)
{
string exeToKill = exe.substr(exe.find_last_of("\\") + 1, exe.size());
OutputDebugStringA(exeToKill.c_str());
OutputDebugStringA("\n");
char PID[32];
itoa((int)dwProcessId, PID, 10);
string pid = PID;
string command = "taskkill /f /t /PID " + pid;
OutputDebugStringA(PID);
OutputDebugStringA("\n");
system(command.c_str());
}
}
CloseHandle(hProcess);
return TRUE;
}