-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJavaInfoTest.pp
102 lines (90 loc) · 3.38 KB
/
JavaInfoTest.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
{ 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/.
}
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
{$APPTYPE GUI}
program JavaInfoTest;
uses
Windows;
type
TGetDWORD = function(): DWORD; stdcall;
TGetString = function(Buffer: PChar; NumChars: DWORD): DWORD; stdcall;
TIsBinary64Bit = function(FileName: PChar; Is64Bit: PDWORD): DWORD; stdcall;
var
DLLHandle: HMODULE;
IsJavaInstalled: TGetDWORD;
GetJavaHome, GetJavaJVMPath, GetJavaVersion: TGetString;
IsBinary64Bit: TIsBinary64Bit;
JavaHome, JavaBinary, JavaJVMPath, JavaVersion, BinaryType, Msg: string;
Is64Bit: DWORD;
procedure MsgBox(const Msg: string; const MsgBoxType: UINT);
begin
MessageBoxW(0, PChar(Msg), 'JavaInfo.dll Test', MsgBoxType or MB_OK);
end;
function GetString(var Func: TGetString): string;
var
NumChars: DWORD;
OutStr: string;
begin
result := '';
NumChars := Func(nil, 0);
SetLength(OutStr, NumChars);
if Func(PChar(OutStr), NumChars) > 0 then
result := OutStr;
end;
begin
DLLHandle := LoadLibraryW('JavaInfo.dll'); // LPCWSTR lpLibFileName
if DLLHandle = 0 then
ExitCode := GetLastError()
else
ExitCode := 0;
if ExitCode <> 0 then
begin
MsgBox('Unable to load JavaInfo.dll.', MB_ICONERROR);
exit();
end;
GetJavaHome := TGetString(GetProcAddress(DLLHandle, // HMODULE hModule
'GetJavaHome')); // LPCSTR lpProcName
GetJavaJVMPath := TGetString(GetProcAddress(DLLHandle, // HMODULE hModule
'GetJavaJVMPath')); // LPCSTR lpProcName
GetJavaVersion := TGetString(GetProcAddress(DLLHandle, // HMODULE hModule
'GetJavaVersion')); // LPCSTR lpProcName
IsBinary64Bit := TIsBinary64Bit(GetProcAddress(DLLHandle, // HMODULE hModule
'IsBinary64Bit')); // LPCSTR lpProcName
IsJavaInstalled := TGetDWORD(GetProcAddress(DLLHandle, // HMODULE hModule
'IsJavaInstalled')); // LPCSTR lpProcName
if IsJavaInstalled() = 0 then
MsgBox('JavaInfo.dll did not detect a Java installation.', 0)
else
begin
JavaHome := GetString(GetJavaHome);
JavaBinary := JavaHome + '\bin\java.exe';
if IsBinary64Bit(PChar(JavaBinary), @Is64Bit) = 0 then
begin
if Is64Bit = 1 then
BinaryType := '64-bit'
else
BinaryType := '32-bit';
end
else
BinaryType := 'unknown';
JavaJVMPath := GetString(GetJavaJVMPath);
JavaVersion := GetString(GetJavaVersion);
Msg := 'Java home: ' + JavaHome + #10
+ 'jvm.dll path: ' + JavaJVMPath + #10
+ 'Java file version: ' + JavaVersion + #10
+ 'Platform: ' + BinaryType;
MsgBox(Msg, 0);
end;
FreeLibrary(DLLHandle); // HMODULE hLibModule
end.