Fred
Fred

Reputation: 21

Send text from VB to Delphi apps, using SendMessage

) I am trying to send a short text from a VB app to Delphi app.. here is the VB Code: Sender Program "Sender"

Public Class SendData

 Const WM_COPYDATA = &H4A
 Public Structure CopyDataStruct
    Public dwData As Integer
    Public cbData As Integer
    Public lpData As String
 End Structure

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
 (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ 
 (ByVal hWnd As Long,  ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As _
 CopyDataStruct) As Long

Private Sub SendData(ByVal cds)
    Dim iHwnd As Long
    Dim SS As String = "Test String less than 30 Char"
    Dim cds As CopyDataStruct
    cds.dwData = 0
    cds.cbData = Len(SS)
    cds.lpData = SS
    iHwnd = FindWindow(0&, "Receive")
    SendMessage(iHwnd, &H4A, Me.Handle, cds) 
End Sub

here is the Delphi Code: Receiver program "Receive"

 procedure TForm1.HandleCopyDataString(copyDataStruct: PCopyDataStruct);
 var
  s : string;
 begin
  s := PChar(CopyDataStruct.lpData);
  cdMemo.Lines.Add(Format('Received data "%s" at %s',[s, TimeToStr(Now)]));
 end;

 procedure TForm1.WMCopyData(var Msg: TWMCopyData) ;
 var
  s : string;
  sText: array[0..255] of Char;
  copyDataType : TCopyDataType;
 begin
  copyDataType := TCopyDataType(Msg.CopyDataStruct.dwData);
  s := PChar(Msg.CopyDataStruct.dwData);
  Form1.cdMemo.Lines.Add(Format('Data from: %d',[msg.From]));
  HandleCopyDataString(Msg.CopyDataStruct);
  case Msg.CopyDataStruct.dwData of 0: //we are being sent a string
  begin
    StrLCopy(sText, Msg.CopyDataStruct.lpData, Msg.CopyDataStruct.cbData);
    Form1.Label1.Caption := sText;
  end;
end;
end;

What am I doing wrong here? It is possible to send strings from VB to Delphi programs using WM_COPYDATA command, and SendMessage function?

please help me :-)

F

Upvotes: 1

Views: 1407

Answers (1)

Rob Kennedy
Rob Kennedy

Reputation: 163287

There are a few things wrong with your Delphi code.

  1. The dwData field holds an integer, but you type-cast it to PChar, a pointer, and then assign it to your string. That's not the field where you stored your string data. That's lpData.

  2. The string you pass is not null-terminated. The OS only promises to copy exactly as many bytes as you specify in the cbData field. That's not necessarily a problem, but you need to be aware of it when you read the string later. To assign s to hold the string copied from the other process, use SetString like this:

    SetString(s, PAnsiChar(Msg.CopyDataStruct.lpData), Msg.CopyDataStruct.cbData);
    
  3. You haven't shown what TCopyDataType is, but if it's anything other than an integer or integer-subrange type, you're using it wrong. The dwData field is already a DWord, so you can use it wherever a numeric value is expected.

  4. You're calling StrLCopy wrong. The third parameter should be the size of the destination buffer, not the source. It's meant to prevent buffer overflows by not copying more characters than will fit in the destination. The function expects to be able to detect the size of the source buffer by finding the terminating null character (but we already established that that won't be available). You could fix it like this:

    StrLCopy(sText, Msg.CopyDataStruct.lpData,
      Min(Length(sText), Msg.CopyDataStruct.cbData));
    

    (Min is in the Math unit.)

Upvotes: 5

Related Questions