Skip to content

Commit 893d00a

Browse files
committed
8346388: Cannot use DllMain in libawt for static builds
Reviewed-by: aivanov, prr
1 parent 1c7641d commit 893d00a

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

src/java.desktop/windows/native/libawt/windows/awt_Toolkit.cpp

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -102,17 +102,36 @@ extern void DWMResetCompositionEnabled();
102102
first loaded */
103103
JavaVM *jvm = NULL;
104104

105+
/* Return a handle to the module containing this method, either a DLL in case
106+
* of a dynamic library build, or the .EXE in case of a static build.
107+
*/
108+
static HMODULE GetAwtModuleHandle() {
109+
HMODULE hModule = NULL;
110+
GetModuleHandleEx(
111+
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
112+
(LPCTSTR) &GetAwtModuleHandle,
113+
&hModule
114+
);
115+
return hModule;
116+
}
117+
118+
extern "C" {
119+
105120
JNIEXPORT jint JNICALL
106121
DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
107122
{
108123
TRY;
109124

125+
AwtToolkit::GetInstance().SetModuleHandle(GetAwtModuleHandle());
126+
110127
jvm = vm;
111128
return JNI_VERSION_1_2;
112129

113130
CATCH_BAD_ALLOC_RET(0);
114131
}
115132

133+
}
134+
116135
extern "C" JNIEXPORT jboolean JNICALL AWTIsHeadless() {
117136
static JNIEnv *env = NULL;
118137
static jboolean isHeadless;
@@ -246,32 +265,6 @@ BOOL AwtToolkit::activateKeyboardLayout(HKL hkl) {
246265
return (prev != 0);
247266
}
248267

249-
/************************************************************************
250-
* Exported functions
251-
*/
252-
253-
extern "C" BOOL APIENTRY DllMain(HANDLE hInstance, DWORD ul_reason_for_call,
254-
LPVOID)
255-
{
256-
// Don't use the TRY and CATCH_BAD_ALLOC_RET macros if we're detaching
257-
// the library. Doing so causes awt.dll to call back into the VM during
258-
// shutdown. This crashes the HotSpot VM.
259-
switch (ul_reason_for_call) {
260-
case DLL_PROCESS_ATTACH:
261-
TRY;
262-
AwtToolkit::GetInstance().SetModuleHandle((HMODULE)hInstance);
263-
CATCH_BAD_ALLOC_RET(FALSE);
264-
break;
265-
case DLL_PROCESS_DETACH:
266-
#ifdef DEBUG
267-
DTrace_DisableMutex();
268-
DMem_DisableMutex();
269-
#endif // DEBUG
270-
break;
271-
}
272-
return TRUE;
273-
}
274-
275268
/************************************************************************
276269
* AwtToolkit fields
277270
*/

0 commit comments

Comments
 (0)