write in log file through dll
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: write in log file through dll

  1. #1
    Join Date
    Apr 2013
    Posts
    5

    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);
    }
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: write in log file through dll

    1. Please use Code tags while posting code snippets!
    2. What value do you retun from CBTProc?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2013
    Posts
    5

    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);

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: write in log file through dll

    Quote Originally Posted by amonil View Post
    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)
    Victor Nijegorodov

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 2013
    Posts
    5

    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

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    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!)
    Victor Nijegorodov

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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().
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center