The attached project shows how two separate VB applications can share String Data with each other without using Winsock or an intermediary file on the disk, using API calls to read the memory of the other process.
Printable View
The attached project shows how two separate VB applications can share String Data with each other without using Winsock or an intermediary file on the disk, using API calls to read the memory of the other process.
That is exactally what I am trying to do.
Thank you very much, oh, and Mirrion if he is reading this thread...
I am transfering byte arrays, so I will rewrite some of your code to take into account for this and also will add buffering to the server :D
Will post back laterz...
Woka
For sharing strings my preference would be to use the global atom table. This is a global resource that stores strings and can be accessed from any program in user space without having to worry about cross process memory access.
VB Code:
Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer Declare Function GlobalGetAtomName Lib "kernel32" Alias "GlobalGetAtomNameA" (ByVal nAtom As Integer, ByVal lpBuffer As String, ByVal nSize As Long) As Long Declare Function GlobalDeleteAtom Lib "kernel32" Alias "GlobalDeleteAtom" (ByVal nAtom As Integer) As Integer Declare Function GlobalFindAtom Lib "kernel32" Alias "GlobalFindAtomA" (ByVal lpString As String) As Integer
This is nice man, great code!
Can someone please tell me what UI am doing wrong? Apart from working with computers...:D
The pointer, to the string, and the length of the string, that get put into my udtDataStructure type get reproduced on the server. but from these pointer and length values I cannot get my string :(
Woka
In
VB Code:
Private Function ReadFromProcess(processID As Long, Pointer As Long) As Long Dim hProcess As Long Dim ReadLength As Long Dim udtStruct As DataStruct Dim strBuffer As String hProcess = OpenProcess(PROCESS_VM_READ, False, processID) If hProcess Then Call ReadProcessMemory(hProcess, ByVal Pointer, ByVal VarPtr(udtStruct), LenB(udtStruct), ReadLength) 'After the above line of code the udtDataStructure has the same pointer AND length as the client did. 'But the line of code below returns a blank string EVEN though I am looking at the corrent pointer and the correct length...??? Call ReadProcessMemory(hProcess, ByVal udtStruct.lngPointer, ByVal StrPtr(strBuffer), udtStruct.lngLength, ReadLength) arBuffer = strBuffer Call CloseHandle(hProcess) Form1.Check1.Value = vbChecked ReadFromProcess = 1 End If End Function
The call to Readprocessmemory cannot resize the empty string strBuffer. Try putting th eline:-
before trying to fill the buffer...VB Code:
strBuffer = String$(udtStruct.lngLength,0)
Me = fool, bad Woka *SLAP* [just edited this line since it said mer = fool, it was supposed to say me and not a ref to mirron]
Cheers...
Right, I have modified the code so it now passes varible length strings between apps using byte arrays. The server can now repond with a msg back to the client after it's processed the data :D
Kleinma, you could pass your Command() params using this method :D I would...
Woka
That's pretty good stuff - nice one geezer
Woka: Ur welcome :) Guess its not as fast as the assembly code I suggested :p , but glad to be of some help to you :)
Phrozeman, yrwyddfa : Thanx folks! :)
Duncan: U never cease to amaze me! Where can I find "Duncan's API Bible"? :D Thanx for that tip.
Regards
KayJay
About 95% of it is documented in the EventVB source which you can get here... - the rest is a n unstable mixture of MSDN, Appleman's Win32 Guide and sheer belligerence....Quote:
Where can I find "Duncan's API Bible"?
What assembly language??? :confused:Quote:
Originally posted by KayJay
Woka: Ur welcome :) Guess its not as fast as the assembly code I suggested :p , but glad to be of some help to you :)
Phrozeman, yrwyddfa : Thanx folks! :)
Duncan: U never cease to amaze me! Where can I find "Duncan's API Bible"? :D Thanx for that tip.
Regards
KayJay
I've got belligerence, the rest....:D Thanx for the links. I'll look them up asap.Quote:
Originally posted by MerrionComputin
About 95% of it is documented in the EventVB source which you can get here... - the rest is a n unstable mixture of MSDN, Appleman's Win32 Guide and sheer belligerence....
Woka: from
here :p
Quote:
U could also push the least significant bit 2 bits to the right and pop the semi - significant bit 4 bits to left and the move the most significant bit right out of its memory space! That would speed things even faster
Why would that speed it up?
How much would it speed it up by?
And how hard would it be to add that into my example?
Sorry, working at byte level is not really by forte :(
Woka
Was jus' kidding pal. Byte level movement is not my forte as well.
Grrrrrrrr! :(
I started looking into that last night...Boooooooooooo :D
Hahahahaha...Gotta laf really...:D
Bad KJ *SLAP*
Woka
A super golden sample for me after scrathing my head for weeks with regard to my IPC programs which use custom window messages (instead of WM_COPYDATA) to send string data.
Excellent work, KayJay.
Anyway, can someboy tell me what are those parameters as shown below mean? (such as: ByVal (Pointer - 4), StringLengthInBytes, 4, ReadLength etc)
==========================================
Call ReadProcessMemory(hProcess, ByVal (Pointer - 4), StringLengthInBytes, 4, ReadLength)
ReceiveString = Space(StringLengthInBytes \ 2)
Call ReadProcessMemory(hProcess, ByVal Pointer, ByVal StrPtr(ReceiveString), StringLengthInBytes, ReadLength)
==========================================
Thank you very much cwchan :) Glad to have been of some help.
As to your Q.
hProcess : The other Process's ProcessIDCode:Call ReadProcessMemory(hProcess, ByVal (Pointer - 4), StringLengthInBytes, 4, ReadLength)
Pointer: The memory address from which the values are being read.
ByVal (Pointer - 4) : The length of a string variable is stored as a long just before actual memory address of that variable. We retrieve that to make sure the correct number of bytes and therefore the correct string is read.
StringLengthInBytes : The variable to hold the length of the string
4 : As the length is a Long, therefore, 4 bytes.
ReadLength : A return value to store the actual number of bytes that are read.
So we open the process "hProcess", seek memory address "Pointer - 4", read 4 bytes from that address - a Long - and get the length of the String that we want. We then, using
ensure the variable to hold the string is correctly initialized, as VB stores in Unicode, the actual number of characters in the string is twice when stored in memory. Then we read the process "hProcess" at the memory address "Pointer" exactly the number of bytes that the String contains, i.e. StringLengthInBytes, and move that string to the buffer ReceiveString.Code:ReceiveString = Space(StringLengthInBytes \ 2)
Call ReadProcessMemory(hProcess, ByVal Pointer, ByVal StrPtr(ReceiveString), StringLengthInBytes, ReadLength)
We now have the required String value as stored in the "hProcess"'s memory into a variable "ReceiveString" which resides in the current process's memory.
HTH :)
Regards
Kaushik Janardhanan