码迷,mamicode.com
首页 > 其他好文 > 详细

获取当前应用程序的文件名

时间:2018-01-19 11:39:38      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:out   post   运行时   and   bbs   mod   获取文件   print   else   

获取应用程序名称,如果文件名在运行时被改变,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 的分享。

获取当前应用程序的文件名

标签:out   post   运行时   and   bbs   mod   获取文件   print   else   

原文地址:https://www.cnblogs.com/autumoonchina/p/8315419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!