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.

http://cid-e2c1100334a33877.skydrive.live.com/self.aspx/.Public/Code For blog/SanpWindows.zip



// Written by: Ahmed Essam Naiem
// Blog: http://www.ahmed-essam.com
// Summery, The code take snapshoot of the open windows every 10 minutes

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

// 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()
{
CreateDirectory(L"C:\\MyOpenWindows",NULL);
}

// 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
memset(strBuffer,0,sizeof(strBuffer));
// 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
GetWindowText(hWnd,strBuffer,255);

// 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.
fwprintf(fileHandle,L"\t\t\n",strBuffer);
}
// 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.
PrepareDirectoreis();
// Getting the current time
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);

// Generating monitor file name that is based on the current date (Day, Month, Year)
char strFileName[256];
sprintf(strFileName,"C:\\MyOpenWindows\\MyOpenWindows[%02d-%02d-%02d].xml",sysTime.wDay,sysTime.wMonth,sysTime.wYear);
// Loop for ever
while(true)
{
// Get the current time to create the monitoring entry ( Hours, Moments )
GetLocalTime(&sysTime);
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
if(doAddHeader)
fwprintf(fileHandle,L"\n\n");
// Add the entry to the file, it is added with the current time (Hour, moments)
fwprintf(fileHandle,L"\t\n",sysTime.wHour,sysTime.wMinute);
// Enumerate the opened windows.
EnumWindows(WindowsEnumerator,0);
// Close the entry
fwprintf(fileHandle,L"\t\n");
// 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)
fclose(fileHandle);

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

No comments: