/** ================================* eli960@qq.com* http://linuxmail.cn/* 2026年02月22日* ================================*/#include "zcc/zcc_stdlib.h"#include "zcc/zcc_win64.h"#ifdef _WIN64#include <shlwapi.h>#include <tlhelp32.h>#pragma comment(lib, "shlwapi.lib")#else#include <dlfcn.h>#include <link.h>#include <unistd.h>#endifzcc_namespace_begin;void *dlopen_auto(const std::string &lib_pathname_or_name, const std::string &symbol_to_check){void *libhandle = nullptr;libhandle = dlopen("");if (libhandle){if (dlsym(libhandle, symbol_to_check)){return libhandle;}dlclose(libhandle);libhandle = nullptr;}//if (lib_pathname_or_name.empty()){return nullptr;}//libhandle = dlopen(lib_pathname_or_name);if (libhandle){return libhandle;}//std::string name;#ifdef _WIN32name = lib_pathname_or_name + ".dll";#elsename = "lib" + lib_pathname_or_name + ".so";#endiflibhandle = dlopen(name);if (libhandle){return libhandle;}//return nullptr;}#ifdef _WIN64static HMODULE g_mainModule = nullptr;// 获取主模块句柄,使用缓存并考虑多线程安全static HMODULE getMainModule(){if (g_mainModule){return g_mainModule;}global_low_level_mutex_lock();if (g_mainModule == nullptr){g_mainModule = GetModuleHandleA(nullptr);}global_low_level_mutex_unlock();return g_mainModule;}#endif // _WIN64// 加载动态库,支持全局符号表void *dlopen(const std::string &lib_name){if (lib_name.empty()){#ifdef _WIN32// 返回一个特殊标记表示全局符号表return reinterpret_cast<void *>(-1);#elsereturn ::dlopen(nullptr, RTLD_GLOBAL | RTLD_LAZY);#endif}#ifdef _WIN32wchar_t lib_name_w[Z_MAX_PATH + 1];if (Utf8ToWideChar(lib_name, lib_name_w, Z_MAX_PATH) < 1){return nullptr;}return LoadLibraryW(lib_name_w);#elsereturn ::dlopen(lib_name.c_str(), RTLD_GLOBAL | RTLD_LAZY);#endif}// 获取符号地址,支持全局符号表void *dlsym(void *handle, const std::string &symbol_name){if (!handle){return nullptr;}#ifdef _WIN32if (handle == reinterpret_cast<void *>(-1)){// 查找全局符号表HMODULE mainModule = getMainModule();void *addr = GetProcAddress(mainModule, symbol_name.c_str());if (addr != nullptr)return addr;HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());if (snapshot == INVALID_HANDLE_VALUE)return nullptr;MODULEENTRY32 modEntry;modEntry.dwSize = sizeof(MODULEENTRY32);if (Module32First(snapshot, &modEntry)){do{if (modEntry.hModule == mainModule)continue;addr = GetProcAddress(modEntry.hModule, symbol_name.c_str());if (addr != nullptr){CloseHandle(snapshot);return addr;}} while (Module32Next(snapshot, &modEntry));}CloseHandle(snapshot);return nullptr;}else{return reinterpret_cast<void *>(GetProcAddress(static_cast<HMODULE>(handle), symbol_name.c_str()));}#elsereturn ::dlsym(handle, symbol_name.c_str());#endif}// 卸载动态库,处理全局符号表特殊情况bool dlclose(void *handle){if (!handle){return true;}#ifdef _WIN32if (handle != reinterpret_cast<void *>(-1)){return FreeLibrary(static_cast<HMODULE>(handle)) != 0;}return true;#elsereturn ::dlclose(handle) == 0;#endif}zcc_namespace_end;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型