Skip to content

Commit 80a9026

Browse files
committed
CoreFoundation: clean up module locator logic
Use the address of the function to get a module handle without bumping the refcount of the module. This avoids the need to hardcode the library name and enables the use of CoreFoundation in a statically linked fashion as well. We can further reduce the logic by using the PathCch routine to remove the trailing file specification. This reduces the complexity in the module location code and reuses the system libraries for a small additional cost of an additional iteration of the string path.
1 parent d956aff commit 80a9026

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

CoreFoundation/Base.subproj/CFRuntime.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ OBJC_EXPORT void *objc_destructInstance(id obj);
4242

4343
#if TARGET_OS_WIN32
4444
#include <Shellapi.h>
45+
#include <pathcch.h>
4546
#endif
4647

4748
enum {
@@ -1321,33 +1322,24 @@ CF_PRIVATE void __CFSocketCleanup(void);
13211322
CF_PRIVATE void __CFStreamCleanup(void);
13221323

13231324
static CFBundleRef RegisterCoreFoundationBundle(void) {
1324-
#ifdef _DEBUG
1325-
// might be nice to get this from the project file at some point
1326-
wchar_t *DLLFileName = (wchar_t *)L"CoreFoundation_debug.dll";
1327-
#else
1328-
wchar_t *DLLFileName = (wchar_t *)L"CoreFoundation.dll";
1329-
#endif
13301325
wchar_t path[MAX_PATH+1];
13311326
path[0] = path[1] = 0;
13321327
DWORD wResult;
13331328
CFIndex idx;
1334-
HMODULE ourModule = GetModuleHandleW(DLLFileName);
13351329

1336-
CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandle failed");
1330+
HMODULE ourModule = NULL;
1331+
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
1332+
(LPCWSTR)&RegisterCoreFoundationBundle, &ourModule);
1333+
CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandleExW failed");
13371334

13381335
wResult = GetModuleFileNameW(ourModule, path, MAX_PATH+1);
13391336
CFAssert1(wResult > 0, __kCFLogAssertion, "GetModuleFileName failed: %d", GetLastError());
13401337
CFAssert1(wResult < MAX_PATH+1, __kCFLogAssertion, "GetModuleFileName result truncated: %s", path);
13411338

13421339
// strip off last component, the DLL name
1343-
for (idx = wResult - 1; idx; idx--) {
1344-
if ('\\' == path[idx]) {
1345-
path[idx] = '\0';
1346-
break;
1347-
}
1348-
}
1340+
PathCchRemoveFileSpec(path, wResult);
13491341

1350-
CFStringRef fsPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar*)path, idx);
1342+
CFStringRef fsPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar*)path, wcslen(path));
13511343
CFURLRef dllURL = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, fsPath, kCFURLWindowsPathStyle, TRUE);
13521344
CFURLRef bundleURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, dllURL, CFSTR("CoreFoundation.resources"), TRUE);
13531345
CFRelease(fsPath);

CoreFoundation/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
478478
AdvAPI32
479479
Secur32
480480
User32
481-
mincore)
481+
mincore
482+
pathcch)
482483
target_link_libraries(CFURLSessionInterface
483484
PRIVATE
484485
AdvAPI32

0 commit comments

Comments
 (0)