Skip to content

CoreFoundation: clean up module locator logic #3163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions CoreFoundation/Base.subproj/CFRuntime.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ OBJC_EXPORT void *objc_destructInstance(id obj);

#if TARGET_OS_WIN32
#include <Shellapi.h>
#include <pathcch.h>
#endif

enum {
Expand Down Expand Up @@ -1321,33 +1322,24 @@ CF_PRIVATE void __CFSocketCleanup(void);
CF_PRIVATE void __CFStreamCleanup(void);

static CFBundleRef RegisterCoreFoundationBundle(void) {
#ifdef _DEBUG
// might be nice to get this from the project file at some point
wchar_t *DLLFileName = (wchar_t *)L"CoreFoundation_debug.dll";
#else
wchar_t *DLLFileName = (wchar_t *)L"CoreFoundation.dll";
#endif
wchar_t path[MAX_PATH+1];
path[0] = path[1] = 0;
DWORD wResult;
CFIndex idx;
HMODULE ourModule = GetModuleHandleW(DLLFileName);

CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandle failed");
HMODULE ourModule = NULL;
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)&RegisterCoreFoundationBundle, &ourModule);
CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandleExW failed");

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

// strip off last component, the DLL name
for (idx = wResult - 1; idx; idx--) {
if ('\\' == path[idx]) {
path[idx] = '\0';
break;
}
}
PathCchRemoveFileSpec(path, wResult);

CFStringRef fsPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar*)path, idx);
CFStringRef fsPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar*)path, wcslen(path));
CFURLRef dllURL = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, fsPath, kCFURLWindowsPathStyle, TRUE);
CFURLRef bundleURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, dllURL, CFSTR("CoreFoundation.resources"), TRUE);
CFRelease(fsPath);
Expand Down
3 changes: 2 additions & 1 deletion CoreFoundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
AdvAPI32
Secur32
User32
mincore)
mincore
pathcch)
target_link_libraries(CFURLSessionInterface
PRIVATE
AdvAPI32
Expand Down