Solved

Calling vars from a DLL

Posted on 2003-12-06
4
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Expert Comment

by:sftweng
ID: 9888397
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
ID: 9888439
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
ID: 9888542
is there any method to call vars directly?
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9888572
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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