獲取當前運行時的目錄

 通常爲了或者程序運行時目錄,我們一般採用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);*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章