1

I need to send a short string, (less than 30 bytes, but sent every second), from one VB application, to a Delphi application.. is this possible, using CopyDataStruct, WM_COPYDATA and SendMessage functions in Windows?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Fred
  • 21
  • 2

2 Answers2

3

I would say that WM_COPYDATA is the perfect way to do this. You just need to get your Delphi main form, say, to implement a message handler for WM_COPYDATA.

At the Delphi end it looks something like this:

TMyMainForm = class(TForm)
protected
  procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;
end;

procedure TMyMainForm.WMCopyData(var Msg: TWMCopyData);
begin
  //do something with Msg.lpData
end;

Your VB code will need to obtain the window handle of your Delphi main form.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • hello and thanks for help.. I need to ask how to get the Handle of the receiving program.. in VB I can use Me.Handle for the sending app, but it seems that whatever I enter as the Receiver Handle, I will get no result.. hwnd gives a long number, e.g 4759178910518738944, that changes for every time the command is sent.. have also tried to manually enter the Delphi window Handle into VB.. the receiver PID i get by using "pid = Shell(C:\Receive.exe, vbNormalFocus)".. I get no result when trying VB to Delphi, but VB to VB is OK, and Delphi to Delphi is also OK.. what am I doing wrong? :-) – Fred Apr 05 '11 at 11:49
  • Call FindWindowEx passing the Delphi window caption and window class name. You can find this out from Spy++. Or call EnumWindows to enumerate the top level windows - your's will be one of these. – David Heffernan Apr 05 '11 at 12:06
  • Dim iHwnd As Long Dim SS As String = "Test String less than 30 Char" Dim ls As Integer = Len(SS) Dim cds As CopyDataStruct = New CopyDataStruct With {.dwData = 0, .cbData = ls, .lpData = SS} iHwnd = FindWindow(0&, "Receive") SendMessage(iHwnd, &H4A, Me.Handle, cds) this is some of the VB code I use.. I have a Delphi program that sends a CopyDataStruct to the "Receive" program, which is working fine, but using VB to send the same string is not successful so far.. anything specific I need to do? – Fred Apr 05 '11 at 12:13
  • this sounds like a different question. Hard to answer in comments. Ask a new question. – David Heffernan Apr 05 '11 at 12:37
0

This sounds like the kind of thing that you would use DDE to accomplish. Another way would be to write a string into a temporary area in the registry and then call the other program to read it and delete the temporary registry key once finished. You could also pass the string as a parameter in the command line and just exec the program.