C++ 如何得知自身程序是被哪个程序启动的(启动程序的路径)?
或如何设置参数,启动就能获知自身是被哪个程序启动的?
int main(int argc, const char* argv[])
{cout<< "启动我的程序是:" <<path[exe]<< endl;
}

解决方案 »

  1.   

    被哪个程序启动的?? 你是说哪个exe吗
      

  2.   


    是的, 比如c.exe b.exe 等程序(可能还有其他)都会启动 a.exe , a.exe 如何知道是谁启动它的?
      

  3.   

    CreateToolhelp32Snapshot、Process32First、Process32Next
    枚举进程,找到自己
    然后看父进程id
      

  4.   

    谢谢!
    就是这个思路 关键词 父进程ID
    参考
    http://bbs.csdn.net/topics/390226903
    http://wenwen.soso.com/z/q284427879.htm
    根据procesid 得到路径
    HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, parentprocessid);
    char processName[MAX_PATH]={0};
    GetModuleBaseName(processHandle, 0, processName, MAX_PATH) ;
    char  processFullPath[MAX_PATH]={0};
    GetModuleFileNameEx(processHandle, 0, processFullPath, MAX_PATH);cout<< processName<< endl;
    cout<< processFullPath<< endl;
    ====================================================================
    完整测试程序#include<windows.h>
    #include <iostream>
    #include <cassert>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <algorithm>   // STL 通用算法
    #include <vector>      // STL 动态数组容器
    #include <fstream> #include <wtypes.h>
     #include<tlhelp32.h>
     #include <psapi.h>
    #pragma   comment(lib,"psapi.lib")
     using namespace std;
    bool isfileexist(char FILENAME[]);
    #define ProcessBasicInformation 0  
     
     typedef struct  
     {  
         DWORD ExitStatus;  
         DWORD PebBaseAddress;  
         DWORD AffinityMask;  
         DWORD BasePriority;  
         ULONG UniqueProcessId;  
         ULONG InheritedFromUniqueProcessId;  
     }   PROCESS_BASIC_INFORMATION;  
     
     typedef LONG (__stdcall *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG);
    //
     DWORD GetParentProcessID(DWORD dwProcessId);
     int   main() 
    { //  输入文件名
    HANDLE   hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0   ); 
    PROCESSENTRY32   procentry;//=sizeof(PROCESSENTRY32); 
    BOOL   bFlag=Process32First(   hSnapShot,   &procentry   )   ; 
    DWORD       parentprocessid;
    while(   bFlag   ) 

                if(stricmp(procentry.szExeFile, "unique.exe")==0) //你的程序名称放这里,可以编程获得
    {
    DWORD       processid=procentry.th32ProcessID;         //找到 
    cout <<procentry.th32ProcessID <<endl;
    cout <<GetParentProcessID(procentry.th32ProcessID)<< endl;
    parentprocessid=GetParentProcessID(procentry.th32ProcessID);
    }
                bFlag=Process32Next(hSnapShot,&procentry); 

    cout <<parentprocessid<< endl;HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, parentprocessid);
    char processName[MAX_PATH]={0};
    GetModuleBaseName(processHandle, 0, processName, MAX_PATH) ;
    char  processFullPath[MAX_PATH]={0};
    GetModuleFileNameEx(processHandle, 0, processFullPath, MAX_PATH);cout<< processName<< endl;
    cout<< processFullPath<< endl;
    //cout << Environment::CommandLine<< endl;
    system("pause");
      return 0;

        DWORD GetParentProcessID(DWORD dwProcessId)
        {
            LONG                        status;
            DWORD                        dwParentPID = (DWORD)-1;
            HANDLE                        hProcess;
            PROCESS_BASIC_INFORMATION    pbi;
     
            PROCNTQSIP NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress(  
                GetModuleHandle("ntdll"), "NtQueryInformationProcess"); 
     
            if(NULL == NtQueryInformationProcess)
            {
                return (DWORD)-1;
            }
            // Get process handle
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE, dwProcessId);
            if (!hProcess)
            {
                return (DWORD)-1;
            }
     
            // Retrieve information
            status = NtQueryInformationProcess( hProcess,
                ProcessBasicInformation,
                (PVOID)&pbi,
                sizeof(PROCESS_BASIC_INFORMATION),
                NULL
                );
     
            // Copy parent Id on success
            if  (!status)
            {
                dwParentPID = pbi.InheritedFromUniqueProcessId;
            }
     
            CloseHandle (hProcess);
     
            return dwParentPID;
             
        }
      

  5.   


    是的, 比如c.exe b.exe 等程序(可能还有其他)都会启动 a.exe , a.exe 如何知道是谁启动它的?晕分数给错了别人..................