Solved

Calling vars from a DLL

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
Connection between libmysql.dll and MySQL Versions 7 130
Dynamically Created Query 3 78
IP without any Dots 1 95
Way to create an iPhone app for my customers 8 87
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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