At times you will need to write a app without an active window. When you double click the
.exe it will just run in the background. You have seen everything here in previous
tutorials so this will just be a refresher :)
We will be using a new function called GetWindowText. Use your help files and
check out GetWindowText.
This cool little program will run silently in the background and record ( write )
the name of every Windows Title Bar that you are viewing. At the top left hand corner
of every window there is a Title name of the current window being viewed. For example:
if you are running Internet Explorer you will see "C/C++ Programming - Windows Internet Explorer"
in the top left corner of the browser window. This program will get that title and write it
to your hard drive :) I set the app to check every eight seconds if the window title name has changed.
If so then write out the new name. This will help keep the file size smaller than if it was writting
the window title name every eight seconds reguardless of change. Be careful you don't forget
this is running :)
Of course there isn't an "exit" button so you will need to open your Task Manager and close the
program before you try to open and view the file created.
When you create your new project, remember to select "Empty Project":)
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <windows.h>
char g_compare[ 260 ];
int StringLength( char *pString )
{
int i;
for ( i = 0; pString[ i ] != 0; i++ )
{
}
return i;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance, LPSTR pCmdLine, int iCmdShow)
{
//create directory for tracking file
CreateDirectory( "C:\\IETitles" ,NULL );
//continue to do this until you shut
//the app down through Task Manager
while ( 1 )
{
//get the foreground window
HWND hwnd = GetForegroundWindow( );
char caption[ 260 ];
//get the title of the window and
//save to "caption"
GetWindowText( hwnd, caption, 260 );
//if the window title name has not changed
//in the last eight seconds skip writing it
//out again
if ( strcmp( caption, g_compare ) == 0 )
{
}
//if the name has changed enter the "else"
//statement to write out new title name
else
{
//copy the new title name to our
//global variable so we can
//compare it in eight seconds
strncpy( g_compare, caption, 260 );
int length;
//send the new title name through
//our StringLength function to get
//the actual length. This way we don't
//waste 260 Bytes each write if we only
//need to write 9 bytes :)
length = StringLength( caption );
//create and open the filename
FILE *pFile = fopen( "C:\\IETitles\\titles.doc", "ab" );
char caption2[ 260 ];
//add \n to our title name so it
//writes on the next line each time
sprintf( caption2, "\n%s", caption );
//write out the new title name
fwrite( &caption2, length + 1, 1, pFile );
//close the file!
fclose( pFile );
}
Sleep( 8000 );
}
return 0;
}