Hi Everyone

I've seen posts similar to this here but not an exact solution to my problem.

Having written a piece of software at work (which I ran as an invisible console app), my boss wishes it to respond to Windows exit signals (such as logoff). Now, on Windows XP this was fairly trivial since XP used to send events like this to console apps, but in Windows 7 it doesn't (yep really).

So what I need to do is to rewrite the app to run as a hidden window and put in some checks for Windows events instead of console events, which I'm confident I can do.

However before I even get that far, by introducing a 'hidden' window the application won't do anything, whereas when I show the window it runs fine. In both cases the exit procedures seem to work, but the main loop (which appends to a file) won't run when hidden, which is what I'm trying to achieve here.

I'm deeply suspicious that by hiding the window, it somehow becomes deactivated, has anyone else encountered this? Here is my code, I've kept it as small as I can, thank you to anyone who takes time to respond!:

Code:
#include <stdio.h>
#include <windows.h>

const char g_szClassName[] = "myWindowClass";
FILE *fp;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "My freakin window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 0, 0,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    //ShowWindow(hwnd, nCmdShow); // If I switch these lines the file appends
    ShowWindow(hwnd, SW_HIDE);    // just fine, SW_HIDE prevents it
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
	fp=fopen("test.txt", "a+");
        
	TranslateMessage(&Msg);
	DispatchMessage(&Msg);

	fprintf(fp, "Testing...\n");
	fclose(fp);
    }
    return Msg.wParam;
}