Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21297

How do I transfer in-memory data from one program to another easilly?

$
0
0
I've created these two test programs. Basic operation is this.

One defines a Long type variable "a" and assigns it a value.
Then the second one is run and it establishes a TCP connection to the first (they both are using Winsock for the initial connection).
Then the first one uses VarPtr to get the address of the variable "a" and sends this memory address over the TCP connection to the second program.
Then the second program attempts to directly access the value stored in the variable "a" from the first program, using the CopyMemory API command.
But then that second program crashes (as in it completely crashes the VB6 IDE that the program was running from, just as if I'd given CopyMemory an invalid memory address, but it's VERY valid, as it was taken directly from the first program).

Here is the code for the first program.
Code:

Dim a As Long
Private Sub Form_Load()
a = 12345
Server.Listen
End Sub

Private Sub Server_ConnectionRequest(ByVal requestID As Long)
Server.Close
Server.Accept requestID
End Sub

Private Sub Server_DataArrival(ByVal bytesTotal As Long)
Dim Text As String
Server.GetData Text
If Text = "Ready" Then Server.SendData VarPtr(a)
End Sub

Here's the code for the second program.
Code:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Client_Connect()
Client.SendData "Ready"
End Sub

Private Sub Client_DataArrival(ByVal bytesTotal As Long)
Dim Address As Long
Dim Value As Long
Client.GetData Value
CopyMemory Value, ByVal Address, 4
MsgBox Value
End Sub

Private Sub Form_Load()
Client.Connect "127.0.0.1", 12000
End Sub

Using memory like this so that the programs could share memory addresses would be faster for copying large quantities of data like raw pixel data from pictures, than it would be to send the all the data over TCP. TCP should just be used to share the numbers that are the memory addresses. This way I could have a collection of image processing tools, each its own program (exe file) but they could ALL have access to the same image data in memory. For example I could have imageprocessor.exe and separately imageloaderplugin.exe, and the first program would run the second, the second program would load the image, and store it in memory, and then the first program would get the image by reading the raw pixel data that was stored in memory by the first program.

I don't understand why it isn't working. I don't know why VB6 keeps crashing. Also, if there's an easier way to accomplish sharing of data between programs (without the saving of a temporary file), then please let me know.

Viewing all articles
Browse latest Browse all 21297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>