Alright well i must say.. i havent really touched any cpp for a year or two, untill now. This is a snippet from a larger peice which gets the name of the active window.

I only want it to write the name of the active window, if the active window has changed since the last write. I cant seem to find where to put the loop (rusty i know)

I started with this
Code:
void saveCaption(HWND hwnd, TCHAR *filename) {
  int length = GetWindowTextLength( hwnd ) + 1;
  TCHAR *caption = new TCHAR[length];

  GetWindowText( hwnd, caption, length );

  ofstream myfile;
  myfile.open (filename, ios::app | ios::out);
  myfile << caption << "\n"  ;
  myfile.close();

}


int main() {



  saveCaption( GetForegroundWindow(), TEXT("caption.txt") );

    }
Then added in a while loop, so its constantly checking the window instead of checking it once and exiting

Code:
void saveCaption(HWND hwnd, TCHAR *filename) {
  int length = GetWindowTextLength( hwnd ) + 1;
  TCHAR *caption = new TCHAR[length];

  GetWindowText( hwnd, caption, length );

  ofstream myfile;
  myfile.open (filename, ios::app | ios::out);
  myfile << caption << "\n"  ;
  myfile.close();

}


int main() {
    while (1) {


  saveCaption( GetForegroundWindow(), TEXT("caption.txt") );
  Sleep(50);
    }}
This works to some degree but its constantly writing the same thing over and over again which seems redundant. eg ( it'd write "main.cpp - Notepad" 50 times over untill the window focus is changed and writes the name of the next window 50 times over).



I cant figure out how to compare the name of the window in the previous loop and the current variable content.

Anyway, im really rusty so any help would be appreciated.

Cheers, kp.