Results 1 to 17 of 17

Thread: Inter-process Communication

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Inter-process Communication

    I have a DLL made in c++ and a client made in vb.
    The DLL is injected into my game.

    I need to be able to make this dll and my client communicate. I have considered these options:

    WM_COPYDATA
    Winsock

    But I'm not sure about either one of them. I can't use WM_COPYDATA because that is an API call, and API calls must be sent to a WINDOW's HWND, and dll's dont have hwnd's.

    And I'm not even sure if full winsock connections are possible in just a dll, without some kind of exe or something..

    help, someone?

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Inter-process Communication

    When the dll is injected into your app it becomes part of the same process so no inter-process communication is needed.

  3. #3
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Inter-process Communication

    When the dll is injected into your app it becomes part of the same process so no inter-process communication is needed.
    He needs communication between the injected dll and a seperate VB program I think.
    Another way you can handle the communication is with a shared memory segment in the dll, or a file mapped object.
    Take a look at the Subclass external Apps link in my sig for an explanation.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Inter-process Communication

    Quote Originally Posted by moeur
    He needs communication between the injected dll and a seperate VB program I think.
    Another way you can handle the communication is with a shared memory segment in the dll, or a file mapped object.
    Take a look at the Subclass external Apps link in my sig for an explanation.
    Correct. The game and client are two different things. I'll look into the link @ your sig, thanks.

  5. #5
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Inter-process Communication

    There are several ways to communicate between processes
    1. Sendmessage WM_COPYDATA - requires recipient have a window
    2. File Mapping - requires both processes share the same dll
    3. Shared data segment - requires both processes share the same dll, easier than file mapping
    4. OutputDebugString - requires a debugger be attached to the process
    5. Clipboard

    Of these methods I think that the shared data segment is the best for you since you don't have a window in your client process.

    In your dll, setup a shared data segment
    Code:
    #pragma data_seg(".shared")
    //anything declared in here will be shared
    //as long as it's initialized
    //can't share arrays or structures however
    	int X_Coord = 0;
    	int Y_Coord = 0;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.shared,RWS")
    Also create a routine that VB can use to retrieve the data
    Code:
    __declspec(dllexport) int _stdcall GetCoordinates(int* pX, int* pY)
    {
       *pX =  X_Coord;
       *pY = Y_Coord;
       return DataState;
    }
    where DataState is a global variable set by your dll if new data is available, otherwise it's zero
    make sure you have a .def file so VB can see the routine
    Code:
    EXPORTS
    	GetCoordinates  @1
    Now, from VB declare the new function as
    VB Code:
    1. Private Declare Function GetCoordinates Lib "MyDLL.Dll" ( _
    2.     X As Long, _
    3.     Y As Long _
    4. ) as Long
    You'll have to do some polling to get these values
    VB Code:
    1. Function PollForXY(X As Long, Y As Long) As Boolean
    2. 'StopPolling is a global variable used
    3. 'to stop the polling process from else-
    4. 'where in the code
    5.  Do
    6.   DoEvents
    7.   If StopPolling Then
    8.     PollForXY = False
    9.     Exit Do
    10.   End If
    11.  While GetCoordinates(X, Y) = 0
    12.  PollForXY = True
    13. End Function
    get it?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Inter-process Communication

    Thanks a lot, I will try this when I get the time.

    The only thing I don't understand is "polling"

    What does polling mean? How would i use it?
    What would happen if i didnt use it?

    Thanks.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Inter-process Communication

    Wait a second. I just thought about this for a second and realized it won't work.

    Did u not know that my dll is injected into my game? In order to get my coordinates I would have to call a function from that specificly loaded dll. There is no way to do this in vb.

    So I might as well not attempt to export any kinds of definitions list, because it is not possible to call a function from a specific loaded dll.

    Couldnt i somehow send a message like "#getcoords", the dll will get that message and parse it, then it will send back "#coords" and then my coords?

  8. #8
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Inter-process Communication

    Wait a second. I just thought about this for a second and realized it won't work.
    Think about it a little more.
    It will work. You don't have to worry about what process the injected dll is running in, they will all share the data with each other through the technique I outlined.
    Couldnt i somehow send a message like "#getcoords", the dll will get that message and parse it, then it will send back "#coords" and then my coords?
    You could do this if your client had a window to send messages to, but the shared data method is easier.
    What does polling mean? How would i use it?
    It's just a fancy word for checking to see if new data is present like I did in the VB code above.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Inter-process Communication

    I'm not sure about what to put in the segment though

    I have a question, is there a limit as to how much data I can put in that segment?

    Also, do the variables already have to be set? Like can I still be able to set the variable even if its in the shared segment, or will I have to re-share it.. sorry if Im not specific enough.

    And about the polling. Would I just put it in a timer until it turns true, then that means that some data has changed? How would I go about checking wich data was changed?

    sorry if I ask too many questions

  10. #10
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Inter-process Communication

    Make a list of all the variables you want to share. I thought it was just X and Y, but no problem if it is a lot more.
    Once this is done, then we can set up the segment and the polling.
    Also, do the variables already have to be set?
    They have to be given initial values, but then you can change them anytime you want.
    And about the polling. Would I just put it in a timer until it turns true, then that means that some data has changed?
    that is one way of doing it, yes.
    sorry if I ask too many questions
    I think that's what this forum is for.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Inter-process Communication

    Its X, Y, Z, Xrotation, Yrotation, Zrotation, and animation - for 32 players.
    Plus 6 lines of chat.. You do the math

    Is this:

    VB Code:
    1. Private Declare Function GetCoordinates Lib "MyDLL.Dll" ( _
    2.     X As Long, _
    3.     Y As Long _
    4. ) as Long

    The only way to access the shared data?

  12. #12
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Inter-process Communication

    OK, so in that case you probably want to set up a data structure array. Then you will want to use file mapping.
    First set up you structure
    Code:
    #include <windows.h> 
    #include <memory.h> 
    
    //define your structure
    #define MAX_PLAYERS 32;
    
    struct sGameData{
    	int X;
    	int Y;
    	int Z;
    	int Xrot;
    	int Yrot;
    	sANIMATION animation;//whatever you need here
    };
    
    sGameData* GameData;			//pointer to your structure
    Now edit your dll entry point to add the file mapping stuff
    Code:
    static LPVOID lpvMem = NULL;      // pointer to shared memory
    static HANDLE hMapObject = NULL;  // handle to file mapping
    
    
    // The DLL entry-point function sets up shared 
    // memory using a named file-mapping object. 
    
     
    #define SHMEMSIZE 4096 
     
     
    BOOL DllMain(HINSTANCE hinstDLL,  // DLL module handle
        DWORD fdwReason,              // reason called 
        LPVOID lpvReserved)           // reserved 
    { 
        BOOL fInit, fIgnore; 
     
        switch (fdwReason) 
        { 
            // The DLL is loading due to process 
            // initialization or a call to LoadLibrary. 
     
              case DLL_PROCESS_ATTACH: 
     
                // Create a named file mapping object.
     
                hMapObject = CreateFileMapping( 
                    INVALID_HANDLE_VALUE, // use paging file
                    NULL,                 // default security attributes
                    PAGE_READWRITE,       // read/write access
                    0,                    // size: high 32-bits
                    SHMEMSIZE,            // size: low 32-bits
                    "dllmemfilemap");     // name of map object
                if (hMapObject == NULL) 
                    return FALSE; 
     
                // The first process to attach initializes memory.
     
                fInit = (GetLastError() != ERROR_ALREADY_EXISTS); 
     
                // Get a pointer to the file-mapped shared memory.
     
                lpvMem = MapViewOfFile( 
                    hMapObject,     // object to map view of
                    FILE_MAP_WRITE, // read/write access
                    0,              // high offset:  map from
                    0,              // low offset:   beginning
                    0);             // default: map entire file
                if (lpvMem == NULL) 
                    return FALSE; 
     
                // Initialize memory if this is the first process.
     
                if (fInit) 
                    memset(lpvMem, '\0', SHMEMSIZE); 
    			
    			//*** set your data pointer here
    			GameData = (sGameData*) lpvMem;
    
                break; 
     
            // The attached process creates a new thread. 
     
            case DLL_THREAD_ATTACH: 
                break; 
     
            // The thread of the attached process terminates.
     
            case DLL_THREAD_DETACH: 
                break; 
     
            // The DLL is unloading from a process due to 
            // process termination or a call to FreeLibrary. 
     
            case DLL_PROCESS_DETACH: 
     
                // Unmap shared memory from the process's address space.
     
                fIgnore = UnmapViewOfFile(lpvMem); 
     
                // Close the process's handle to the file-mapping object.
     
                fIgnore = CloseHandle(hMapObject); 
     
                break; 
     
            default: 
              break; 
         } 
     
        return TRUE; 
        UNREFERENCED_PARAMETER(hinstDLL); 
        UNREFERENCED_PARAMETER(lpvReserved); 
    }
    Finally add function(s) to access the data
    Code:
    //-------------------------------------------------------------------------
    //   These are the exported functions which are called by the VB Apps
    //-------------------------------------------------------------------------
    MAINHOOK_API void _stdcall GetSharedData(sGameData pGameData, int Player)
    {
    	pGameData = GameData[Player];
    
    }
    don't forget to create a .def file for the exported functions
    GetSharedData @1

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Inter-process Communication

    LINK : warning LNK4039: section ".shared" specified with /SECTION option does not exist

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Inter-process Communication

    Nvm I got it to work but now VB is saying "Cannot Find Entry Point 'GetCoords' in test.dll" but I know I exported GetCoords in a definition file. ?

  15. #15
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Inter-process Communication

    Show me the .def file and your GetCoords declaration.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Inter-process Communication

    Okay I have gotten VB to successfully read player information.

    Now how to make the VB communicate with the DLL? The vb client will get other players coords and it will need to send them to the dll, is this possible or is this method one-way only?

  17. #17
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Inter-process Communication

    It's two way
    Code:
    MAINHOOK_API void _stdcall SetSharedData(sGameData pGameData, int Player)
    {
    	GameData[Player] = pGameData;
    
    }

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