20090807 c# Parent Process


嵌套WEBBROWSER随便写了个程序,可是新窗口总是弹出IE。


程序退出后这些IE窗口就是个麻烦,不能同时退出。


想根据父进程来杀之


<code>


class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetParentProcess().ProcessName);
            Console.Read();
        }


        private static Process GetParentProcess()
        {
            int iParentPid = 0;
            int iCurrentPid = 3836;// Process.GetCurrentProcess().Id;


            IntPtr oHnd = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);


            if (oHnd == IntPtr.Zero)
                return null;


            PROCESSENTRY32 oProcInfo = new PROCESSENTRY32();


            oProcInfo.dwSize =
            (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(PROCESSENTRY32));


            if (Process32First(oHnd, ref oProcInfo) == false)
                return null;


            do
            {
                if (iCurrentPid == oProcInfo.th32ProcessID)
                    iParentPid = (int)oProcInfo.th32ParentProcessID;
            }
            while (iParentPid == 0 && Process32Next(oHnd, ref oProcInfo));


            if (iParentPid > 0)
                return Process.GetProcessById(iParentPid);
            else
                return null;
        }


        static uint TH32CS_SNAPPROCESS = 2;


        [StructLayout(LayoutKind.Sequential)]
        public struct PROCESSENTRY32
        {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szExeFile;
        };


        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);


        [DllImport("kernel32.dll")]
        static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);


        [DllImport("kernel32.dll")]
        static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);


    }


</code>


可是获得的的SVHOST·· 晕·


还有一个方法时通过WMI获得··


<code>


public static void Main() {
Process p = Process.GetCurrentProcess();
int parentPid = GetParentProcess(p.Id);
Console.WriteLine(parentPid);
}
static int GetParentProcess(int Id)
{
int parentPid=0;
using(ManagementObject mo = new ManagementObject(“win32_process.handle=’”
+ Id.ToString() + “‘”))
{
mo.Get();
parentPid = Convert.ToInt32(mo["ParentProcessId"]);
DumpProcessProperties(parentPid);
}
return parentPid;
}
// dump proces properties to the console
static void DumpProcessProperties(int Id)
{
using(ManagementObject mo = new ManagementObject(“win32_process.handle=’”
+ Id.ToString() + “‘”))
{
mo.Get();
foreach(PropertyData pd in mo.Properties)
{
Console.WriteLine(“Property: {0}, Value: [{1}]“,pd.Name, pd.Value);
}
}
}


</code>

历史博文

标签:, ,
八月 18, 2009 at 12:00 上午 by yippee 1,039 次
Category: Dev
Tags: , ,