DLL Send/PostMessage to caller

This is a discussion on DLL Send/PostMessage to caller within the Windows Programming forums, part of the Platform Specific Boards category; I made a global keyboard hook which according to MSDN needs to be in a separate DLL and it works ...

  1. #1
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555

    DLL Send/PostMessage to caller

    I made a global keyboard hook which according to MSDN needs to be in a separate DLL and it works fine. Now when a user does a certain keypress I want the DLL to report that back to the application loading it who does all the job. I pass the HWND of the application to the DLL in an initialization procedure and then try to SendMessage and PostMessage to it with a (WM_USER + offset) message, but the message is never received by the application. Since the DLL resides in the same thread it should be no issue. I also tried sending the message to the thread itself (NULL HWND) and that didn't work out either.

    How can I communicate with the caller?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    Quote Originally Posted by MSDN
    Filter functions for systemwide hooks must be prepared to share any data they need across the different processes they are running from. A DLL is mapped into the address space of each of its client processes. Global variables within the DLL will be instance specific...
    So choose your favorite IPC mechanism.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Have you tried using shared memory?

    Code:
    #pragma data_seg(".BOB")
    // Define the server handle.
    HWND hWndServer = NULL;
    #pragma data_seg()
    // Include a section comment that the data segment is read/write/shared.
    #pragma comment(linker, "/section:.BOB,rws")
    
    
    __declspec(dllexport) BOOL SetHook(HWND hWnd)
    {
    	.
    	.
    	.
    	.	
      	hWndServer = hWnd;
      	PostMessage(hWndServer, IT_STARTED, 0, 0);
    }

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    I try to stay away from shared memory and things that aren't lock- and wait-free.

    Quote Originally Posted by Codeplug View Post
    So choose your favorite IPC mechanism.

    gg
    I don't quite get it, filter function? The page on MSDN seems to be about a functional programming filter function.
    I am not trying to send any data, just a message (wparam and lparam are both 0). Just sending a message to a window should be fine no matter how apart the processes are AFAIK.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    >> ... filter function?
    Forgot to post the list, sorry...
    http://msdn.microsoft.com/en-us/library/ms997537.aspx
    Section "Filter functions in DLLs". That article just calls the hook functions "filter functions".

    So which hook are you using?
    WH_KEYBOARD_LL is always called in the same thread context which installed the hook - which requires that thread to have a message loop. Also means that you don't need to put it in a DLL. This is what I've used in the past - makes things easier.

    >> Just sending a message to a window should be fine...
    Perhaps - but where did you get the window handle?

    >> I pass the HWND of the application to the DLL in an initialization procedure...
    >>>> DLL is mapped into the address space of each of its client processes. Global variables within the DLL will be instance specific...
    So your HWND value may not be what you think it is.

    Maybe WH_KEYBOARD_LL will simplify things all around for you

    gg

  6. #6
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    I don't understand how instance specific would be a problem, but I have noticed that the static global variable is back at NULL when another function is called after the one that passes the HWND to the DLL. Using FindWindow() every time I need to get the handle to the window works though so thanks for the help.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    Say a DLL has global variable G. If process A and process B both load the DLL, each process gets a unique instance of G.

    There are multiple processes loading your hook DLL - thus a unique instance of the global containing the HWND for each process. Calling your "Set_HWND()" DLL function does not change the instances in other processes.

    gg

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    I highly suggest using named shared memory for this task. Tasks like this, passing interprocess variables is specifically what named shared memory is for.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 04:29 AM
  5. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 02:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21