Why would my program output to a text file when the window is in focus, but not when another window has the focus?

the following is in a dll and if the function that is giving me probs:

LRESULT CALLBACK KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam )
{
HDC hDC;
//Timer Elements
_ftime( &timebuffer );
timeline = ctime( & ( timebuffer.time ) );



if ( nCode >= 0 ) {

if ( nCode == HC_NOREMOVE )
strcpy(szType, "NOT Removed from Queue");
else
strcpy(szType, "REMOVED from Queue ");

//strcpy(temp, keyType(wParam));
if(pKB) //Output to txt File
if (lParam < 0)
fprintf(pKB, "Key:%d,Up,%.19s.%hu %s",wParam,timeline, timebuffer.millitm, &timeline[20] );
else
fprintf(pKB, "Key:%d,Down,%.19s.%hu %s",wParam,timeline, timebuffer.millitm, &timeline[20] );

wsprintf((LPSTR)szFilterLine[KEYBOARDINDEX],
"KEYBOARD\tKey:%d\t%s",wParam,(LPSTR)szType);

hDC = GetDC(hwndMain);
TabbedTextOut(hDC, 1, nLineHeight * KEYBOARDINDEX,
(LPSTR)szFilterLine[KEYBOARDINDEX],
strlen(szFilterLine[KEYBOARDINDEX]), 0, NULL, 1);
ReleaseDC(hwndMain, hDC);
}
CallNextHookEx.
return( CallNextHookEx(hhookHooks[KEYBOARDINDEX], nCode, wParam, lParam));
}


am i outputting to the text file properly?!

it paints the results in the window ok, but dosent output to the txt file?! why would this be?

Andy