Esempio n. 1
0
        public static IEnumerable <ProcessEntry32> GetProcesses()
        {
            var handleToSnapshot = IntPtr.Zero;

            try
            {
                var procEntry = new ProcessEntry32 {
                    dwSize = (uint)Marshal.SizeOf(typeof(ProcessEntry32))
                };
                handleToSnapshot = Win32ApiImports.CreateToolhelp32Snapshot(SnapshotFlags.Process, 0);

                if (Win32ApiImports.Process32First(handleToSnapshot, ref procEntry))
                {
                    do
                    {
                        yield return(procEntry);
                    }while (Win32ApiImports.Process32Next(handleToSnapshot, ref procEntry));
                }
                else
                {
                    throw new ApplicationException($"Failed with win32 error code {Marshal.GetLastWin32Error()}");
                }
            }
            finally
            {
                Win32ApiImports.CloseHandle(handleToSnapshot);
            }
        }
Esempio n. 2
0
        static ulong GetWorkingSet(uint processId)
        {
            var hProcess = Win32ApiImports.OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, processId);

            if (!Win32ApiImports.GetProcessMemoryInfo(hProcess, out var counters, (uint)Marshal.SizeOf(typeof(ProcessMemoryCounters))))
            {
                return(0);
            }

            Win32ApiImports.CloseHandle(hProcess);

            return(counters.WorkingSetSize);
        }
Esempio n. 3
0
        static void AdjustPrivileges()
        {
            var luid = new Win32ApiHelpers.LUID();

            if (!Win32ApiImports.LookupPrivilegeValue(null, "SeDebugPrivilege", ref luid))
            {
                return;
            }

            var tokenHandle = IntPtr.Zero;

            try
            {
                if (!Win32ApiImports.OpenProcessToken(Win32ApiImports.GetCurrentProcess(), Win32ApiHelpers.TOKEN_ADJUST_PRIVILEGES, out tokenHandle))
                {
                    return;
                }

                var tp = new Win32ApiHelpers.TOKEN_PRIVILEGES
                {
                    PrivilegeCount = 1,
                    Privileges     = new Win32ApiHelpers.LUID_AND_ATTRIBUTES[1]
                };

                tp.Privileges[0].Luid       = luid;
                tp.Privileges[0].Attributes = Win32ApiHelpers.SE_PRIVILEGE_ENABLED;

                Win32ApiImports.AdjustTokenPrivileges(tokenHandle, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
            }
            finally
            {
                if (tokenHandle != IntPtr.Zero)
                {
                    Win32ApiImports.CloseHandle(tokenHandle);
                }
            }
        }