int CSDShellExt::GetExtensionDirectory(wchar_t *path) { HMODULE moduleHandle = NULL; HANDLE processHandle = NULL; processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, GetCurrentProcessId()); if(processHandle == NULL) { // // failed to get process handle // return 0; } if(GetModuleHandleEx(0, L"SDShellNative.dll", &moduleHandle) == 0) { if(GetModuleHandleEx(0, L"SDShellNative32.dll", &moduleHandle) == 0) { // // try get for 64 bit version instead // if(GetModuleHandleEx(0, L"SDShellNative64.dll", &moduleHandle) == 0) { // // failed // CloseHandle(processHandle); return 0; } } } // // get the file path // if(GetModuleFileNameEx(processHandle, moduleHandle, path, MAX_PATH) == 0) { // // failed // CloseHandle(processHandle); return 0; } // // extract directory // int length = wcslen(path); for(int i = length - 1;i > 0;i--) { if(path[i] == '\\') { // // end the string here // path[i + 1] = 0; break; } } CloseHandle(processHandle); return 1; }
void ofxPS3::StartSettingsDialog() { HWND hwnd; MSG msg; WNDCLASS wndclass; char szAppName[64] = "PS3 Eye settings: "; strcat(szAppName,GUIDToString(guid).c_str()); wndclass.style = 0; wndclass.lpfnWndProc = ofxPS3::WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; HMODULE hInstance; GetModuleHandleEx(0,NULL,&hInstance); wndclass.hInstance = hInstance; wndclass.hIcon = LoadIconA(hInstance, szAppName); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); //wndclass.lpszMenuName = szAppName; //wndclass.lpszClassName = szAppName; RegisterClass(&wndclass); InitCommonControls(); hwnd = CreateWindowA(szAppName, szAppName, DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_VISIBLE, 0, 0, 465, cameraPixelMode ? 410 : 110, NULL, NULL, hInstance, 0); SetWindowLongPtr(hwnd,GWLP_USERDATA,(LONG_PTR)(this)); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) { HMODULE module; int length = -1; #if defined(_MSC_VER) #pragma warning(push) #pragma warning(disable: 4054) #endif if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR) #if defined(_MSC_VER) _ReturnAddress() #else __builtin_extract_return_addr(__builtin_return_address(0)) #endif , &module)) #if defined(_MSC_VER) #pragma warning(pop) #endif { length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length); } return length; }
DWORD WINAPI QueryRecycleBinThreadProc(void* pParam) { // NOTE: Do not use CRT functions (since thread was created with CreateThread())! SHQUERYRBINFO rbi = {0}; rbi.cbSize = sizeof(SHQUERYRBINFO); SHQueryRecycleBin(NULL, &rbi); g_BinCount = (double)rbi.i64NumItems; g_BinSize = (double)rbi.i64Size; EnterCriticalSection(&g_CriticalSection); HMODULE module = NULL; if (g_FreeInstanceInThread) { DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; GetModuleHandleEx(flags, (LPCWSTR)DllMain, &module); g_FreeInstanceInThread = false; } g_Thread = false; LeaveCriticalSection(&g_CriticalSection); if (module) { // Decrement the ref count and possibly unload the module if this is // the last instance. FreeLibraryAndExitThread(module, 0); } return 0; }
static dynamic_link_handle global_symbols_link( const wchar_t* library, const dynamic_link_descriptor descriptors[], size_t required ) { ::tbb::internal::suppress_unused_warning( library ); dynamic_link_handle library_handle; #if _WIN32 if ( GetModuleHandleEx( 0, library, &library_handle ) ) { if ( resolve_symbols( library_handle, descriptors, required ) ) return library_handle; else FreeLibrary( library_handle ); } #else /* _WIN32 */ #if !__TBB_DYNAMIC_LOAD_ENABLED /* only __TBB_WEAK_SYMBOLS_PRESENT is defined */ if ( !dlopen ) return 0; #endif /* !__TBB_DYNAMIC_LOAD_ENABLED */ library_handle = dlopen( NULL, RTLD_LAZY ); #if !__ANDROID__ // On Android dlopen( NULL ) returns NULL if it is called during dynamic module initialization. LIBRARY_ASSERT( library_handle, "The handle for the main program is NULL" ); #endif // Check existence of the first symbol only, then use it to find the library and load all necessary symbols. pointer_to_handler handler; dynamic_link_descriptor desc; desc.name = descriptors[0].name; desc.handler = &handler; if ( resolve_symbols( library_handle, &desc, 1 ) ) return pin_symbols( library_handle, desc, descriptors, required ); #endif /* _WIN32 */ return 0; }
void loadPathHack (char buf[256], void *addr) { HMODULE h; size_t sz; DWORD k; BOOL brc; char libBuf[1024]; buf[0] = '\0'; brc = GetModuleHandleEx (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)addr, &h); if (brc) { /* OK: got a handle */ k = GetModuleFileName (h, libBuf, sizeof(libBuf)); if (k > 0) { /* success */ /* do some trimming later */ sz = strlen(libBuf); if (sz <= 255) { memcpy (buf, libBuf, sz); buf[sz] = '\0'; } } } return; } /* loadPathHack */
PLUGIN_EXPORT void Finalize(void* data) { Measure* measure = (Measure*)data; std::unique_lock<std::recursive_mutex> lock(measure->mutex); if (measure->threadActive) { // Increment ref count of this module so that it will not be // unloaded prior to thread completion. DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS; HMODULE module; GetModuleHandleEx(flags, (LPCWSTR)DllMain, &module); if (measure->hProc != INVALID_HANDLE_VALUE && !TerminateApp(measure->hProc, measure->dwPID, (measure->state == SW_HIDE))) { measure->value = 105.0f; RmLogF(measure->rm, LOG_ERROR, err_Terminate, measure->program.c_str()); // Could not terminate process (very rare!) } // Tell the thread to perform any cleanup measure->threadActive = false; return; } lock.unlock(); delete measure; }
/// @param hDXGI must be a valid module handle. void hookDXGI(HMODULE hDXGI, bool preonly) { wchar_t modulename[MODULEFILEPATH_BUFLEN]; GetModuleFileNameW(NULL, modulename, ARRAY_NUM_ELEMENTS(modulename)); ods("DXGI: hookDXGI in App '%ls'", modulename); // Add a ref to ourselves; we do NOT want to get unloaded directly from this process. HMODULE hTempSelf = NULL; GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCTSTR>(&hookDXGI), &hTempSelf); bHooked = true; // Can we use the prepatch data? GetModuleFileNameW(hDXGI, modulename, ARRAY_NUM_ELEMENTS(modulename)); if (_wcsicmp(dxgi->wcFileName, modulename) == 0) { // The module seems to match the one we prepared d3dd for. unsigned char *raw = (unsigned char *) hDXGI; HookPresentRaw((voidFunc)(raw + dxgi->offsetPresent)); HookResizeRaw((voidFunc)(raw + dxgi->offsetResize)); } else if (! preonly) { ods("DXGI: Interface changed, can't rawpatch. Current: %ls ; Previously: %ls", modulename, dxgi->wcFileName); } else { bHooked = false; } }
bool Renderer::initializeCompiler() { #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES) // Find a D3DCompiler module that had already been loaded based on a predefined list of versions. static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i) { if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule)) { break; } } #else // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with. mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL); #endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES if (!mD3dCompilerModule) { ERR("No D3D compiler module found - aborting!\n"); return false; } mD3DCompileFunc = reinterpret_cast<pCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile")); ASSERT(mD3DCompileFunc); return mD3DCompileFunc != NULL; }
static HMODULE WINAPI MyLoadLibraryA(LPCSTR lpLibFileName) { if (VERBOSITY >= 2) { debugPrintf("%s(\"%s\")\n", __FUNCTION__, lpLibFileName); } if (VERBOSITY > 0) { const char *szBaseName = getBaseName(lpLibFileName); for (unsigned i = 0; i < numReplacements; ++i) { if (stricmp(szBaseName, replacements[i].szMatchModule) == 0) { debugPrintf("%s(\"%s\")\n", __FUNCTION__, lpLibFileName); #ifdef __GNUC__ void *caller = __builtin_return_address (0); HMODULE hModule = 0; BOOL bRet = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)caller, &hModule); assert(bRet); char szCaller[256]; DWORD dwRet = GetModuleFileNameA(hModule, szCaller, sizeof szCaller); assert(dwRet); debugPrintf(" called from %s\n", szCaller); #endif break; } } } return MyLoadLibrary(lpLibFileName); }
int GetFnOffsetInModule(voidFunc fnptr, wchar_t *refmodulepath, unsigned int refmodulepathLen, const std::string &logPrefix, const std::string &fnName) { HMODULE hModule = NULL; if (! GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (char *) fnptr, &hModule)) { ods((logPrefix + ": Failed to get module for " + fnName).c_str()); return -1; } const bool bInit = refmodulepath[0] == '\0'; if (bInit) { GetModuleFileNameW(hModule, refmodulepath, refmodulepathLen); } else { wchar_t modulename[MODULEFILEPATH_BUFLEN]; GetModuleFileNameW(hModule, modulename, ARRAY_NUM_ELEMENTS(modulename)); if (_wcsicmp(modulename, refmodulepath) != 0) { ods((logPrefix + ": " + fnName + " functions module path does not match previously found. Now: '%ls', Previously: '%ls'").c_str(), modulename, refmodulepath); return -2; } } unsigned char *fn = reinterpret_cast<unsigned char *>(fnptr); unsigned char *base = reinterpret_cast<unsigned char *>(hModule); unsigned long off = static_cast<unsigned long>(fn - base); // XXX: convert this function to use something other than int. // Issue mumble-voip/mumble#1924. if (off > static_cast<unsigned long>(std::numeric_limits<int>::max())) { ods("Internal overlay error: GetFnOffsetInModule() offset greater than return type can hold."); return -1; } return static_cast<int>(off); }
ds::ds() { HMODULE hModule = NULL; BOOL bRc = GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, ( LPCSTR)this, &hModule ); if ( bRc ) { GetModuleFileName(hModule, g_szProjectName, MAX_PATH); FreeLibrary(hModule); DebugOut( "\n*******************Memory Check %s!\n\n", g_szProjectName ); } else { OutputDebugString("\n*******************GetModuleHandleEx Failed!\n\n"); } _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR ); /* * Set the debug-heap flag to keep freed blocks in the * heap's linked list - This will allow us to catch any * inadvertent use of freed memory */ //int tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); //tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF; //_CrtSetDbgFlag(tmpDbgFlag); }
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { char cBuff[MAX_PATH]; DWORD dwPID; HANDLE hProcs; // get the calling exe path GetModuleFileName(NULL, cBuff, sizeof(cBuff)); // get PID dwPID = GetProcessPathID(cBuff); if (!dwPID) { MessageBox(0, "Couldn't get PID :(", NULL, MB_ICONERROR); return -1; } // get procs.dll base hProcs = GetModuleHandleEx(dwPID, "procs.dll"); if (!hProcs) { MessageBox(0, "GetModuleHandleEx failed :(", NULL, MB_ICONERROR); return -1; } // print result wsprintf(cBuff, "My PID: 0x%08lX\nProcs.dll base: 0x%08lX", dwPID, hProcs); MessageBox(0, cBuff, ":D", MB_ICONINFORMATION | MB_SYSTEMMODAL); return 0; }
bool addLibrary(char* libraryName,void* addressContained) { HMODULE module; if(GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,(LPCSTR)addressContained,&module)) { HANDLE process = GetCurrentProcess(); _MODULEINFO moduleInfo; if(GetModuleInformation(process,module,&moduleInfo,sizeof moduleInfo)) { CloseHandle(process); LibraryInfo* libraryInfo = new LibraryInfo; libraryInfo->baseAddress = (void*) moduleInfo.lpBaseOfDll; libraryInfo->length = moduleInfo.SizeOfImage; libraryInfo->handle = module; LibraryNameToLibraryInfo->insert(libraryName,libraryInfo); return true; } } return false; }
int _zbar_processor_open (zbar_processor_t *proc, char *title, unsigned width, unsigned height) { HMODULE hmod = NULL; if(!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (void*)_zbar_processor_open, (HINSTANCE*)&hmod)) return(err_capture(proc, SEV_ERROR, ZBAR_ERR_WINAPI, __func__, "failed to obtain module handle")); ATOM wca = win_register_class(hmod); if(!wca) return(err_capture(proc, SEV_ERROR, ZBAR_ERR_WINAPI, __func__, "failed to register window class")); RECT r = { 0, 0, width, height }; AdjustWindowRectEx(&r, WIN_STYLE, 0, EXT_STYLE); proc->display = CreateWindowEx(EXT_STYLE, (LPCTSTR)(long)wca, "ZBar", WIN_STYLE, CW_USEDEFAULT, CW_USEDEFAULT, r.right - r.left, r.bottom - r.top, NULL, NULL, hmod, proc); if(!proc->display) return(err_capture(proc, SEV_ERROR, ZBAR_ERR_WINAPI, __func__, "failed to open window")); return(0); }
LCBUV_API lcb_error_t lcb_create_libuv_io_opts(int version, lcb_io_opt_t *io, lcbuv_options_t *options) { lcb_io_opt_t iop; uv_loop_t *loop = NULL; my_iops_t *ret; if (version != 0) { return LCB_PLUGIN_VERSION_MISMATCH; } #ifdef _WIN32 { /** UV unloading on Windows doesn't work well */ HMODULE module; /* We need to provide a symbol */ static int dummy; BOOL result; result = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, (LPCSTR)&dummy, &module); if (!result) { return LCB_EINTERNAL; } } #endif ret = calloc(1, sizeof(*ret)); if (!ret) { return LCB_CLIENT_ENOMEM; } iop = &ret->base; iop->version = 2; iop->destructor = iops_lcb_dtor; iop->v.v2.get_procs = wire_iops2; ret->iops_refcount = 1; *io = iop; if (options) { if (options->v.v0.loop) { ret->external_loop = 1; loop = options->v.v0.loop; } ret->startstop_noop = options->v.v0.startsop_noop; } if (!loop) { loop = uv_loop_new(); } ret->loop = loop; return LCB_SUCCESS; }
bool Renderer::initializeCompiler() { TRACE_EVENT0("gpu", "initializeCompiler"); #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES) // Find a D3DCompiler module that had already been loaded based on a predefined list of versions. static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i) { if (GetModuleHandleEx(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 #if !defined(ANGLE_OS_WINRT) const wchar_t *defaultCompiler = _wgetenv(L"QT_D3DCOMPILER_DLL"); if (!defaultCompiler) defaultCompiler = QT_D3DCOMPILER_DLL; #else // !ANGLE_OS_WINRT # ifdef _DEBUG const wchar_t *defaultCompiler = L"d3dcompiler_qtd.dll"; # else const wchar_t *defaultCompiler = L"d3dcompiler_qt.dll"; # endif #endif // ANGLE_OS_WINRT 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) { // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with. mD3dCompilerModule = LoadLibrary(compilerDlls[i]); if (mD3dCompilerModule) break; } if (!mD3dCompilerModule) { ERR("No D3D compiler module found - aborting!\n"); return false; } mD3DCompileFunc = reinterpret_cast<pCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile")); ASSERT(mD3DCompileFunc); return mD3DCompileFunc != NULL; }
/* * GetModuleFromAddress * Finds module handle from some address inside it */ static HMODULE GetModuleFromAddress(LPVOID address) { HMODULE module; if(GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (char*)address, &module)) return module; return nullptr; }
static HMODULE GetCurrentModule() { HMODULE hModule = NULL; GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)GetCurrentModule, &hModule); return hModule; }
HMODULE wutil_LibModuleHandle() { HMODULE hModule; const DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; const BOOL ok = GetModuleHandleEx(flags, (LPCWSTR)&wutil_LibModuleHandle, &hModule); // (avoid ENSURE etc. because we're called from debug_DisplayError) wdbg_assert(ok); return hModule; }
static HMODULE GetModuleFromAddress(void * address) { DWORD flags = GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS; HMODULE module = 0; GetModuleHandleEx(flags, (LPCWSTR)address, &module); return module; }
HINSTANCE console::_get_instance() { HINSTANCE instance = NULL; GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast<LPCWSTR>(&_get_instance), &instance); return instance; }
int OPENSSL_atexit(void (*handler)(void)) { OPENSSL_INIT_STOP *newhand; #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE) { union { void *sym; void (*func)(void); } handlersym; handlersym.func = handler; # ifdef DSO_WIN32 { HMODULE handle = NULL; BOOL ret; /* * We don't use the DSO route for WIN32 because there is a better * way */ ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, handlersym.sym, &handle); if (!ret) return 0; } # else /* * Deliberately leak a reference to the handler. This will force the * library/code containing the handler to remain loaded until we run the * atexit handler. If -znodelete has been used then this is * unnecessary. */ { DSO *dso = NULL; ERR_set_mark(); dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE); DSO_free(dso); ERR_pop_to_mark(); } # endif } #endif newhand = OPENSSL_malloc(sizeof(*newhand)); if (newhand == NULL) return 0; newhand->handler = handler; newhand->next = stop_handlers; stop_handlers = newhand; return 1; }
/*---------------------------------------------------------------------------*/ void __stdcall dll_loader_unload_dsc(void *addr) { HMODULE handle; GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, addr, &handle); FreeLibrary(handle); }
unsigned int WINAPI BackgroundJobProcessor::StaticThreadProc(void *lpParam) { Assert(lpParam); #ifdef _M_X64_OR_ARM64 #ifdef RECYCLER_WRITE_BARRIER Memory::RecyclerWriteBarrierManager::OnThreadInit(); #endif #endif #if !defined(_UCRT) HMODULE dllHandle = NULL; if (!GetModuleHandleEx(0, AutoSystemInfo::GetJscriptDllFileName(), &dllHandle)) { dllHandle = NULL; } #endif ParallelThreadData * threadData = static_cast<ParallelThreadData *>(lpParam); BackgroundJobProcessor *const processor = threadData->processor; // Indicate to the constructor that the thread has fully started. threadData->threadStartedOrClosing.Set(); #if DBG threadData->backgroundPageAllocator.SetConcurrentThreadId(GetCurrentThreadId()); #endif #ifdef DISABLE_SEH processor->Run(threadData); #else __try { processor->Run(threadData); } __except(ExceptFilter(GetExceptionInformation())) { Assert(false); } #endif // Indicate to Close that the thread is about to exit. This has to be done before CoUninitialize because CoUninitialize // may require the loader lock and if Close was called while holding the loader lock during DLL_THREAD_DETACH, it could // end up waiting forever, causing a deadlock. threadData->threadStartedOrClosing.Set(); #if !defined(_UCRT) if (dllHandle) { FreeLibraryAndExitThread(dllHandle, 0); } else #endif { return 0; } }
CSpyHookBase::CSpyHookBase() : m_bInit(false) , m_hHookHandle(NULL) , m_hModuleHandle(NULL) { uint32_t eipForHandleFetch = 0; BEATS_ASSI_GET_EIP(eipForHandleFetch); m_bInit = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR) eipForHandleFetch, &m_hModuleHandle) == TRUE; BEATS_ASSERT(m_bInit); }
DWORD WINAPI NetworkThreadProc(void* pParam) { // NOTE: Do not use CRT functions (since thread was created by CreateThread())! MeasureData* measure = (MeasureData*)pParam; const DWORD bufferSize = sizeof(ICMP_ECHO_REPLY) + 32; BYTE buffer[bufferSize]; double value = 0.0; HANDLE hIcmpFile = IcmpCreateFile(); if (hIcmpFile != INVALID_HANDLE_VALUE) { IcmpSendEcho(hIcmpFile, measure->destAddr, NULL, 0, NULL, buffer, bufferSize, measure->timeout); IcmpCloseHandle(hIcmpFile); ICMP_ECHO_REPLY* reply = (ICMP_ECHO_REPLY*)buffer; value = (reply->Status != IP_SUCCESS) ? measure->timeoutValue : reply->RoundTripTime; if (!measure->finishAction.empty()) { RmExecute(measure->skin, measure->finishAction.c_str()); } } HMODULE module = NULL; EnterCriticalSection(&g_CriticalSection); if (measure->threadActive) { measure->value = value; measure->threadActive = false; } else { // Thread is not attached to an existing measure any longer, so delete // unreferenced data. delete measure; DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; GetModuleHandleEx(flags, (LPCWSTR)DllMain, &module); } LeaveCriticalSection(&g_CriticalSection); if (module) { // Decrement the ref count and possibly unload the module if this is // the last instance. FreeLibraryAndExitThread(module, 0); } return 0; }
// GetCurrentModule // // Helper to get the module handle for the application. // HMODULE ResourceFontFileStream::GetCurrentModule() { HMODULE handle = NULL; // Determine the module handle from the address of this function. GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCTSTR>(&GetCurrentModule), &handle ); return handle; }
BOOL CALLBACK menum(PCTSTR ModuleName, DWORD64 ModuleBase, ULONG ModuleSize, PVOID UserContext) { dbg("Module: <%s>", ModuleName); HMODULE mod = NULL; GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)ModuleBase, &mod); if(mod) dumpImports(mod); else dbg(" Cannot find module?"); return TRUE; }
// AddRef - Calls to IMalloc::AddRef end up here. This function is just a // wrapper around the real IMalloc::AddRef implementation. // // Return Value: // // Returns the value returned by the system implementation of // IMalloc::AddRef. // ULONG VisualLeakDetector::AddRef () { PRINT_HOOKED_FUNCTION(); assert(m_iMalloc != NULL); if (m_iMalloc) { // Increment the library reference count to defer unloading the library, // since this function increments the reference count of the IMalloc interface. HMODULE module = NULL; GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)m_vldBase, &module); return m_iMalloc->AddRef(); } return 0; }