获取当前应用程序的文件名
获取应用程序名称,如果文件名在运行时被改变,QueryFullProcessImageName()同样可以获取文件名。
#include "stdafx.h" #include <Windows.h> #include <Psapi.h> #include <stdio.h> #pragma comment(lib, "Psapi.lib") void OutputSelfpath() { char szFile[MAX_PATH] = {0}; GetModuleFileName(NULL, szFile, MAX_PATH); printf("GetModuleFileName:\n\r%s\n\n", szFile); memset(szFile, 0, MAX_PATH); HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()); if (!hProcess) { printf("OpenProcess failed!\n"); } else { DWORD dwRet = GetProcessImageFileName(hProcess, szFile, MAX_PATH); if (dwRet) { printf("GetProcessImageFileName:\n\r%s\n\n", szFile); } else { printf("GetProcessImageFileName failed!\n"); } DWORD dwSize = MAX_PATH; if (QueryFullProcessImageName(hProcess, 0, szFile, &dwSize)) { printf("QueryFullProcessImageName:\n\r%s\n\n", szFile); } else { printf("QueryFullProcessImageName failed\n", szFile); } } } int main() { const char* pszFile = "ConsoleTest.exe"; const char* pszNewFile = "ConsoleTest_bak.exe"; remove(pszNewFile); OutputSelfpath(); int nRet = rename(pszFile, pszNewFile); if (0 != nRet) { printf("rename file failed!\n"); } else { OutputSelfpath(); } system("pause"); return 0; }
以上内容转自CSDN,原帖地址:http://bbs.csdn.net/topics/390801866
感谢@mzlogin 的分享。