我用的方法是得到当前所有进程的快照。然后查找每个进程子进程,如果当前进程(我的进程)是某个进程的子进程,则认为我的程序是被别的程序调用的。如果没找到就认为是独立运行的。
但这个方法很慢,有没有快一点的方法?

解决方案 »

  1.   

    请教楼主,我们通过双击图标的方式打开的进程,它的父进程不是shell么?shell(explorer)难道不是用 CreateProcess来打开程序的吗?
      

  2.   

    我觉得只要能得到父进程的ID或句柄,判断这个父进程是谁应该很容易呀。最起码可以通过ModulName来判断呀。但关键是怎样得到父进程
      

  3.   

    Process32First 这些函数不是可以枚举进程ID,并得到父进程的ID吗?
      

  4.   

    #include <windows.h>
    #include <tlhelp32.h>
    #include <stdio.h>BOOL GetProcessList () 

        HANDLE         hProcessSnap = NULL; 
        BOOL           bRet      = FALSE; 
        PROCESSENTRY32 pe32      = {0}; 
     
        //  Take a snapshot of all processes in the system.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);     if (hProcessSnap == INVALID_HANDLE_VALUE) 
            return (FALSE); 
     
        //  Fill in the size of the structure before using it.     pe32.dwSize = sizeof(PROCESSENTRY32); 
     
        //  Walk the snapshot of the processes, and for each process, 
        //  display information.     if (Process32First(hProcessSnap, &pe32)) 
        {         do 
            {                 // Print the process's information. 
                    printf( "\nPriority Class Base\t%d\n", pe32.pcPriClassBase); 
                    printf( "PID\t\t\t%d\n", pe32.th32ProcessID);
                    printf( "Parent PID\t\t%d\n",pe32.th32ParentProcessID);
                    printf( "Thread Count\t\t%d\n", pe32.cntThreads);
                    printf( "Full Path\t\t%s\n\n", pe32.szExeFile);
            } 
            while (Process32Next(hProcessSnap, &pe32)); 
            bRet = TRUE; 
        } 
        else 
            bRet = FALSE;    // could not walk the list of processes 
     
        // Do not forget to clean up the snapshot object.     CloseHandle (hProcessSnap); 
        return (bRet); 
    } int main()
    {
        return GetProcessList();
    }