zoukankan      html  css  js  c++  java
  • 获取当前应用程序的文件名

    获取应用程序名称,如果文件名在运行时被改变,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:
    
    %s
    
    ", szFile);
    
        memset(szFile, 0, MAX_PATH);
    
        HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
        if (!hProcess)
        {
            printf("OpenProcess failed!
    ");
        }
        else
        {
            DWORD dwRet = GetProcessImageFileName(hProcess, szFile, MAX_PATH);
            if (dwRet)
            {
                printf("GetProcessImageFileName:
    
    %s
    
    ", szFile);
            }
            else
            {
                printf("GetProcessImageFileName failed!
    ");
            }
    
            DWORD dwSize = MAX_PATH;
            if (QueryFullProcessImageName(hProcess, 0, szFile, &dwSize))
            {
                printf("QueryFullProcessImageName:
    
    %s
    
    ", szFile);
            }
            else
            {
                printf("QueryFullProcessImageName failed
    ", 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!
    ");
        }
        else
        {
            OutputSelfpath();
        }
    
        system("pause");
        return 0;
    }

    以上内容转自CSDN,原帖地址:http://bbs.csdn.net/topics/390801866

    感谢@mzlogin 的分享。

  • 相关阅读:
    shell编程
    git
    Flask-SQLAlchemy
    pipreqs
    命令行操作flask
    SQLAlchemy中scoped_session实现线程安全
    打印信息
    键盘事件
    安卓手机APP压力monkey测试
    手机APP功能测试经验分享2016.06.06
  • 原文地址:https://www.cnblogs.com/autumoonchina/p/8315419.html
Copyright © 2011-2022 走看看