jaulib v1.3.0
Jau Support Library (C++, Java, ..)
WindowsDynamicLinkerImpl.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2013 Gothel Software e.K.
5 * Copyright (c) 2013 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package jau.sys.dl;
27
28public final class WindowsDynamicLinkerImpl extends DynamicLinkerImpl {
29
30 /** Interface to C language function: <br> <code> BOOL FreeLibrary(HANDLE hLibModule); </code> */
31 private static native int FreeLibrary(long hLibModule);
32
33 /** Interface to C language function: <br> <code> DWORD GetLastError(void); </code> */
34 private static native int GetLastError();
35
36 /** Interface to C language function: <br> <code> PROC GetProcAddressA(HANDLE hModule, LPCSTR lpProcName); </code> */
37 private static native long GetProcAddressA(long hModule, java.lang.String lpProcName);
38
39 /** Interface to C language function: <br> <code> HANDLE LoadLibraryW(LPCWSTR lpLibFileName); </code> */
40 private static native long LoadLibraryW(java.lang.String lpLibFileName);
41
42 @Override
43 protected final long openLibraryLocalImpl(final String libraryName) throws SecurityException {
44 // How does that work under Windows ?
45 // Don't know .. so it's an alias to global, for the time being
46 return LoadLibraryW(libraryName);
47 }
48
49 @Override
50 protected final long openLibraryGlobalImpl(final String libraryName) throws SecurityException {
51 return LoadLibraryW(libraryName);
52 }
53
54 @Override
55 protected final long lookupSymbolGlobalImpl(final String symbolName) throws SecurityException {
56 if(DEBUG_LOOKUP) {
57 System.err.println("lookupSymbolGlobal: Not supported on Windows");
58 }
59 // allow DynamicLibraryBundle to continue w/ local libs
60 return 0;
61 }
62
63 private static final int symbolArgAlignment=4; // 4 byte alignment of each argument
64 private static final int symbolMaxArguments=12; // experience ..
65
66 @Override
67 protected final long lookupSymbolLocalImpl(final long libraryHandle, final String symbolName) throws IllegalArgumentException {
68 String _symbolName = symbolName;
69 long addr = GetProcAddressA(libraryHandle, _symbolName);
70 if( 0 == addr ) {
71 // __stdcall hack: try some @nn decorations,
72 // the leading '_' must not be added (same with cdecl)
73 for(int arg=0; 0==addr && arg<=symbolMaxArguments; arg++) {
74 _symbolName = symbolName+"@"+(arg*symbolArgAlignment);
75 addr = GetProcAddressA(libraryHandle, _symbolName);
76 }
77 }
78 return addr;
79 }
80
81 @Override
82 protected final void closeLibraryImpl(final long libraryHandle) throws IllegalArgumentException {
83 FreeLibrary(libraryHandle);
84 }
85
86 @Override
87 public final String getLastError() {
88 final int err = GetLastError();
89 return "Last error: 0x"+Integer.toHexString(err)+" ("+err+")";
90 }
91
92}
final long openLibraryGlobalImpl(final String libraryName)
final long openLibraryLocalImpl(final String libraryName)
final long lookupSymbolLocalImpl(final long libraryHandle, final String symbolName)
final long lookupSymbolGlobalImpl(final String symbolName)
final void closeLibraryImpl(final long libraryHandle)
final String getLastError()
Returns a string containing the last error.
static final boolean DEBUG_LOOKUP