-
Notifications
You must be signed in to change notification settings - Fork 3
/
JavaInfo.pp
183 lines (161 loc) · 5.41 KB
/
JavaInfo.pp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{ Copyright (C) 2020-2023 by Bill Stewart (bstewart at iname.com)
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
}
{
Note Regarding the GetJavaHome(), GetJavaJVMPath(), and GetJavaVersion()
Functions
========================================================================
You must allocate a buffer to get the result strings from these functions. To
determine the required buffer size, call the function with a null pointer for
the first parameter and 0 for the second parameter. The function will return
the number of characters required for the buffer, not including the
terminating null character. Allocate the buffer (don't forget to account for
the terminating null character), then call the function again.
// Example using WideChar pointer
function GetJavaHomeDir(): UnicodeString;
var
NumChars, BufSize: DWORD;
OutStr: PWideChar;
begin
result := '';
// First call: Get number of characters needed
NumChars := GetJavaHome(nil, 0);
// Specify buffer size (+ space for null terminator character)
BufSize := NumChars * SizeOf(WideChar) + SizeOf(WideChar);
// Allocate the buffer
GetMem(OutStr, BufSize);
// Second call: Get the string
if GetJavaHome(OutStr, NumChars) > 0 then
result := OutStr;
FreeMem(OutStr);
end;
// Example using UnicodeString
function GetJavaHomeDir(): UnicodeString;
var
NumChars: DWORD;
OutStr: UnicodeString;
begin
result := '';
// First call: Get number of characters needed
NumChars := GetJavaHome(nil, 0);
// Allocate space for the string (accounts for null terminator)
SetLength(OutStr, NumChars);
// Second call: Get the string
if GetJavaHome(PWideChar(OutStr), NumChars) > 0 then
result := OutStr;
end;
}
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
{$R *.res}
library JavaInfo;
uses
Windows,
wsJavaInfo;
type
TStringFunction = function(): string;
// Copies Source to Dest.
procedure CopyString(const Source: string; Dest: PChar);
var
NumChars: DWORD;
begin
NumChars := Length(Source);
Move(Source[1], Dest^, NumChars * SizeOf(Char));
Dest[NumChars] := #0;
end;
// First parameter is address of string function you want to call. Returns
// number of characters needed for output buffer, not including the terminating
// null character.
function GetString(var StringFunction: TStringFunction; Buffer: PChar; const NumChars: DWORD): DWORD;
var
OutStr: string;
begin
OutStr := StringFunction();
if (Length(OutStr) > 0) and Assigned(Buffer) and (NumChars >= Length(OutStr)) then
CopyString(OutStr, Buffer);
result := Length(OutStr);
end;
// Gets Java home directory into string buffer pointed to by PathName.
function GetJavaHome(PathName: PChar; NumChars: DWORD): DWORD; stdcall;
var
StringFunction: TStringFunction;
begin
StringFunction := @wsGetJavaHome;
result := GetString(StringFunction, PathName, NumChars);
end;
// Gets path of jvm.dll into string buffer pointed to by PathName.
function GetJavaJVMPath(PathName: PChar; NumChars: DWORD): DWORD; stdcall;
var
StringFunction: TStringFunction;
begin
StringFunction := @wsGetJavaJVMPath;
result := GetString(StringFunction, PathName, NumChars);
end;
// Gets Java version string (a.b.c.d) into string buffer pointed to by Version.
function GetJavaVersion(Version: PChar; NumChars: DWORD): DWORD; stdcall;
var
StringFunction: TStringFunction;
begin
StringFunction := @wsGetJavaVersion;
result := GetString(StringFunction, Version, NumChars);
end;
// Retrieves whether a specified binary is 64-bit or not to the Is64Bit
// parameter. Returns 0 for success, or non-zero for failure. If successful,
// value pointed to by Is64Bit will be 0 if not 64-bit, or 1 otherwise.
function IsBinary64Bit(FileName: PChar; Is64Bit: PDWORD): DWORD; stdcall;
var
Is64: Boolean;
begin
result := wsIsBinary64Bit(FileName, Is64);
if result = 0 then
begin
if Is64 then
Is64Bit^ := 1
else
Is64Bit^ := 0;
end;
end;
// Returns 1 if Java installation detected or 0 otherwise.
function IsJavaInstalled(): DWORD; stdcall;
begin
if wsIsJavaInstalled() then
result := 1
else
result := 0;
end;
// Retrieves whether the installed Java version is at least the specified
// version to the VersionOK parameter. Returns 0 for success, or non-zero for
// failure. If successful, the value pointed to by VersionOK will be 1 if the
// installed Java version is at least the specified version, or 0 otherwise.
function IsJavaMinimumVersion(Version: PChar; VersionOK: PDWORD): DWORD; stdcall;
var
IsOK: Boolean;
begin
if wsIsJavaMinimumVersion(Version, IsOK) then
begin
result := ERROR_SUCCESS;
if IsOK then
VersionOK^ := 1
else
VersionOK^ := 0;
end
else
result := ERROR_INVALID_PARAMETER;
end;
exports
GetJavaHome,
GetJavaJVMPath,
GetJavaVersion,
IsBinary64Bit,
IsJavaInstalled,
IsJavaMinimumVersion;
end.