1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #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"); }
memset(szFile, 0, MAX_PATH); 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 { printf("################### after rename ###################\n\n"); OutputSelfpath(); }
system("pause"); return 0; }
|