Skip to content

Commit f0be24a

Browse files
committed
Attempt to fix dlfcn-win32 patch for UWP
1 parent 15e4eb6 commit f0be24a

1 file changed

Lines changed: 148 additions & 134 deletions

File tree

Lines changed: 148 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,163 @@
1-
From 4a4ba9ce9f666a66d027d84a26bb49bd6f92205a Mon Sep 17 00:00:00 2001
1+
From 6944869021af1f4f5585d7e54aaf0e8d971d0afd Mon Sep 17 00:00:00 2001
22
From: Alwin Esch <alwin.esch@web.de>
33
Date: Thu, 22 Aug 2019 19:30:12 +0100
44
Subject: [PATCH] [win10] fixed uwp build
55

66
---
7-
src/dlfcn.c | 43 ++++++++++++++++++++++++++++++++++++++-----
8-
1 file changed, 38 insertions(+), 5 deletions(-)
7+
src/dlfcn.c | 47 +++++++++++++++++++++++++++++++++++++++++------
8+
1 file changed, 41 insertions(+), 6 deletions(-)
99

1010
diff --git a/src/dlfcn.c b/src/dlfcn.c
11-
index cb9f9bb..74cac34 100644
11+
index cb9f9bb..ce43ae7 100644
1212
--- a/src/dlfcn.c
1313
+++ b/src/dlfcn.c
1414
@@ -24,6 +24,7 @@
15-
* THE SOFTWARE.
16-
*/
17-
18-
+#define _CRT_SECURE_NO_WARNINGS
19-
#ifdef _DEBUG
20-
#define _CRTDBG_MAP_ALLOC
21-
#include <stdlib.h>
15+
* THE SOFTWARE.
16+
*/
17+
18+
+#define _CRT_SECURE_NO_WARNINGS
19+
#ifdef _DEBUG
20+
#define _CRTDBG_MAP_ALLOC
21+
#include <stdlib.h>
2222
@@ -311,6 +312,7 @@ static HMODULE MyGetModuleHandleFromAddress( const void *addr )
23-
/* Load Psapi.dll at runtime, this avoids linking caveat */
24-
static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded )
25-
{
26-
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
27-
static BOOL (WINAPI *EnumProcessModulesPtr)(HANDLE, HMODULE *, DWORD, LPDWORD) = NULL;
28-
static BOOL failed = FALSE;
29-
UINT uMode;
23+
/* Load Psapi.dll at runtime, this avoids linking caveat */
24+
static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded )
25+
{
26+
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
27+
static BOOL (WINAPI *EnumProcessModulesPtr)(HANDLE, HMODULE *, DWORD, LPDWORD) = NULL;
28+
static BOOL failed = FALSE;
29+
UINT uMode;
3030
@@ -349,21 +351,27 @@ static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb,
31-
}
32-
33-
return EnumProcessModulesPtr( hProcess, lphModule, cb, lpcbNeeded );
34-
+#else
35-
+ return 0;
36-
+#endif
37-
}
38-
39-
DLFCN_EXPORT
40-
void *dlopen( const char *file, int mode )
41-
{
42-
- HMODULE hModule;
43-
- UINT uMode;
44-
+ HMODULE hModule = NULL;
45-
+ UINT uMode = 0;
46-
47-
error_occurred = FALSE;
48-
49-
/* Do not let Windows display the critical-error-handler message box */
50-
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
51-
uMode = MySetErrorMode( SEM_FAILCRITICALERRORS );
52-
+#endif
53-
54-
if( file == NULL )
55-
{
56-
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP) // what is replacement of GMH on UWP?
57-
/* POSIX says that if the value of file is NULL, a handle on a global
58-
* symbol object must be provided. That object must be able to access
59-
* all symbols from the original program file, and any objects loaded
31+
}
32+
33+
return EnumProcessModulesPtr( hProcess, lphModule, cb, lpcbNeeded );
34+
+#else
35+
+ return 0;
36+
+#endif
37+
}
38+
39+
DLFCN_EXPORT
40+
void *dlopen( const char *file, int mode )
41+
{
42+
- HMODULE hModule;
43+
- UINT uMode;
44+
+ HMODULE hModule = NULL;
45+
+ UINT uMode = 0;
46+
47+
error_occurred = FALSE;
48+
49+
/* Do not let Windows display the critical-error-handler message box */
50+
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
51+
uMode = MySetErrorMode( SEM_FAILCRITICALERRORS );
52+
+#endif
53+
54+
if( file == NULL )
55+
{
56+
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP) // what is replacement of GMH on UWP?
57+
/* POSIX says that if the value of file is NULL, a handle on a global
58+
* symbol object must be provided. That object must be able to access
59+
* all symbols from the original program file, and any objects loaded
6060
@@ -378,6 +386,7 @@ void *dlopen( const char *file, int mode )
61-
62-
if( !hModule )
63-
save_err_str( "(null)", GetLastError( ) );
64-
+#endif
65-
}
66-
else
67-
{
68-
@@ -415,8 +424,25 @@ void *dlopen( const char *file, int mode )
69-
* to UNIX's search paths (start with system folders instead of current
70-
* folder).
71-
*/
72-
- hModule = LoadLibraryExA( lpFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
73-
+#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)
74-
+ int result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, lpFileName, strlen(lpFileName), NULL, 0);
75-
+ if (result == 0)
76-
+ return NULL;
77-
+
78-
+ wchar_t* newStr = (wchar_t*)malloc(result*sizeof(wchar_t));
79-
+ result = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, lpFileName, strlen(lpFileName), newStr, result );
80-
+ if (result == 0)
81-
+ {
82-
+ free( newStr );
83-
+ return NULL;
84-
+ }
85-
86-
+ hModule = LoadPackagedLibrary( newStr, 0 );
87-
+ free( newStr );
88-
+ dwProcModsAfter = 0;
89-
+#else // WINAPI_PARTITION_DESKTOP
90-
+ hModule = LoadLibraryExA( lpFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
91-
+#endif
92-
if( !hModule )
93-
{
94-
save_err_str( lpFileName, GetLastError( ) );
95-
@@ -453,7 +479,9 @@ void *dlopen( const char *file, int mode )
96-
}
97-
98-
/* Return to previous state of the error-mode bit flags. */
99-
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
100-
MySetErrorMode( uMode );
101-
+#endif
102-
103-
return (void *) hModule;
104-
}
105-
@@ -488,13 +516,15 @@ void *dlsym( void *handle, const char *name )
106-
{
107-
FARPROC symbol;
108-
HMODULE hCaller;
109-
- HMODULE hModule;
110-
- DWORD dwMessageId;
111-
+ HMODULE hModule = 0;
112-
+ DWORD dwMessageId = 0;
113-
114-
error_occurred = FALSE;
115-
116-
symbol = NULL;
117-
hCaller = NULL;
118-
+
119-
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP) // what is replacement of GMH on UWP?
120-
hModule = GetModuleHandle( NULL );
121-
dwMessageId = 0;
122-
123-
@@ -525,6 +555,7 @@ void *dlsym( void *handle, const char *name )
124-
goto end;
125-
}
126-
}
127-
+#endif
128-
129-
if( handle != RTLD_NEXT )
130-
{
131-
@@ -538,6 +569,7 @@ void *dlsym( void *handle, const char *name )
132-
* in all globally loaded objects.
133-
*/
134-
135-
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
136-
if( hModule == handle || handle == RTLD_NEXT )
137-
{
138-
HANDLE hCurrentProc;
139-
@@ -588,6 +620,7 @@ void *dlsym( void *handle, const char *name )
140-
}
141-
}
142-
}
143-
+#endif
144-
145-
end:
146-
if( symbol == NULL )
61+
62+
if( !hModule )
63+
save_err_str( "(null)", GetLastError( ) );
64+
+#endif
65+
}
66+
else
67+
{
68+
@@ -415,16 +424,35 @@ void *dlopen( const char *file, int mode )
69+
* to UNIX's search paths (start with system folders instead of current
70+
* folder).
71+
*/
72+
- hModule = LoadLibraryExA( lpFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
73+
+#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)
74+
+ int result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, lpFileName, strlen(lpFileName), NULL, 0);
75+
+ if (result == 0)
76+
+ return NULL;
77+
+
78+
+ wchar_t* newStr = (wchar_t*)malloc(result*sizeof(wchar_t));
79+
+ result = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, lpFileName, strlen(lpFileName), newStr, result );
80+
+ if (result == 0)
81+
+ {
82+
+ free( newStr );
83+
+ return NULL;
84+
+ }
85+
86+
+ hModule = LoadPackagedLibrary( newStr, 0 );
87+
+ free( newStr );
88+
+ dwProcModsAfter = 0;
89+
+#else // WINAPI_PARTITION_DESKTOP
90+
+ hModule = LoadLibraryExA( lpFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
91+
+#endif
92+
if( !hModule )
93+
{
94+
save_err_str( lpFileName, GetLastError( ) );
95+
}
96+
else
97+
{
98+
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
99+
if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 )
100+
dwProcModsAfter = 0;
101+
+#endif
102+
103+
/* If the object was loaded with RTLD_LOCAL, add it to list of local
104+
* objects, so that its symbols cannot be retrieved even if the handle for
105+
@@ -453,7 +481,9 @@ void *dlopen( const char *file, int mode )
106+
}
107+
108+
/* Return to previous state of the error-mode bit flags. */
109+
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
110+
MySetErrorMode( uMode );
111+
+#endif
112+
113+
return (void *) hModule;
114+
}
115+
@@ -488,13 +518,15 @@ void *dlsym( void *handle, const char *name )
116+
{
117+
FARPROC symbol;
118+
HMODULE hCaller;
119+
- HMODULE hModule;
120+
- DWORD dwMessageId;
121+
+ HMODULE hModule = 0;
122+
+ DWORD dwMessageId = 0;
123+
124+
error_occurred = FALSE;
125+
126+
symbol = NULL;
127+
hCaller = NULL;
128+
+
129+
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP) // what is replacement of GMH on UWP?
130+
hModule = GetModuleHandle( NULL );
131+
dwMessageId = 0;
132+
133+
@@ -525,6 +557,7 @@ void *dlsym( void *handle, const char *name )
134+
goto end;
135+
}
136+
}
137+
+#endif
138+
139+
if( handle != RTLD_NEXT )
140+
{
141+
@@ -538,9 +571,10 @@ void *dlsym( void *handle, const char *name )
142+
* in all globally loaded objects.
143+
*/
144+
145+
+#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
146+
if( hModule == handle || handle == RTLD_NEXT )
147+
{
148+
- HANDLE hCurrentProc;
149+
+ HANDLE hCurrentProc = 0;
150+
HMODULE *modules;
151+
DWORD cbNeeded;
152+
DWORD dwSize;
153+
@@ -588,6 +622,7 @@ void *dlsym( void *handle, const char *name )
154+
}
155+
}
156+
}
157+
+#endif
158+
159+
end:
160+
if( symbol == NULL )
147161
--
148-
2.47.1.windows.1
162+
2.53.0
149163

0 commit comments

Comments
 (0)