-
Notifications
You must be signed in to change notification settings - Fork 35
/
unit1.pas
114 lines (90 loc) · 2.12 KB
/
unit1.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
status: TMemo;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
DynLibs;
{ TForm1 }
type
TProc = procedure(); cdecl;
TIntFunc = function(): int32; cdecl;
var
h: TLibHandle;
PrintBye: TProc;
GetIntFromDLL: TIntFunc;
const
GO_LIB = 'goDLL.dll';
PROC1 = 'PrintBye';
PROC2 = 'GetIntFromDLL';
procedure StatusLn(s: string); overload;
begin
Form1.Status.Lines.Add(s);
end;
procedure StatusLn(s: string; i: integer); overload;
begin
StatusLn(s + inttostr(i));
end;
function LoadLib: boolean;
var p: pointer;
begin
h := LoadLibrary(GO_LIB);
if h = NilHandle then result := false else result := true;
StatusLn('Handle: ', h);
p := GetProcAddress(h, PROC1);
if p = nil then begin
StatusLn('Error getting procedure address: '+ PROC1);
result := false;
end;
PrintBye := TProc(p);
p := nil;
p := GetProcAddress(h, PROC2);
if p = nil then begin
StatusLn('Error getting procedure address: '+ PROC2);
result := false;
end;
GetIntFromDLL := TIntFunc(p);
end;
procedure UnloadLib;
begin
UnloadLibrary(h);
end;
procedure TryGoDLL;
var
c: char;
i: integer;
begin
StatusLn('Loading library: '+ GO_LIB);
if LoadLib then begin
i := GetIntFromDLL();
StatusLn('Integer from DLL: ', i);
end else begin
StatusLn('Error loading library or proc address not found in: '+ GO_LIB);
end;
// PROBLEM: can't unload library because it crashes the program?
// only can unload it at the end of the program.
// Go runtime conflicting with fpc?
// Maybe this is it: https://github.com/golang/go/issues/11100
// StatusLn('Unloading library: '+ GO_LIB);
// UnloadLib;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TryGoDLL;
end;
end.