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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
fatihbarutCommented:
why don't you just use simple txt file?
Write as many lines as you want and let the programs read it?
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Sinisa VukSoftware architectCommented:

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
Sinisa VukSoftware architectCommented:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.