Board index » delphi » Passing data from dialog in .DLL back to the MainForm

Passing data from dialog in .DLL back to the MainForm

Hello,

One of my dialog forms reside in a .DLL - I export a function ShowDialog and
pass THandle to that dialog... I can pass some more parameters there, but I
wonder how to pass a string (or any other object) BACK from DLL to the
MainForm?

Any advice?

Thanks,
Vsevolod

 

Re:Passing data from dialog in .DLL back to the MainForm


Quote
In article <3ba0cb9f_2@dnews>, Vsevolod Ukrainsky wrote:
> One of my dialog forms reside in a .DLL - I export a function ShowDialog and
> pass THandle to that dialog... I can pass some more parameters there, but I
> wonder how to pass a string (or any other object) BACK from DLL to the
> MainForm?

Depends on how the DLL is to be used. If you only want to use it with Delphi apps
you could add ShareMem to the DLL and host apps Uses clauses (in the DPR files,
as the very first unit) and use Var parameters of type String, or String as
function result. If the procedure  needs to be usable with non-Delphi host apps
you need two value parameters to return a string: a Pchar and an integer.
The caller passes the address of a buffer it has allocated in the Pchar and the
size of the buffer in the integer. The DLL function then uses StrLCopy or
StrPLCopy to copy the contents of a String to this buffer. This is the way
the API does it.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:Passing data from dialog in .DLL back to the MainForm


Quote

> Depends on how the DLL is to be used. If you only want to use it with
Delphi apps
> you could add ShareMem to the DLL and host apps Uses clauses (in the DPR
files,
> as the very first unit) and use Var parameters of type String, or String
as
> function result. If the procedure  needs to be usable with non-Delphi host
apps
> you need two value parameters to return a string: a Pchar and an integer.
> The caller passes the address of a buffer it has allocated in the Pchar
and the
> size of the buffer in the integer. The DLL function then uses StrLCopy or
> StrPLCopy to copy the contents of a String to this buffer. This is the way
> the API does it.

I wrote several DLL files to use with COBOL and DELPHI program. I don't use
the ShareMem unit and allways use pchar, shortstring, integer and boolean
with no problems. Well, I never returned values in pchar, just in the other
tree types, but I do it just using var and out in the parameter
declarations.

Thinking again, I remember to had problems using pchar as var and out
parameter. The reason should be that I didn't make the changes in the pchar
as you wrote above.

But if you don't need to have text greather than 255 caracters I recomend
the use of shortstrings, thus making your program more simple.

Moises

Re:Passing data from dialog in .DLL back to the MainForm


Hi Vsevolod,

you may send a result data to the MainForm with a WM_COPYDATA message.

For example:

DLL:
-----

procedure YourDLLFunction;
var
   buffer: array[0..255] of char;
   Data: TCopyDataStruct;
   Hd   : HWND;
   YourResultString : String;
begin

    YourResultString:='Hello World';

    Hd:=FindWindow('TYourMainForm', nil);
    if Hd<>0 then begin
       StrPCopy(@buffer, YourResultString);
       Data.dwData := 0;
       Data.cbData := 255;
       Data.lpData :=@buffer;
       SendMessage(Hd, WM_COPYDATA,  0, LongInt(@Data));
    end;
end;

MainForm:
-----------

type
    TYourMainForm = class(TForm);
....
private
    procedure WmCopyData(var Message: TMessage); message WM_COPYDATA;

...
implementation

procedure TYourMainForm.WmCopyData(var Message: TMessage); message
WM_COPYDATA;
begin

ResultStringFromTheDLL:=String(PChar(PCopyDataStruct(Message.LParam)^.lpData
));
end;

Regards
Juri.

"Vsevolod Ukrainsky" <vsevo...@unforgettable.com> schrieb im Newsbeitrag
news:3ba0cb9f_2@dnews...

Quote
> Hello,

> One of my dialog forms reside in a .DLL - I export a function ShowDialog
and
> pass THandle to that dialog... I can pass some more parameters there, but
I
> wonder how to pass a string (or any other object) BACK from DLL to the
> MainForm?

> Any advice?

> Thanks,
> Vsevolod

Re:Passing data from dialog in .DLL back to the MainForm


Quote
In article <3ba1006d_1@dnews>, Moises wrote:
> But if you don't need to have text greather than 255 caracters I recomend
> the use of shortstrings, thus making your program more simple.

But the shortstring type is not something other languages will
support directly, so you have to have code in the program that uses the DLL
to correctly interpret the characters (dealing with the length byte) and
to correctly set up the length byte if data is to be passed to the DLL using
a shortstring parameter.

Any programming language that can call Windows API functions will be able to
deal with the data types used in the API, and for character data that is PChar.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:Passing data from dialog in .DLL back to the MainForm


Quote
> Any programming language that can call Windows API functions will be able
to
> deal with the data types used in the API, and for character data that is

PChar.

That's true, but in some cases (mine is one), the shortstring is more simple
to implement than pchar. I'm using Net Express cobol, where I can define a
very simple structure to recive the shortstring. This structure gives me the
size os the shortstring an it's contents in different variables at once. The
problem with pchar in cobol is that I need to define a max size when
declaring the variable, then search for a "low-value" (x00) to get it's
size. My "cobol life" made me write that about shortstring.

Moiss

Other Threads