2008-09-10 50 views

回答

9
Process this_process = Process.GetCurrentProcess(); 
int parent_pid = 0; 
using (ManagementObject MgmtObj = new ManagementObject("win32_process.handle='" + this_process.Id.ToString() + "'")) 
{ 
    MgmtObj.Get(); 
    parent_pid = Convert.ToInt32(MgmtObj["ParentProcessId"]); 
} 
string parent_process_name = Process.GetProcessById(parent_pid).ProcessName; 
+0

請檢查亞當Mitz在下面評論。由於簡單,我仍然發現這個答案是最好的。但有人應該關心。 – 2008-09-10 10:01:42

3

一個與TOOLHELP /的ManagementObject問題辦法是,父進程可能已經退出。

的GetStartupInfo Win32函數(使用的PInvoke如果沒有.NET當量)在包括窗口標題的結構填充。對於Win32控制檯應用程序「app.exe」,從cmd啓動時該標題字符串爲「app」,從explorer(或VS調試器)啓動時,該標題字符串爲「c:\ full \ path \ to \ app.exe」。

當然這是一個劈(受試者在其它版本中,改變等)。

#define WIN32_LEAN_AND_MEAN 
#include <windows.h> 
int main() 
{ 
    STARTUPINFO si; 
    GetStartupInfo(&si); 
    MessageBox(NULL, si.lpTitle, NULL, MB_OK); 
    return 0; 
} 
相關問題