zoukankan      html  css  js  c++  java
  • 枚举CE进程

      [DllImport("toolhelp.dll")]
            private static extern int Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);

            [DllImport("toolhelp.dll")]
            private static extern int Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);

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

            private const uint TH32CS_SNAPPROCESS = 0x00000002;
            private const uint TH32CS_SNAPNOHEAPS = 0x40000000;
            [StructLayout(LayoutKind.Sequential)]
            private 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;
            }



     IntPtr handle = IntPtr.Zero;
                try
                {
                    handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);
                    PROCESSENTRY32 info = new PROCESSENTRY32();
                    info.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32)) +8;
                   
                    int first = Process32First(handle, ref info);
                    //if (first == 0)
                    //    MessageBox.Show("no pr");
                    //else
                    {
                        do
                        {
                            //if (string.Compare(info.szExeFile, "CamWedge.exe", true) == 0)
                            //    MessageBox.Show("yes");
                            listBox1.Items.Add(info.szExeFile);
                        }
                        while (Process32Next(handle, ref info) != 0);
                    }
                }
                catch
                { throw; }


    网上很多这样的文章,但是我按照网上文章去做,在Process32First函数永远返回的都是0,后来才发现原来
     info.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32)) +8;
    这个地方一定要 +8

    希望给需要的朋友有所帮助,少走冤枉路!



    特别提示 感谢李森给出的提示:

    还有一点要说明的是,在使用完handle后,要CloseToolhelp32Snapshot(handle);
    1[DllImport("toolhelp.dll")]
    2[return: MarshalAs(UnmanagedType.Bool)]
    3public static extern bool CloseToolhelp32Snapshot(IntPtr hSnapshot);
  • 相关阅读:
    js获取触发事件的元素
    js获取触发事件的元素
    jQuery动态显示和隐藏datagrid中的某一列的方法
    jQuery动态显示和隐藏datagrid中的某一列的方法
    在控制器“xxxx”上找不到与该请求匹配的操作
    在控制器“xxxx”上找不到与该请求匹配的操作
    配置virtualbox使得openstackn controller可以连接外网
    配置mysql远程访问
    fuel部完高可用环境后vip__public无法启动从而导致创建虚机时无法创建virtualInterface.
    VMWare/VirtualBox三种网络模式及NAT/host-only模式设置上网与主机互联通信
  • 原文地址:https://www.cnblogs.com/moses/p/1581644.html
Copyright © 2011-2022 走看看