MFC DLL获取当前路径

1.首先定义此获取模块的静态方法

#if _MSC_VER >= 1300    
// for VC 7.0 // from ATL 7.0 sources 
#ifndef _delayimp_h extern "C" IMAGE_DOS_HEADER __ImageBase; 
#endif #endif
static  HMODULE GetCurrentModule() { 
#if _MSC_VER < 1300    // earlier than .NET compiler (VC 6.0)
// Here's a trick that will get you the handle of the module  // you're running in without any a-priori knowledge:  MEMORY_BASIC_INFORMATION mbi;  
static int dummy;  VirtualQuery( &dummy, &mbi, sizeof(mbi) );
return reinterpret_cast<HMODULE>(mbi.AllocationBase); 
#else    
// VC 7.0  // from ATL 7.0 sources  
return reinterpret_cast<HMODULE>(&__ImageBase); 
#endif }

2.可以继续调用以下方法来获取;
HMODULE hm = GetCurrentModule();
 GetModuleFileName(hm,gExePath,sizeof(gExePath));

原文地址:https://www.cnblogs.com/hejoy91/p/5027479.html