vc++返回模块路径

时间:2021-08-17 17:17:29

#include "stdafx.h"
#include <Windows.h>
#include <string.h>
const int MAXPATHLEN=256;
int _tmain(int argc, _TCHAR* argv[])
{
  TCHAR * appPath=new TCHAR[MAXPATHLEN];
  memset(appPath,0,sizeof(TCHAR)*MAXPATHLEN);
  if ( !GetModuleFileName(NULL,appPath,MAXPATHLEN))
  {
    _tprintf(_T("GetModuleFileName failed. (%d)\n"),GetLastError());
  }
  else
  {
    (_tcsrchr(appPath, _T('\\')))[1] = 0;
    _stprintf(appPath,_T("%scmd.exe"),appPath);
    _tprintf(_T("%s"),appPath);
  }
  delete []appPath;
  appPath=NULL;
  return 0;
}