通常为了或者程序运行时目录,我们一般采用GetModuleFileName函数,但是当在DLL或者COM组建里调用该函数,
或者的是EXE的目录,不是当前COM组建或者DLL的目录。
可以使用下面的方法或者真正的目录。
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:
// http://www.dotnet247.com/247reference/msgs/13/65259.aspx
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
}
调用代码:
/* HMODULE h = GetCurrentModule();
TCHAR lpFile[MAX_PATH];
GetModuleFileName(h, lpFile, MAX_PATH);*/