CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2005
    Posts
    42

    Angry IPC from hook DLL to Application

    What is the best mechanism for inter process communication between the injected hook DLL and the process that injected it into the external application.

    What I want is to forward all the messages that my DLL intercepts to my original application, this have to be made in some king of synchronized manner.

    Many thanks Henrik

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: IPC from hook DLL to Application

    I would say that given the specs... Sending WM_COPYDATA messages would be the easiest solution.

    But if it's the best choice for the particular application is another matter. There are so many other types of IPC you can use. It depends a bit on how much data you need to transfer, how often you get data and so on...

  3. #3
    Join Date
    Feb 2005
    Posts
    42

    Re: IPC from hook DLL to Application

    Ok I will try to give a more precise specification.

    I am using the WH_CALLWNDPROC hook to spy for messages in an external thread. All the messages are filtered in my DLL, now I want my application that sets the hook to get a COPY of the messages that was filtered out in my DLL. I am using a console application so I rather don’t want to solve this whit message passing.

    I was thinking about some producer consumer scenario using a dynamic shared memory space in the DLL, what do you think about that?

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: IPC from hook DLL to Application

    Quote Originally Posted by ZwOOp
    I was thinking about some producer consumer scenario using a dynamic shared memory space in the DLL, what do you think about that?
    That is a possibility too, although somewhat harder to do.

    You will need to convert pointers to datastructures in the messages into actual structures though, a pointer from the hooked application has no meaning in another application. (regardless of which IPC method you would use.

    Example the WM_SETTEXT function sets text with the pointer to the text in lParam. But the pointer value in lParam won't mean much in the 'server' applciation (your console app) so you hook will need to copy the text and pass it along with whatever system you use.

    If you want to grab all messages, and need to have all data associated with each message, you will have to create a LOT of stubs to copy data in various structures (with possible internal pointers of their own) into data you can IPC towards your app.

    As to IPC methods:
    - WM_COPYDATA
    - sockets
    - (named) pipes
    - mailslots
    - shared memory
    - dde
    - COM
    - Explicit cross-process data copy (ReadProcessMemory)
    - RPC
    - ...
    Too many possibilities ;-)

  5. #5
    Join Date
    Feb 2005
    Posts
    42

    Talking Re: IPC from hook DLL to Application

    Ok lucky for me i'm oly interested in the EM_REPLACESEL string pointer Maby I go for the shared memory.
    I'm extremly inexperienced whit C++ maby you ca point me in the right direction for how to convert this LPARAM string pointer into the actual string so i dont have to scan the internet for that to
    Last edited by ZwOOp; February 7th, 2005 at 04:51 PM.

  6. #6
    Join Date
    Feb 2005
    Posts
    42

    Thumbs down Now im totaly stuck (again ;) )

    Hi there again all fellow programmers. I have soon finished my little helper program in C++ so that I can get on to a more familiar language (Java so that’s why I suck on pointers ) and finish the project. I want to thank for all the help in this forum.

    I have now get stuck on the IPC can someone please take a look at this I’m sure it is yeast some lame *** newbie error that I have done wrong.

    This little C++ project is structured like this…
    It has 1 .DLL file and one .EXE file they both import a .h file.
    I now what to communicate between the .DLL and the .EXE file using WM_COPYDATA.

    The error I get when debugging the code is [Unhandled exception in prog.exe (USER32.DLL): 0xC0000005: Access Violation]

    (OBS there is more code then what is listed below but that is not of interest here)

    This is the code in the .h file
    Code:
     
    //The structure that I want to send to the .EXE file
    typedef struct ipcMsg{
    	int msgType;
    	HWND hwnd;
    	TCHAR text[100];
    	TCHAR className[100];
    	HWND parenthwnd;
    } *LPWM_IPCMSG;
    This is the code in the .DLL file
    Code:
     
    //A shared section between the .EXE file and .DLL file
    #pragma data_seg ("shared")
    LPWM_IPCMSG copyDataStruct = NULL;
    TCHAR textOut[MAX_TEXT_LENGTH] = {'\0'};
    CWPSTRUCT *cwp = NULL;
    #pragma data_seg ()
    #pragma comment(linker,"/SECTION:shared,RWS")
    
    //Then there is a function WH_CALLWNDPROC
    //LPARAM in the WH_CALLWNDPROC function points to a CWPSTRUCT
    cwp = (CWPSTRUCT*) lParam;
    //Lets put some data in my structure (lParam is a pointer to a null terminates string that newer is longer that 100 char)
    lstrcat(copyDataStruct->text, (char*) cwp->lParam);
    //Initiating the COPYDATASTRUCT that WM_COPYDATA points to, we set the data field of COPYDATASTRUCT 
    //to point at the copyDataStruct and because it is placed in the shared memory space the .EXE should have access to it (OR???)
    COPYDATASTRUCT data = {0, sizeof(copyDataStruct), (PVOID) copyDataStruct};
    //And then we send it away to the .EXE window
    SendMessage(hObserver, WM_COPYDATA, (WPARAM) hDllInstance, (LPARAM) &data);
    And finally the code in the .EXE file that import the .h file
    Code:
     
    //This code is in the WndProc function
    static LPWM_IPCMSG dataCopy = NULL;
    static HWND hwndList;
    hwndList = CreateWindow(TEXT("listbox"), …Some stuff
    switch(message)
     {
    case WM_COPYDATA:
    //We extract the structure I want to use
    dataCopy = (LPWM_IPCMSG)((COPYDATASTRUCT*)lParam)->lpData;
    //and then we insert the string in the listbox by sending a pointer to a null terminated string, I use the one in the dataCopy structure, this is actually not a 
    //pointer but not sure how to do this
    SendMessage(hwndList[0], LB_INSERTSTRING, 0, (LPARAM) dataCopy->text);
    return TRUE;
    }
    Now, what access am I violating?

  7. #7
    Join Date
    May 2002
    Location
    Phoenix, AZ
    Posts
    95

    Re: IPC from hook DLL to Application

    Just curious,
    But you have placed a pointer ( not an object ) in a shared data segment.
    So all process which map your dll can have a look at the pointer ( and the value will be the same since it is shared ), but if they try to access the pointed at memory it will cause unpredictable results as the contents at the location would be different.

  8. #8
    Join Date
    Feb 2005
    Posts
    42

    Re: IPC from hook DLL to Application

    Thanks Now i see it. Problem fixed.

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