Data exchange by Delphi DLL / WM_COPYDATA

Hello,
I have to write a DLL in Delphi to pass data between two applications by using WM_COPYDATA.
I have following problem: data to be exchanged are represented by couples Name / Value, where value may be an integer or a string, and the number of couples is variable, so I have to use dynamic structures (array, records, ...).
In DLL I have firstly to load all couples of data (from program A) and finally to send a unique data packet to program B (by using WM_COPYDATA).
Do You have some suggestions about data structures to use, considering that programs A and B are written in other languages (not in Delphi) ?
henry60Asked:
Who is Participating?
 
Sinisa VukCommented:

I use function like this to send text (in dll):

function SendSharedData(hwd: Cardinal; const sText: String; iType: Longint;
  const sMemID: String): Boolean;
var
  hMap : THandle;
  pData: Pointer;
  DataStruct: TCopyDataStruct;
begin
  Result := False;

  hMap := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, Length(sText)+1, PChar(sMemID));
  if hMap = 0 then Exit;
  
  pData := MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, Length(sText)+1);
  if pData<>nil then
  begin
    CopyMemory(pData, PChar(sText), Length(sText));
    if hwd<>0 then
    begin
      DataStruct.dwData := iType;
      DataStruct.cbData := Length(sText)+1;
      DataStruct.lpData := pData;
      Result := (SendMessage(hwd, WM_COPYDATA, Longint(Application.Handle), Longint(@DataStruct)) = 0);
    end;
    UnmapViewOfFile(pData);
  end;
  if hMap<>0 then
    CloseHandle(hMap);
end;

Open in new window


Simple call SendSharedData(<win handle to send>, <Text>, C_DATA_ID1, 'UNQ_APP_DATA')

You must know win handle which will receive message, but this can be pass to dll...

...and  to get copy of message (again i Delphi) ....

...
procedure MSGCopyData(var Msg: TWMCopyData); message WM_COPYDATA;
....


procedure TfrmKasa.MSGCopyData(var Msg: TWMCopyData);
var
  sText: String;
begin
  Msg.Result:=0;
  case Msg.CopyDataStruct.dwData of
    C_DATA_ID1: 
      begin
        if Msg.CopyDataStruct.cbData>0 then
        begin
          SetLength(sText, Msg.CopyDataStruct.cbData);
          CopyMemory(PChar(sText), Msg.CopyDataStruct.lpData, Msg.CopyDataStruct.cbData);
          Msg.Result:=1;
        end;
      end;
...

Open in new window

0
 
fatihbarutCommented:
by the way, do you write both of the programs using different languages?
or there is already a program B and you want to send data from the program A which is written by you in delphi.
0
 
henry60Author Commented:
Yes, both the programs A and B are written in languages different from Delphi.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
fatihbarutCommented:
why don't you just use simple txt file?
Write as many lines as you want and let the programs read it?
0
 
Sinisa VukCommented:

You can pass simple in-memory-like ini file to client:

Name1=123<CR><LF>
Name2=ABC<CR><LF>
Name3=2012-01-02<CR><LF>
....
with IPC (interprocess communication - using WM_COPYDATA and passing pointer to memory block) you can mix different prog. languages.
0
 
fatihbarutCommented:
:sinisav
it looks great way to communicate 2 programs.
But could you explain it more?
0
 
Geert GOracle dbaCommented:
I've requested that this question be closed as follows:

Accepted answer: 250 points for sinisav's comment http:/Q_27491393.html#37340551

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
henry60Author Commented:
OK, thank You.
I'm sorry for waiting so much, I've been away for longtime.
0
 
henry60Author Commented:
I'm sorry but I've been away for longtime without possibility to check forum posts.
Sinisav's comment is satisfying, so I ask if it is possible to close question with this comment.
I'm sorry I'm late.
Bye.
0
 
henry60Author Commented:
Helpful solution
0
 
henry60Author Commented:
Helpful solution.
Thank You
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.