write in log file through dll
so i have this dll with a DBTProc, whenever a window gets created i want to write something in a log file. Now everytime I run it it adds three times the
text in this log file, but then it does nothing anymore. I do hear a beep everytime a window get created so it doesn't actually open my file to write in, any ideas about what's wrong?
code:
LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code < 0)
{
return CallNextHookEx(CBThook, code, wParam, lParam);
}
hWndnew = (HWND)wParam;
if (code == HCBT_CREATEWND)
{
CREATESTRUCTA* cswnd = (CREATESTRUCTA*) lParam;
cswnd-> y = 0;
cswnd-> x = 0;
cswnd -> cx = 500;
cswnd -> cy = 400;
lParam = (LPARAM) cswnd;
FILE * logfile;
if((logfile=fopen("logfile.LOG","a")) != NULL)
{
fprintf(logfile, "hoi\n");
fclose(logfile);
}
else{
Beep(2000, 20);
}
}
Re: write in log file through dll
1. Please use Code tags while posting code snippets!
2. What value do you retun from CBTProc?
Re: write in log file through dll
Here is my code in between codetags:
Code:
LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code < 0)
{
return CallNextHookEx(CBThook, code, wParam, lParam);
}
hWndnew = (HWND)wParam;
if (code == HCBT_CREATEWND)
{
CREATESTRUCTA* cswnd = (CREATESTRUCTA*) lParam;
cswnd-> y = 0;
cswnd-> x = 0;
cswnd -> cx = 500;
cswnd -> cy = 400;
lParam = (LPARAM) cswnd;
FILE * logfile;
if((logfile=fopen("logfile.LOG","a")) != NULL)
{
fprintf(logfile, "hoi\n");
fclose(logfile);
}
else{
Beep(2000, 20);
}
}
else if (code == HCBT_ACTIVATE)
{
CBTACTIVATESTRUCT* structure = (CBTACTIVATESTRUCT*) lParam;
}
else if (code == HCBT_DESTROYWND)
{
}
return 0;
}
I Posted my whole CBTProc function this time, as you can see it returns 0.
this is the code I use to install the function:
Code:
CBThook = SetWindowsHookEx(WH_CBT, (HOOKPROC) CBTProc, hinst, 0);
Re: write in log file through dll
Quote:
Originally Posted by
amonil
I do hear a beep everytime a window get created so it doesn't actually open my file to write in, any ideas about what's wrong?
Obviously fopen fails!
See the possible reasons for its failure in the documenation (and sorry, I do not use fopen , so cannot say about such a possible reason)
Re: write in log file through dll
The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.
Re: write in log file through dll
Changing the mode from a to a+ didn't have any effect, I also tried using freopen() after the first time opening the file using the following code, but this way the program doesn't work at all.
Code:
static boolean isit = false;
static FILE * logfile;
LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code < 0)
{
return CallNextHookEx(CBThook, code, wParam, lParam);
}
hWndnew = (HWND)wParam;
if (code == HCBT_CREATEWND)
{
if(isit = false && (logfile = fopen("logfile.LOG","a+")) != NULL)
{
fprintf(logfile, "hoi");
//fclose(logfile);
isit = true;
}
else if((logfile = freopen("logfile.LOG","a+", logfile)) != NULL)
{
fprintf(logfile, "hoi");
}
else
{
Beep(2000, 20);
}
}
//rest of code
Re: write in log file through dll
Hey amonil!
In what system do you run your code? In Vista_ Windows 7, Windows Server 2008?
Perhaps there are some security reasons your dll cannot open this file? (FYI: I never tried global hooking, so it is just a guess!)
Re: write in log file through dll
Instead of using fopen, try using CreateFile. That way when/if it fails you'll be able to get a proper error code from GetlastError().