Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable into a DLL

Status
Not open for further replies.

gxydas

Programmer
Jan 15, 2005
76
GR
Hi to all,
I have a DLL that contains a form. I'm trying to pass a single variable to the form of the DLL so it can be displaied in a component on it. Can someone tell how to do this! I'd like to use a procedure instead of a function.
Thanks!
 
This shouldn't be difficult, you just need to add a new procedure to your dll and make sure that it is exported.

e.g.

Code:
procedure ShowVar(aVar: integer); stdcall;

implementation

procedure ShowVar(aVar: integer); stdcall;
begin
  fmLabel.Label1.Caption := 'Value: '+IntToStr(aVar);
end;

As I say, make sure that ShowVar is added to the exports clause in the dll's main unit, and you should then be able to call it from another program.

Steve
 
First of all thanks for answering to my question.
There is a little problem with that...

Here is the procedure that creates the form in the DLL:

Procedure DLLForm1Create; stdcall;
begin
try
Form1:= TForm1.Create(application);
Form1.showModal;
finally
form1.Free;
end;
end;

And here is the event handler that calls the create procedure from another program:

procedure TForm1.Button1Click(Sender: TObject);
var num: integer;
begin
try
num:= StrToInt(Edit1.Text);
except
num:= 0;
end;
DllForm1Create;
VarShow(num);
end;

The problem is that the procedure VarShow is been calling after the destroy of the form in the DLL. I' ve tried to pass the variable into the DLLForm1Create procedure but it did not work.
Thanks alot!
 
There's no reason why you shouldn't be able to pass a parameter into your form creation routine. I just tested the following code and it worked fine, what error were you getting?

In the dll:

Code:
procedure CreateAndShowForm(InitialVar: integer); stdcall;

implementation

procedure CreateAndShowForm(InitialVar: integer); stdcall;
begin
  fmLabel := TfmLabel.Create(nil);
  fmLabel.Label1.Caption := IntToStr(InitialVar);
  fmLabel.ShowModal;
end;

and in the calling program:

Code:
procedure CreateAndShowForm(InitialVar: integer); stdcall; external 'showform.dll';

implementation

procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateAndShowForm(1);
end;

As a little aside, rather than putting your StrToInt statement in a try..except block you can use StrToIntDef, in which you provide a default value for use in case of an error. It basically does the same thing but with one line.

Steve
 
Thanks alot for all!
It worked just fine. The error was that I had the Form1.showModal statement first and then I was calling the VarShow procedure.
Maybe you could explain to me why am I getting another form in windows task bar though I use a modal form.

Thanks for StrToIntDef too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top