[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Calling vars from a DLL

Posted on 2003-12-06
4
Medium Priority
?
272 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
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 540 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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