Search This Blog

Friday, December 04, 2009

Code to monitor your self

Peace be upon you

How are you? today I am giving you piece of code that I think it might be useful for most of us if you care about watching your self.

The code that I am talking about is very simple, the code is just make a snapshots of your open windows and save it in file, it is saved in XML like format, The application collect the open window titles every 10 minutes.

The code uses Windows API called "EnumWindows" to collect all the open window, after that there is a check if this window is in task bar or not, let us see the code, the code has enough comment as I think

Before getting to the code here is link for the full source code, I recommend you build it your self, to avoid any thing bad might come from the binary. For blog/

// Written by: Ahmed Essam Naiem
// Blog:
// Summery, The code take snapshoot of the open windows every 10 minutes

// used in the file operations, fopen, wfprintf,fclose
// used in the file exist check,_access

// Public file handle will be used by many functions.
FILE* fileHandle = NULL;

// The function creates the directory that will contain the monitor files
void PrepareDirectoreis()

// Call back function for "EnumWindows", the function will get all opened functions
// by their handle, then other operations can be done for that handle
BOOL CALLBACK WindowsEnumerator(HWND hWnd, LPARAM lParam)
// Allocating space for the window title buffer
// I did it very large to avoid any lose, also
// I prefer stack variable to avoid memory fragmentation
// in Replacement for that u can use GetWindowTextLength
// then dynamiclly allocate the memory in the heap then do
// what ever with the buffer then deallocate it at the end
// at the end of the function
TCHAR strBuffer[256];
// Clearing memory to avoid memory rubbish
// Get the window text in the buffer that we have prevoiusly
// talked about, You will notice that I have used the buffer
// with 1 character less, this kept for the string terminator

// Check if the buffer has something, also check if the window
// is visiable window, without IsWindowVisible we will have TONS
// of windows that we actually don't need
if(wcslen(strBuffer)>0 && IsWindowVisible(hWnd)){
// This line put the buffer in the opened file, with
// XML like format.
// The function return true to keep the getting window handles.
return TRUE;

// The main application function, "the Entry Point"
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
// Preparing the directories that will hold the monitor fiels.
// Getting the current time

// Generating monitor file name that is based on the current date (Day, Month, Year)
char strFileName[256];
// Loop for ever
// Get the current time to create the monitoring entry ( Hours, Moments )
bool doAddHeader = false;

// Check if the file exist or not, to set a flag to construce XML hearder
if(_access(strFileName, 0))
doAddHeader = true;

// Open file for append, this will help us keeping the old data
// The file handle is a public variable this will help the other
// functions to use this handle
fileHandle = fopen(strFileName,"at");

// Check if the application need to have a header
// Add the entry to the file, it is added with the current time (Hour, moments)
// Enumerate the opened windows.
// Close the entry
// Close the file, this will allow other applications to see the latest update,
// also will give chance to delete the file (in case u don't like it :D)

// Sleep for 10 minutes, of course u can change this intervale
Sleep(1000 * 60 * 10);
} // While closure.
return 0;

No comments: