Solved

Calling vars from a DLL

Posted on 2003-12-06
4
261 Views
Last Modified: 2010-04-05
Hello, I would Export Vars from a DLL and call them in my programm, example I would export S:String from a Dll and call that in another programm. Could you help me there??
0
Comment
Question by:k4hvd88
  • 2
4 Comments
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
It would be much better practice to export a Function call from the DLL that returns a value of the expected type (e.g., in this case, a string), or to make it a property of a class.

The history of software is strewn with defects caused by the use of "global" shared variables. That's why encapsulation is such a valuable concept.
0
 
LVL 8

Accepted Solution

by:
gmayo earned 180 total points
Comment Utility
An example (DLL code first):

library Project2;

uses
  SysUtils,
  Classes;

{$R *.res}

var
      MyInt : integer = 999;
      MyStr : string = 'Hello, world!';

function GetMyInt : integer; stdcall;
begin
      Result := MyInt;
end;

procedure SetMyInt(v : integer); stdcall;
begin
      MyInt := v;
end;

function GetMyStr : PChar; stdcall;
begin
      Result := PChar(MyStr);
end;

procedure SetMyStr(s : PChar); stdcall;
begin
      MyStr := s;
end;

exports
      GetMyInt,
      SetMyInt,
      GetMyStr,
      SetMyStr;

begin
end.


...and the code to call it:

program Project1;

uses
  Dialogs, SysUtils;

{$R *.res}

const
      MyDLL = 'Project2.dll';

function GetMyInt : integer; stdcall; external MyDLL;
function GetMyStr : PChar; stdcall; external MyDLL;
procedure SetMyInt(v : integer); stdcall; external MyDLL;
procedure SetMyStr(s : PChar); stdcall; external MyDLL;

begin
      ShowMessage(GetMyStr);
      ShowMessage(IntToStr(GetMyInt));
      SetMyStr('Goodbye');
      SetMyInt(123);
      ShowMessage(GetMyStr);
      ShowMessage(IntToStr(GetMyInt));
end.


Geoff M.
0
 

Author Comment

by:k4hvd88
Comment Utility
is there any method to call vars directly?
0
 
LVL 8

Expert Comment

by:gmayo
Comment Utility
I believe not. From Delphi help:

"Global variables declared in a shared library cannot be imported by a Delphi application.

A library can be used by several applications at once, but each application has a copy of the library in its own process space with its own set of global variables. For multiple libraries--or multiple instances of a library--to share memory, they must use memory-mapped files. Refer to the your system documentation for further information."

Geoff M.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now