For example in Pascal, if I had a library that I’m compiling to DLL:
library Blah;
procedure AddToNum;
begin
Num := Num + 1;
end;
procedure PrintNum;
begin
WriteLN(Num);
end;
Exports AddToNum;
Exports PrintNum;
var
Num: Integer;
begin
Num := 0
end.
Ideally, the user could just call the AddToNum and PrintNum routines and it would do as such. However, you actually have to pass in arguments, and that means the user has to keep track of ALL of the variables I’d be using. What is the ideal method to do this? Pointers or something?
I’m looking for the variable to be the same between functions calls, much like some sort of “global”
Move your DLL code (the actual code that runs) into a separate unit (for instance,
DLLCode.pas), declare the variable at the top of the implementation section, and have your.DPRfile just use that unit. All of the actual code goes inDLLCode.pas, and visibility of the variable follows the normal Pascal scoping rules.Here’s a sample DLL (
DLLSample.dprandDLLCode.pas), and a test console application that uses it. All of the code compiles and properly executes under Delphi 2007 and Windows 7 64-bit.DllSample.dpr:
DllCode.pas:
DllTestApp.dpr:
This outputs:
in a console window and waits for you to hit Enter to close it.