CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  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
    Hanover Germany
    Posts
    20,396

    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
    Hanover Germany
    Posts
    20,396

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  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
    Hanover Germany
    Posts
    20,396

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured