Solved

Calling vars from a DLL

Posted on 2003-12-06
4
264 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 115
Run video youtube webbrowse 10 65
TServerSocket - file via sendbuf or Text via sendtext ? 1 66
Tidtcpserver listening on multiports? 1 35
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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