Results 1 to 17 of 17

Thread: VB - API - Memory - Project - Inter Process String Transfer

  1. #1

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    VB - API - Memory - Project - Inter Process String Transfer

    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.
    Attached Files Attached Files

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  2. #2
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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

    Will post back laterz...

    Woka

  3. #3
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    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:
    1. Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
    2.  
    3. Declare Function GlobalGetAtomName Lib "kernel32" Alias "GlobalGetAtomNameA" (ByVal nAtom As Integer, ByVal lpBuffer As String, ByVal nSize As Long) As Long
    4.  
    5. Declare Function GlobalDeleteAtom Lib "kernel32" Alias "GlobalDeleteAtom" (ByVal nAtom As Integer) As Integer
    6.  
    7. Declare Function GlobalFindAtom Lib "kernel32" Alias "GlobalFindAtomA" (ByVal lpString As String) As Integer
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  4. #4
    Hyperactive Member phrozeman's Avatar
    Join Date
    Apr 2000
    Location
    Netherlands
    Posts
    442
    This is nice man, great code!
    There's a certain mystique when I speak, that you notice that it's sorta unique, cause you know it's me

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Can someone please tell me what UI am doing wrong? Apart from working with computers...

    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
    Attached Files Attached Files

  6. #6
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    In
    VB Code:
    1. Private Function ReadFromProcess(processID As Long, Pointer As Long) As Long
    2. Dim hProcess            As Long
    3. Dim ReadLength          As Long
    4. Dim udtStruct           As DataStruct
    5. Dim strBuffer           As String
    6.     hProcess = OpenProcess(PROCESS_VM_READ, False, processID)
    7.     If hProcess Then
    8.         Call ReadProcessMemory(hProcess, ByVal Pointer, ByVal VarPtr(udtStruct), LenB(udtStruct), ReadLength)
    9.         'After the above line of code the udtDataStructure has the same pointer AND length as the client did.
    10.         'But the line of code below returns a blank string EVEN though I am looking at the corrent pointer and the correct length...???
    11.         Call ReadProcessMemory(hProcess, ByVal udtStruct.lngPointer, ByVal StrPtr(strBuffer), udtStruct.lngLength, ReadLength)
    12.         arBuffer = strBuffer
    13.         Call CloseHandle(hProcess)
    14.         Form1.Check1.Value = vbChecked
    15.         ReadFromProcess = 1
    16.     End If
    17. End Function

    The call to Readprocessmemory cannot resize the empty string strBuffer. Try putting th eline:-
    VB Code:
    1. strBuffer = String$(udtStruct.lngLength,0)
    before trying to fill the buffer...
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  7. #7
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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

    Kleinma, you could pass your Command() params using this method I would...

    Woka
    Attached Files Attached Files

  8. #8
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    That's pretty good stuff - nice one geezer

  9. #9

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Woka: Ur welcome Guess its not as fast as the assembly code I suggested , 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"? Thanx for that tip.

    Regards

    KayJay

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  10. #10
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Where can I find "Duncan's API Bible"?
    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....
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  11. #11
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by KayJay
    Woka: Ur welcome Guess its not as fast as the assembly code I suggested , 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"? Thanx for that tip.

    Regards

    KayJay
    What assembly language???

  12. #12

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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....
    I've got belligerence, the rest.... Thanx for the links. I'll look them up asap.

    Woka: from
    here
    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

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  13. #13
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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

  14. #14

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Was jus' kidding pal. Byte level movement is not my forte as well.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  15. #15

  16. #16
    New Member
    Join Date
    Sep 2003
    Location
    Malaysia
    Posts
    3

    Talking

    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)

    ==========================================
    Last edited by cwchan; Sep 14th, 2003 at 10:50 PM.

  17. #17

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Thank you very much cwchan Glad to have been of some help.

    As to your Q.
    Code:
    Call ReadProcessMemory(hProcess, ByVal (Pointer - 4), StringLengthInBytes, 4, ReadLength)
    hProcess : The other Process's ProcessID

    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
    Code:
    ReceiveString = Space(StringLengthInBytes \ 2)
    
    Call ReadProcessMemory(hProcess, ByVal Pointer, ByVal StrPtr(ReceiveString), StringLengthInBytes, ReadLength)
    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.

    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

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

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