// 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;
}