16,548
社区成员




#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;
}