1. 程式人生 > >獲得可執行程式EXE的絕對路徑

獲得可執行程式EXE的絕對路徑

環境:win7_64bit + VS2012

獲得當前可執行程式的絕對路徑 

	char cExecPath[MAX_PATH];
	char* pszPathofFile;
    //GetModuleFileName獲得當前可執行程式絕對路徑\\xx\\xx\\xx.exe
	if(GetModuleFileName( 0,  cExecPath, sizeof(cExecPath)))
	{
        //strrchr查詢字元在指定字串中從左面開始的最後一次出現的位置,如果成功,返回該字元最後出現的位置,如果失敗,則返回 NULL	
		pszPathofFile = strrchr(cExecPath, '\\'); /* remove the current EXE name from the path */
        //刪除xx.exe,保留路徑\\xx\\xx\\
		if(pszPathofFile)
		{		
			*(pszPathofFile + 1) = '\0';		
		} 
	}