Search This Blog

Saturday, February 21, 2009

MFC code to get the Path of Appliction and DLL






//MFC code to get the Path of Appliction
CString GetExePath ()
{
   TCHAR szPath[_MAX_PATH];
   szPath[0] = '\0';

   GetModuleFileName( NULL, szPath, sizeof(szPath) );

   CString csPath(szPath);

   int nPos = csPath.ReverseFind('\\');
   if ( nPos != -1)
   {
       csPath = csPath.Left(nPos+1);
   }
   else
   {
       csPath = "";
   }

   return csPath ;

}


//MFC code to get the Path of DLL if it is loaded from different path
#include <atlbase.h>
CString GetDLLPath ()
{
   TCHAR szPath[_MAX_PATH];
   szPath[0] = '\0';

   GetModuleFileName((HINSTANCE)&__ImageBase, szPath, sizeof(szPath) );

   CString csPath(szPath);

   int nPos = csPath.ReverseFind('\\');
   if ( nPos != -1)
   {
       csPath = csPath.Left(nPos+1);
   }
   else
   {
       csPath = "";
   }

   return csPath ;

}

No comments: