|
-
Mar 6th, 2005, 05:19 AM
#1
Thread Starter
Addicted Member
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?
-
Mar 6th, 2005, 06:36 AM
#2
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.
-
Mar 6th, 2005, 12:07 PM
#3
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.
-
Mar 6th, 2005, 09:51 PM
#4
Thread Starter
Addicted Member
Re: Inter-process Communication
 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.
-
Mar 7th, 2005, 01:08 AM
#5
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:
Private Declare Function GetCoordinates Lib "MyDLL.Dll" ( _
X As Long, _
Y As Long _
) as Long
You'll have to do some polling to get these values
VB Code:
Function PollForXY(X As Long, Y As Long) As Boolean
'StopPolling is a global variable used
'to stop the polling process from else-
'where in the code
Do
DoEvents
If StopPolling Then
PollForXY = False
Exit Do
End If
While GetCoordinates(X, Y) = 0
PollForXY = True
End Function
get it?
-
Mar 7th, 2005, 07:08 PM
#6
Thread Starter
Addicted Member
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.
-
Mar 7th, 2005, 07:24 PM
#7
Thread Starter
Addicted Member
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?
-
Mar 7th, 2005, 08:28 PM
#8
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.
-
Mar 7th, 2005, 10:11 PM
#9
Thread Starter
Addicted Member
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
-
Mar 7th, 2005, 11:35 PM
#10
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.
-
Mar 8th, 2005, 10:14 PM
#11
Thread Starter
Addicted Member
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:
Private Declare Function GetCoordinates Lib "MyDLL.Dll" ( _
X As Long, _
Y As Long _
) as Long
The only way to access the shared data?
-
Mar 8th, 2005, 11:50 PM
#12
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
-
Mar 9th, 2005, 07:35 PM
#13
Thread Starter
Addicted Member
Re: Inter-process Communication
LINK : warning LNK4039: section ".shared" specified with /SECTION option does not exist
-
Mar 9th, 2005, 07:41 PM
#14
Thread Starter
Addicted Member
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. ?
-
Mar 9th, 2005, 11:14 PM
#15
Re: Inter-process Communication
Show me the .def file and your GetCoords declaration.
-
Mar 10th, 2005, 09:23 AM
#16
Thread Starter
Addicted Member
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?
-
Mar 10th, 2005, 09:34 AM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|