static void *clib_getsym(CLibrary *cl, const char *name) { void *p = NULL; if (cl->handle == CLIB_DEFHANDLE) { /* Search default libraries. */ MSize i; for (i = 0; i < CLIB_HANDLE_MAX; i++) { HINSTANCE h = (HINSTANCE)clib_def_handle[i]; if (!(void *)h) { /* Resolve default library handles (once). */ switch (i) { case CLIB_HANDLE_EXE: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &h); break; case CLIB_HANDLE_DLL: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (const char *)clib_def_handle, &h); break; case CLIB_HANDLE_CRT: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (const char *)&_fmode, &h); break; case CLIB_HANDLE_KERNEL32: h = LoadLibraryExA("kernel32.dll", NULL, 0); break; case CLIB_HANDLE_USER32: h = LoadLibraryExA("user32.dll", NULL, 0); break; case CLIB_HANDLE_GDI32: h = LoadLibraryExA("gdi32.dll", NULL, 0); break; } if (!h) continue; clib_def_handle[i] = (void *)h; } p = (void *)GetProcAddress(h, name); if (p) break; } } else { p = (void *)GetProcAddress((HINSTANCE)cl->handle, name); } return p; }
void InitResourceUtils() { if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &localFunc, &gInstance)) { int ret = GetLastError(); fprintf(stderr, "GetModuleHandle returned %d\n", ret); } GetModuleFileNameA(gInstance, szModulePath, sizeof(szModulePath)); }
std::string GetCurrentModuleName() { std::string name; char buffer[MAX_PATH]; HMODULE hModule = NULL; GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)&GetCurrentModuleName, //这是函数名,强转 &hModule); DWORD len = GetModuleFileNameA(hModule, buffer, MAX_PATH); if (len) { name = PathHelp::_base_get_noext_name(buffer); } return name; }
static const char *ll_bcsym(void *lib, const char *sym) { if (lib) { return (const char *)GetProcAddress((HINSTANCE)lib, sym); } else { HINSTANCE h = GetModuleHandleA(NULL); const char *p = (const char *)GetProcAddress(h, sym); if (p == NULL && GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (const char *)ll_bcsym, &h)) p = (const char *)GetProcAddress(h, sym); return p; } }
void * windows_open_shared_library(char *path) { HMODULE module = (HMODULE)0; /* Try to open an existing module in a way that increments its reference count without running any initialization code in the dll. */ if (!GetModuleHandleExA(0,path,&module)) { /* If that failed ... */ module = LoadLibraryA(path); } return (void *)module; }
std::string ElDorito::GetDirectory() { char Path[MAX_PATH]; HMODULE hMod; if( !GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)&::HandleFinder, &hMod) ) { int Error = GetLastError(); OutputDebugString(std::string("Unable to resolve current directory, error code: ").append(std::to_string(Error)).c_str()); } GetModuleFileNameA(hMod, Path, sizeof(Path)); std::string Dir(Path); Dir = Dir.substr(0, std::string(Dir).find_last_of('\\') + 1); return Dir; }
std::string GetLocalModuleFilename() { #ifdef WIN32 char path[256]; HMODULE hm = NULL; GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &GetLocalModuleFilename, &hm); GetModuleFileNameA(hm, path, sizeof(path)); return path; #else #error GetLocalModuleName() not implemented for this platform #endif }
void VulkanReplay::OutputWindow::CreateSurface(VkInstance inst) { VkWin32SurfaceCreateInfoKHR createInfo; createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; createInfo.pNext = NULL; createInfo.flags = 0; createInfo.hwnd = wnd; GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (const char *)&dllLocator, (HMODULE *)&createInfo.hinstance); VkResult vkr = ObjDisp(inst)->CreateWin32SurfaceKHR(Unwrap(inst), &createInfo, NULL, &surface); RDCASSERTEQUAL(vkr, VK_SUCCESS); }
bool HLSLCompiler::initialize() { TRACE_EVENT0("gpu", "initializeCompiler"); #if !defined(ANGLE_ENABLE_WINDOWS_STORE) #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES) // Find a D3DCompiler module that had already been loaded based on a predefined list of versions. static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i) { if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule)) { break; } } #endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES if (!mD3DCompilerModule) { // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with. mD3DCompilerModule = LoadLibrary(D3DCOMPILER_DLL); } if (!mD3DCompilerModule) { ERR("No D3D compiler module found - aborting!\n"); return false; } mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile")); ASSERT(mD3DCompileFunc); mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(GetProcAddress(mD3DCompilerModule, "D3DDisassemble")); ASSERT(mD3DDisassembleFunc); #else // D3D Shader compiler is linked already into this module, so the export // can be directly assigned. mD3DCompilerModule = NULL; mD3DCompileFunc = reinterpret_cast<pD3DCompile>(D3DCompile); mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(D3DDisassemble); #endif return mD3DCompileFunc != NULL; }
CString sharedResourcesPath() { static CString cachedPath; if (!cachedPath.isNull()) return cachedPath; #if OS(WINDOWS) HMODULE hmodule = 0; GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char*>(sharedResourcesPath), &hmodule); GUniquePtr<gchar> runtimeDir(g_win32_get_package_installation_directory_of_module(hmodule)); GUniquePtr<gchar> dataPath(g_build_filename(runtimeDir.get(), "share", "webkitgtk-" WEBKITGTK_API_VERSION_STRING, NULL)); #else GUniquePtr<gchar> dataPath(g_build_filename(DATA_DIR, "webkitgtk-" WEBKITGTK_API_VERSION_STRING, NULL)); #endif cachedPath = dataPath.get(); return cachedPath; }
static FILE * OpenStackFile () { static char filename[ 1024 ]; memset ( filename, 0, 1024 ); char * it ( filename ); #ifdef WIN32 static HMODULE module; GetModuleHandleExA ( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast< LPSTR >( filename ), &module ); GetModuleFileNameA ( module, filename, 1023 ); #else static Dl_info info; dladdr ( reinterpret_cast<void*>( filename ), &info ); memcpy ( filename, info.dli_fname, 1023 ); #endif it += strlen( filename ); while( *it != '\\' && *it != '/' ) --it; memcpy ( ++it, "!stacktrace.txt", 16 ); printf("Opening %s\n", filename ); FILE * pfile(fopen(filename, "a")); if (pfile) { fprintf(pfile, "Using plugin version %s-%s-%s\n\n", NCZ_VERSION_GIT, #ifdef DEBUG "Debug", #else "Release", #endif #ifdef GNUC "Linux" #else "Windows" #endif ); } return pfile; }
/* * The GetModuleBase64 function retrieves the base address of the module that * contains the specified address. * * Same as SymGetModuleBase64, but that seems to often cause problems. */ DWORD64 WINAPI MgwSymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddress) { if (hProcess == GetCurrentProcess()) { HMODULE hModule = NULL; BOOL bRet = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)(UINT_PTR)dwAddress, &hModule); if (bRet) { return (DWORD64)(UINT_PTR)hModule; } } MEMORY_BASIC_INFORMATION Buffer; if (VirtualQueryEx(hProcess, (LPCVOID)(UINT_PTR)dwAddress, &Buffer, sizeof Buffer) != 0) { return (DWORD64)(UINT_PTR)Buffer.AllocationBase; } return SymGetModuleBase64(hProcess, dwAddress); }
static FILE * OpenStackFile () { static char filename[ 1024 ]; memset ( filename, 0, 1024 ); char * it ( filename ); #ifdef WIN32 static HMODULE module; GetModuleHandleExA ( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast< LPSTR >( filename ), &module ); GetModuleFileNameA ( module, filename, 1023 ); #else static Dl_info info; dladdr ( reinterpret_cast<void*>( filename ), &info ); memcpy ( filename, info.dli_fname, 1023 ); #endif it += strlen( filename ); while( *it != '\\' && *it != '/' ) --it; memcpy ( ++it, "!stacktrace.txt", 16 ); printf("Opening %s\n", filename ); return fopen ( filename, "a" ); }
MStatus TreeGeneratorGUI::doIt(const MArgList& args) { // Get the absoulute path of this .dll char buffer[256]; HMODULE handle = nullptr; GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)"TreeGenerator.mll", &handle); GetModuleFileNameA(handle, buffer, sizeof(buffer)); // Convert the path to the gui .mel MString path(buffer); const int size = static_cast<int>(path.length()-std::string("TreeGenerator.mll").size()); path = path.substring(0, size - 1); path += "TreeGeneratorGUI.mel"; // Open the GUI .mel file std::ifstream gui(path.asChar()); if(!gui.is_open()) { MGlobal::executeCommand("error \"" + path + " could not open\""); return MStatus::kFailure; } // Execute the mel file std::string line; MString command; while(!gui.eof()) { std::getline(gui, line); command += line.c_str(); } MGlobal::executeCommand(command); return MStatus::kSuccess; }
void GetResourcesPath(char *path, int size) { ZeroMemory(path, size); HMODULE hm = NULL; if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &GetResourcesPath, &hm)) { return; } DWORD x = GetModuleFileNameA(hm, path, size); if (!x) return; int p = lstrlenA(path); if (!p) return; while (p--) { if (path[p] == '\\') { path[p+1] = 0; break; } } StringCchCatA(path, MAX_PATH, "\\"BMP_PATH); }
am_shm_t *am_shm_create(const char *name, size_t usize) { struct mem_pool *pool = NULL; size_t size; char opened = AM_FALSE; void *area = NULL; am_shm_t *ret = NULL; #ifdef _WIN32 char dll_path[AM_URI_SIZE]; DWORD error = 0; HMODULE hm = NULL; void *caller = _ReturnAddress(); #else int fdflags; int error = 0; #endif ret = calloc(1, sizeof(am_shm_t)); if (ret == NULL) return NULL; #ifdef _WIN32 if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) caller, &hm) && GetModuleFileNameA(hm, dll_path, sizeof(dll_path) - 1) > 0) { PathRemoveFileSpecA(dll_path); strcat(dll_path, FILE_PATH_SEP); snprintf(ret->name[0], sizeof(ret->name[0]), AM_GLOBAL_PREFIX"%s_l", name); /* mutex/semaphore */ snprintf(ret->name[1], sizeof(ret->name[1]), AM_GLOBAL_PREFIX"%s_s", name); /* shared memory name */ snprintf(ret->name[2], sizeof(ret->name[2]), "%s.."FILE_PATH_SEP"log"FILE_PATH_SEP"%s_f", dll_path, name); /* shared memory file name */ snprintf(ret->name[3], sizeof(ret->name[3]), AM_GLOBAL_PREFIX"%s_sz", name); /* shared memory name for global_size */ } else { ret->error = AM_NOT_FOUND; return ret; } #else snprintf(ret->name[0], sizeof(ret->name[0]), "/%s_l", name); /* mutex/semaphore */ snprintf(ret->name[1], sizeof(ret->name[1]), #ifdef __sun "/%s_s" #else "%s_s" #endif , name); /* shared memory name */ #endif size = page_size(usize + SIZEOF_mem_pool); /* need at least the size of the mem_pool header */ #ifdef _WIN32 ret->h[0] = CreateMutexA(NULL, TRUE, ret->name[0]); error = GetLastError(); if (ret->h[0] != NULL && error == ERROR_ALREADY_EXISTS) { do { error = WaitForSingleObject(ret->h[0], INFINITE); } while (error == WAIT_ABANDONED); } else { if (error == ERROR_ACCESS_DENIED) { ret->h[0] = OpenMutexA(SYNCHRONIZE, FALSE, ret->name[0]); } if (ret->h[0] == NULL) { ret->error = error; return ret; } } ret->h[1] = CreateFileA(ret->name[2], GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_NEW, FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL); error = GetLastError(); if (ret->h[1] == INVALID_HANDLE_VALUE && error == ERROR_FILE_EXISTS) { ret->h[1] = CreateFileA(ret->name[2], GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL); error = GetLastError(); if (ret->h[1] != INVALID_HANDLE_VALUE) { opened = AM_TRUE; size = GetFileSize(ret->h[1], NULL); } } if (ret->h[1] == INVALID_HANDLE_VALUE || error != 0) { CloseHandle(ret->h[0]); ret->error = error; am_shm_unlock(ret); return ret; } if (!opened) { ret->h[2] = CreateFileMappingA(ret->h[1], NULL, PAGE_READWRITE, 0, (DWORD) size, ret->name[1]); error = GetLastError(); } else { ret->h[2] = OpenFileMappingA(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, ret->name[1]); error = GetLastError(); if (ret->h[2] == NULL && error == ERROR_FILE_NOT_FOUND) { ret->h[2] = CreateFileMappingA(ret->h[1], NULL, PAGE_READWRITE, 0, (DWORD) size, ret->name[1]); error = GetLastError(); } } if (ret->h[2] == NULL || error != 0) { CloseHandle(ret->h[0]); CloseHandle(ret->h[1]); ret->error = error; am_shm_unlock(ret); return ret; } area = MapViewOfFile(ret->h[2], FILE_MAP_ALL_ACCESS, 0, 0, 0); error = GetLastError(); if (area == NULL || (error != 0 && error != ERROR_ALREADY_EXISTS)) { CloseHandle(ret->h[0]); CloseHandle(ret->h[1]); CloseHandle(ret->h[2]); ret->error = error; am_shm_unlock(ret); return ret; } ret->h[3] = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD) sizeof(size_t), ret->name[3]); if (ret->h[3] == NULL) { ret->error = GetLastError(); CloseHandle(ret->h[0]); CloseHandle(ret->h[1]); CloseHandle(ret->h[2]); am_shm_unlock(ret); return ret; } ret->global_size = MapViewOfFile(ret->h[3], FILE_MAP_ALL_ACCESS, 0, 0, 0); if (ret->global_size == NULL) { ret->error = GetLastError(); CloseHandle(ret->h[0]); CloseHandle(ret->h[1]); CloseHandle(ret->h[2]); CloseHandle(ret->h[3]); am_shm_unlock(ret); return ret; } *(ret->global_size) = ret->local_size = size; #else ret->lock = mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (ret->lock == MAP_FAILED) { ret->error = errno; return ret; } else { pthread_mutexattr_t attr; pthread_mutex_t *lock = (pthread_mutex_t *) ret->lock; pthread_mutexattr_init(&attr); pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); #if defined(__sun) #if defined(__SunOS_5_10) #if defined(_POSIX_THREAD_PRIO_INHERIT) pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP); #endif #else pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); #endif #endif #if defined(LINUX) pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP); #endif pthread_mutex_init(lock, &attr); pthread_mutexattr_destroy(&attr); } ret->global_size = mmap(NULL, sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (ret->global_size == MAP_FAILED) { ret->error = errno; return ret; } *(ret->global_size) = ret->local_size = size; am_shm_lock(ret); ret->fd = shm_open(ret->name[1], O_CREAT | O_EXCL | O_RDWR, 0666); error = errno; if (ret->fd == -1 && error != EEXIST) { munmap(ret->lock, sizeof(pthread_mutex_t)); ret->error = error; am_shm_unlock(ret); return ret; } if (ret->fd == -1) { ret->fd = shm_open(ret->name[1], O_RDWR, 0666); error = errno; if (ret->fd == -1) { munmap(ret->lock, sizeof(pthread_mutex_t)); ret->error = error; am_shm_unlock(ret); return ret; } /* reset FD_CLOEXEC */ fdflags = fcntl(ret->fd, F_GETFD); fdflags &= ~FD_CLOEXEC; fcntl(ret->fd, F_SETFD, fdflags); /* try with just a header */ area = mmap(NULL, SIZEOF_mem_pool, PROT_READ | PROT_WRITE, MAP_SHARED, ret->fd, 0); if (area == MAP_FAILED) { ret->error = errno; am_shm_unlock(ret); return ret; } size = ((struct mem_pool *) area)->size; if (munmap(area, SIZEOF_mem_pool) == -1) { ret->error = errno; am_shm_unlock(ret); return ret; } area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ret->fd, 0); if (area == MAP_FAILED) { ret->error = errno; am_shm_unlock(ret); return ret; } opened = AM_TRUE; } else { /* reset FD_CLOEXEC */ fdflags = fcntl(ret->fd, F_GETFD); fdflags &= ~FD_CLOEXEC; fcntl(ret->fd, F_SETFD, fdflags); if (ftruncate(ret->fd, size) == -1) { ret->error = errno; am_shm_unlock(ret); return ret; } area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ret->fd, 0); if (area == MAP_FAILED) { ret->error = errno; am_shm_unlock(ret); return ret; } } #endif ret->init = !opened; pool = (struct mem_pool *) area; if (ret->init) { struct mem_chunk *e = (struct mem_chunk *) ((char *) pool + SIZEOF_mem_pool); pool->size = size; pool->user_offset = 0; pool->open = 1; pool->resize = 0; /* add all available (free) space as one chunk in a freelist */ e->used = 0; e->usize = 0; e->size = pool->size - SIZEOF_mem_pool; e->lh.next = e->lh.prev = 0; /* update head prev/next pointers */ pool->lh.next = pool->lh.prev = AM_GET_OFFSET(pool, e); } else { if (pool->user_offset > 0) { ret->user = AM_GET_POINTER(pool, pool->user_offset); } pool->open++; } ret->pool = pool; ret->error = 0; am_shm_unlock(ret); return ret; }
void KeepModuleAlive() { // deliberately omitting GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT to bump refcount GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const char *)&ownModuleHandle, &ownModuleHandle); }
uv_lib_t *jl_load_dynamic_library(char *fname) { int error; char *modname, *ext; char path[PATHBUF]; int i; uv_lib_t *handle=malloc(sizeof(uv_lib_t)); handle->errmsg=NULL; modname = fname; if (modname == NULL) { #if defined(__WIN32__) if(!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)(&jl_load_dynamic_library), handle->handle)) jl_errorf("could not load base module", fname); #else handle->handle = dlopen(NULL,RTLD_NOW); #endif goto done; } else if (modname[0] == '/') { error = uv_dlopen(modname,handle); if (!error) goto done; } char *cwd; for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; if (modname[0] != '/') { if (julia_home) { /* try julia_home/usr/lib */ strncpy(path, julia_home, PATHBUF-1); strncat(path, "/usr/lib/", PATHBUF-1-strlen(path)); strncat(path, modname, PATHBUF-1-strlen(path)); strncat(path, ext, PATHBUF-1-strlen(path)); error = uv_dlopen(path, handle); if (!error) goto done; // if file exists but didn't load, show error details struct stat sbuf; if (stat(path, &sbuf) != -1) { JL_PRINTF(JL_STDERR, "%d\n", error); jl_errorf("could not load module %s", fname); } } cwd = getcwd(path, PATHBUF); if (cwd != NULL) { /* next try load from current directory */ strncat(path, "/", PATHBUF-1-strlen(path)); strncat(path, modname, PATHBUF-1-strlen(path)); strncat(path, ext, PATHBUF-1-strlen(path)); error = uv_dlopen(path, handle); if (!error) goto done; } } /* try loading from standard library path */ strncpy(path, modname, PATHBUF-1); strncat(path, ext, PATHBUF-1-strlen(path)); error = uv_dlopen(path, handle); if (!error) goto done; } JL_PRINTF(JL_STDERR, "could not load module %s (%d): %s\n", fname, error, uv_dlerror(handle)); jl_errorf("could not load module %s", fname); free(handle); return NULL; done: return handle; }
bool HLSLCompiler::initialize() { #if !defined(ANGLE_ENABLE_WINDOWS_STORE) #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES) // Find a D3DCompiler module that had already been loaded based on a predefined list of versions. static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i) { if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule)) { break; } } #endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES // Load the compiler DLL specified by the environment, or default to QT_D3DCOMPILER_DLL const wchar_t *defaultCompiler = _wgetenv(L"QT_D3DCOMPILER_DLL"); if (!defaultCompiler) defaultCompiler = QT_D3DCOMPILER_DLL; const wchar_t *compilerDlls[] = { defaultCompiler, L"d3dcompiler_47.dll", L"d3dcompiler_46.dll", L"d3dcompiler_45.dll", L"d3dcompiler_44.dll", L"d3dcompiler_43.dll", 0 }; // Load the first available known compiler DLL for (int i = 0; compilerDlls[i]; ++i) { mD3DCompilerModule = LoadLibrary(compilerDlls[i]); if (mD3DCompilerModule) break; } if (!mD3DCompilerModule) { // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with. mD3DCompilerModule = LoadLibrary(D3DCOMPILER_DLL); } if (!mD3DCompilerModule) { ERR("No D3D compiler module found - aborting!\n"); return false; } mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile")); ASSERT(mD3DCompileFunc); mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(GetProcAddress(mD3DCompilerModule, "D3DDisassemble")); ASSERT(mD3DDisassembleFunc); #else // D3D Shader compiler is linked already into this module, so the export // can be directly assigned. mD3DCompilerModule = NULL; mD3DCompileFunc = reinterpret_cast<pD3DCompile>(D3DCompile); mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(D3DDisassemble); #endif return mD3DCompileFunc != NULL; }
static uv_lib_t *jl_load_dynamic_library_(char *modname, unsigned flags, int throw_err) { int error; char *ext; char path[PATHBUF]; int i; uv_lib_t *handle=malloc(sizeof(uv_lib_t)); handle->errmsg=NULL; if (modname == NULL) { #ifdef _OS_WINDOWS_ if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)(&jl_load_dynamic_library), &handle->handle)) jl_errorf("could not load base module", modname); #else handle->handle = dlopen(NULL,RTLD_NOW); #endif goto done; } #ifdef _OS_WINDOWS_ else if (modname[1] == ':') { #else else if (modname[0] == '/') { #endif error = jl_uv_dlopen(modname,handle,flags); if (!error) goto done; } else { jl_array_t* DL_LOAD_PATH = (jl_array_t*)jl_get_global(jl_base_module, jl_symbol("DL_LOAD_PATH")); if (DL_LOAD_PATH != NULL) { size_t j; for (j = 0; j < jl_array_len(DL_LOAD_PATH); j++) { char *dl_path = jl_string_data(jl_cell_data(DL_LOAD_PATH)[j]); size_t len = strlen(dl_path); if (len == 0) continue; for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; if (dl_path[len-1] == PATHSEP) snprintf(path, PATHBUF, "%s%s%s", dl_path, modname, ext); else snprintf(path, PATHBUF, "%s" PATHSEPSTRING "%s%s", dl_path, modname, ext); error = jl_uv_dlopen(path, handle, flags); if (!error) goto done; } } } } for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; /* try loading from standard library path */ snprintf(path, PATHBUF, "%s%s", modname, ext); error = jl_uv_dlopen(path, handle, flags); if (!error) goto done; } #if defined(__linux__) char *soname = jl_lookup_soname(modname, strlen(modname)); error = (soname==NULL) || jl_uv_dlopen(soname, handle, flags); if (!error) goto done; #endif if (throw_err) { //JL_PRINTF(JL_STDERR, "could not load module %s (%d): %s\n", modname, error, uv_dlerror(handle)); jl_errorf("could not load module %s: %s", modname, uv_dlerror(handle)); } uv_dlclose(handle); free(handle); return NULL; done: return handle; } uv_lib_t *jl_load_dynamic_library_e(char *modname, unsigned flags) { return jl_load_dynamic_library_(modname, flags, 0); } uv_lib_t *jl_load_dynamic_library(char *modname, unsigned flags) { return jl_load_dynamic_library_(modname, flags, 1); } DLLEXPORT void *jl_dlsym_e(uv_lib_t *handle, char *symbol) { void *ptr; int error=uv_dlsym(handle, symbol, &ptr); if (error) ptr=NULL; return ptr; }
std::string Settings::getDllPath() { #ifdef _MSC_VER char szAppPath[MAX_PATH]; memset(szAppPath, 0x00, sizeof(szAppPath)); static std::string path = "."; if (path == ".") { char tmp[128]; DWORD error = ERROR_SUCCESS; if (!GetModuleFileNameA((HMODULE)&__ImageBase, szAppPath, sizeof(szAppPath) - 1)) { error = GetLastError(); sprintf(tmp, "Cannot get module file name. Last error code: %lu. Trying with GetModuleHandle first...", error); OutputDebugStringA(tmp); HMODULE hm = NULL; if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)&instance, &hm)) { int ret = GetLastError(); sprintf(tmp, "GetModuleHandle returned %d\n", ret); OutputDebugStringA(tmp); } if (!GetModuleFileNameA(hm, szAppPath, sizeof(szAppPath) - 1)) { error = GetLastError(); sprintf(tmp, "Cannot get module file name. Last error code: %lu. Trying with hmodule (%p) from dllmain...", error, __hLibLogicalAccessModule); OutputDebugStringA(tmp); if (__hLibLogicalAccessModule == NULL) { sprintf(tmp, "hmodule from dllmain is null."); OutputDebugStringA(tmp); } else { if (!GetModuleFileNameA(__hLibLogicalAccessModule, szAppPath, sizeof(szAppPath) - 1)) { error = GetLastError(); } else { error = ERROR_SUCCESS; } } } else { error = ERROR_SUCCESS; } } if (error == ERROR_SUCCESS) { std::string tmp(szAppPath); size_t index = tmp.find_last_of("/\\"); if (index != std::string::npos) { tmp = tmp.substr(0, index); } path = tmp; } else { sprintf(tmp, "Still cannot get module file name. Last error code: %lu.", error); OutputDebugStringA(tmp); } sprintf(tmp, "Current dll path is: %s.", path.c_str()); OutputDebugStringA(tmp); } return path; #elif defined(__APPLE__) CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle); char path[PATH_MAX]; if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX)) { return boost::filesystem::current_path().string(); } CFRelease(resourcesURL); return std::string(path, strlen(path)); #else return boost::filesystem::current_path().string(); #endif }
uv_lib_t *jl_load_dynamic_library(char *modname) { int error; char *ext; char path[PATHBUF]; int i; uv_lib_t *handle=malloc(sizeof(uv_lib_t)); handle->errmsg=NULL; if (modname == NULL) { #ifdef _WIN32 if(!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)(&jl_load_dynamic_library), &handle->handle)) jl_errorf("could not load base module", modname); #else handle->handle = dlopen(NULL,RTLD_NOW); #endif goto done; } #ifdef _WIN32 else if (modname[1] == ':') { #else else if (modname[0] == '/') { #endif error = jl_uv_dlopen(modname,handle); if (!error) goto done; } for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; if (modname[0] != '/') { if (julia_home) { /* try julia_home/../lib */ snprintf(path, PATHBUF, "%s/../lib/%s%s", julia_home, modname, ext); error = jl_uv_dlopen(path, handle); if (!error) goto done; // if file exists but didn't load, show error details struct stat sbuf; if (stat(path, &sbuf) != -1) { //JL_PRINTF(JL_STDERR, "could not load module %s (%d): %s\n", modname, error, uv_dlerror(handle)); //jl_errorf("could not load module %s: %s", modname, uv_dlerror(handle)); goto error; } } } /* try loading from standard library path */ snprintf(path, PATHBUF, "%s%s", modname, ext); error = jl_uv_dlopen(path, handle); if (!error) goto done; } #if !defined(__APPLE__) && !defined(_WIN32) char *soname = jl_lookup_soname(modname, strlen(modname)); error = jl_uv_dlopen(soname, handle); if (!error) goto done; #endif error: //JL_PRINTF(JL_STDERR, "could not load module %s (%d): %s\n", modname, error, uv_dlerror(handle)); jl_errorf("could not load module %s: %s", modname, uv_dlerror(handle)); uv_dlclose(handle); free(handle); return NULL; done: return handle; } void *jl_dlsym_e(uv_lib_t *handle, char *symbol) { void *ptr; int error=uv_dlsym(handle, symbol, &ptr); if(error) ptr=NULL; return ptr; }
bool ScreenCapturerMagnifier::InitializeMagnifier() { BOOL bResult; // Import necessary functions m_hMagLib = ::LoadLibrary(L"Magnification.dll"); if (!m_hMagLib) { TRACE(L"Failed to load Magnification.dll library\n"); return false; } m_MagInitializeFunc = reinterpret_cast<MagInitializeFunc>(GetProcAddress(m_hMagLib, "MagInitialize")); m_MagUninitializeFunc = reinterpret_cast<MagUninitializeFunc>(GetProcAddress(m_hMagLib, "MagUninitialize")); m_MagSetWindowSourceFunc = reinterpret_cast<MagSetWindowSourceFunc>(GetProcAddress(m_hMagLib, "MagSetWindowSource")); m_MagSetWindowFilterListFunc = reinterpret_cast<MagSetWindowFilterListFunc>(GetProcAddress(m_hMagLib, "MagSetWindowFilterList")); m_MagSetImageScalingCallbackFunc = reinterpret_cast<MagSetImageScalingCallbackFunc>(GetProcAddress(m_hMagLib, "MagSetImageScalingCallback")); if (!m_MagInitializeFunc || !m_MagUninitializeFunc || !m_MagSetWindowSourceFunc || !m_MagSetWindowFilterListFunc || !m_MagSetImageScalingCallbackFunc) { TRACE(L"Failed to initialize one of the Magnification functions\n"); return false; } // Initialize Magnifier bResult = m_MagInitializeFunc(); if (!bResult) { TRACE(L"MagInitialize() failed\n"); return false; } // Create host window HMODULE hInst; bResult = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast<char*>(&DefWindowProc), &hInst); if (!bResult) { m_MagUninitializeFunc(); TRACE(L"GetModuleHandle() failed\n"); return false; } // Register host window class WNDCLASSEX wcex = {}; wcex.cbSize = sizeof(WNDCLASSEX); wcex.lpfnWndProc = &DefWindowProc; wcex.hInstance = hInst; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.lpszClassName = kMagnifierHostClass; RegisterClassEx(&wcex); m_hHostWindow = CreateWindowEx(WS_EX_LAYERED, kMagnifierHostClass, kHostWindowName, 0, 0, 0, 0, 0, NULL, NULL, hInst, NULL); if (!m_hHostWindow) { m_MagUninitializeFunc(); TRACE(L"Failed to create host window\n"); return false; } // Create the magnifier control in host window created above m_hMagnifierControlWindow = CreateWindow(kMagnifierWindowClass, kMagnifierWindowName, WS_CHILD | WS_VISIBLE | MS_SHOWMAGNIFIEDCURSOR, 0, 0, 0, 0, m_hHostWindow, NULL, NULL, NULL); if (!m_hMagnifierControlWindow) { m_MagUninitializeFunc(); TRACE(L"CreteWindow for Magnifier Control failed\n"); return false; } // Hide host window ShowWindow(m_hHostWindow, SW_HIDE); // Set callback to receive captured image bResult = m_MagSetImageScalingCallbackFunc(m_hMagnifierControlWindow, &ScreenCapturerMagnifier::OnMagImageScalingCallback); if (!bResult) { m_MagUninitializeFunc(); TRACE(L"Failed to set Magnifier callback\n"); return false; } // Don't forget to try to exclude window here if (m_hExcludedWindow) { bResult = m_MagSetWindowFilterListFunc(m_hMagnifierControlWindow, MW_FILTERMODE_EXCLUDE, 1, &m_hExcludedWindow); if (!bResult) { TRACE(L"Failed to exclude requested window: 0x%x\n", GetLastError()); return false; } } g_Owner = this; m_bMagInitialized = true; return true; }
static void init_ap_data() { #if _WIN32 // Get handle of our DLL first. HMODULE handle; BOOL brc = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPSTR)( & dynamic_link ), // any function inside the library can be used for the address & handle ); if ( !brc ) { // Error occurred. int err = GetLastError(); DYNAMIC_LINK_WARNING( dl_sys_fail, "GetModuleHandleEx", err ); return; } // Now get path to our DLL. DWORD drc = GetModuleFileNameA( handle, ap_data._path, static_cast< DWORD >( PATH_MAX ) ); if ( drc == 0 ) { // Error occurred. int err = GetLastError(); DYNAMIC_LINK_WARNING( dl_sys_fail, "GetModuleFileName", err ); return; } if ( drc >= PATH_MAX ) { // Buffer too short. DYNAMIC_LINK_WARNING( dl_buff_too_small ); return; } // Find the position of the last backslash. char *backslash = strrchr( ap_data._path, '\\' ); if ( !backslash ) { // Backslash not found. LIBRARY_ASSERT( backslash!=NULL, "Unbelievable."); return; } LIBRARY_ASSERT( backslash >= ap_data._path, "Unbelievable."); ap_data._len = (size_t)(backslash - ap_data._path) + 1; *(backslash+1) = 0; #else // Get the library path #if __TBB_WEAK_SYMBOLS_PRESENT if ( !dladdr || !dlerror ) return; #endif /* __TBB_WEAK_SYMBOLS_PRESENT */ Dl_info dlinfo; int res = dladdr( (void*)&dynamic_link, &dlinfo ); // any function inside the library can be used for the address if ( !res ) { char const * err = dlerror(); DYNAMIC_LINK_WARNING( dl_sys_fail, "dladdr", err ); return; } else { LIBRARY_ASSERT( dlinfo.dli_fname!=NULL, "Unbelievable." ); } char const *slash = strrchr( dlinfo.dli_fname, '/' ); size_t fname_len=0; if ( slash ) { LIBRARY_ASSERT( slash >= dlinfo.dli_fname, "Unbelievable."); fname_len = (size_t)(slash - dlinfo.dli_fname) + 1; } size_t rc; if ( dlinfo.dli_fname[0]=='/' ) { // The library path is absolute rc = 0; ap_data._len = 0; } else { // The library path is relative so get the current working directory if ( !getcwd( ap_data._path, sizeof(ap_data._path)/sizeof(ap_data._path[0]) ) ) { DYNAMIC_LINK_WARNING( dl_buff_too_small ); return; } ap_data._len = strlen( ap_data._path ); ap_data._path[ap_data._len++]='/'; rc = ap_data._len; } if ( fname_len>0 ) { if ( ap_data._len>PATH_MAX ) { DYNAMIC_LINK_WARNING( dl_buff_too_small ); ap_data._len=0; return; } strncpy( ap_data._path+rc, dlinfo.dli_fname, fname_len ); ap_data._len += fname_len; ap_data._path[ap_data._len]=0; } #endif /* _WIN32 */ }
int BuildDepTree (BuildTreeConfig* cfg, char *name, struct DepTreeElement *root, struct DepTreeElement *self) { LOADED_IMAGE loaded_image; LOADED_IMAGE *img; IMAGE_DOS_HEADER *dos; HMODULE hmod; BOOL success; DWORD i, j; int soffs_len; soff_entry *soffs; if (self->flags & DEPTREE_PROCESSED) { return 0; } if (cfg->on_self) { char modpath[MAX_PATH]; success = GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, name, &hmod); if (!success) return 1; if (GetModuleFileNameA (hmod, modpath, MAX_PATH) == 0) return 1; if (self->resolved_module == NULL) self->resolved_module = strdup (modpath); dos = (IMAGE_DOS_HEADER *) hmod; loaded_image.FileHeader = (IMAGE_NT_HEADERS *) ((char *) hmod + dos->e_lfanew); loaded_image.Sections = (IMAGE_SECTION_HEADER *) ((char *) hmod + dos->e_lfanew + sizeof (IMAGE_NT_HEADERS)); loaded_image.NumberOfSections = loaded_image.FileHeader->FileHeader.NumberOfSections; loaded_image.MappedAddress = (void *) hmod; if (cfg->machineType != -1 && (int)loaded_image.FileHeader->FileHeader.Machine != cfg->machineType) return 1; } else { success = FALSE; for (i = 0; i < cfg->searchPaths->count && !success; ++i) { success = TryMapAndLoad (name, cfg->searchPaths->path[i], &loaded_image, cfg->machineType); } if (!success) success = TryMapAndLoad (name, NULL, &loaded_image, cfg->machineType); if (!success) { self->flags |= DEPTREE_UNRESOLVED; return 1; } if (self->resolved_module == NULL) self->resolved_module = strdup (loaded_image.ModuleName); } if (cfg->machineType == -1) cfg->machineType = (int)loaded_image.FileHeader->FileHeader.Machine; img = &loaded_image; PushStack (cfg->stack, cfg->stack_len, cfg->stack_size, name); self->mapped_address = loaded_image.MappedAddress; self->flags |= DEPTREE_PROCESSED; soffs_len = img->NumberOfSections; soffs = (soff_entry *) malloc (sizeof(soff_entry) * (soffs_len + 1)); for (i = 0; i < img->NumberOfSections; i++) { soffs[i].start = img->Sections[i].VirtualAddress; soffs[i].end = soffs[i].start + img->Sections[i].Misc.VirtualSize; if (cfg->on_self) soffs[i].off = img->MappedAddress/* + img->Sections[i].VirtualAddress*/; else if (img->Sections[i].PointerToRawData != 0) soffs[i].off = img->MappedAddress + img->Sections[i].PointerToRawData - img->Sections[i].VirtualAddress; else soffs[i].off = NULL; } soffs[img->NumberOfSections].start = 0; soffs[img->NumberOfSections].end = 0; soffs[img->NumberOfSections].off = 0; BuildDepTree32or64 (img, cfg, root, self, soffs, soffs_len); free (soffs); if (!cfg->on_self) UnMapAndLoad (&loaded_image); /* Not sure if a forwarded export warrants an import. If it doesn't, then the dll to which the export is forwarded will NOT * be among the dependencies of this dll and it will be necessary to do yet another ProcessDep... for (i = 0; i < self->exports_len; i++) { if (self->exports[i]->forward_str != NULL && self-.exports[i]->forward == NULL) { char *forward_str_copy = NULL, *export_name = NULL, *rdot = NULL; DWORD export_ordinal = 0; forward_str_copy = strdup (self->exports[i]->forward_str); rdot = strrchr (forward_str_copy, '.'); if (rdot != NULL && rdot[1] != 0) { rdot[0] = 0; export_name = &rdot[1]; if (export_name[0] == '#' && export_name[1] >= '0' && export_name[1] <= '9') { export_ordinal = strtol (&export_name[1], NULL, 10); export_name = NULL; } self->exports[i]->forward = FindExportForward (forward_str_copy, export_name, export_ordinal); } free (forward_str_copy); } } */ for (i = 0; i < self->imports_len; i++) { if (self->imports[i].mapped == NULL && self->imports[i].dll != NULL && (self->imports[i].name != NULL || self->imports[i].ordinal > 0)) { struct DepTreeElement *dll = self->imports[i].dll; for (j = 0; j < dll->exports_len; j++) { if ((self->imports[i].name != NULL && dll->exports[j].name != NULL && strcmp (self->imports[i].name, dll->exports[j].name) == 0) || (self->imports[i].ordinal > 0 && dll->exports[j].ordinal > 0 && self->imports[i].ordinal == dll->exports[j].ordinal)) { self->imports[i].mapped = &dll->exports[j]; break; } } /* if (self->imports[i].mapped == NULL) printf ("Could not match %s (%d) in %s to %s\n", self->imports[i].name, self->imports[i].ordinal, self->module, dll->module); */ } } /* By keeping items in the stack we turn it into a list of all * processed modules, this should be more effective at preventing * us from processing modules multiple times */ /*PopStack (stack, stack_len, stack_size, name);*/ return 0; }