tools/VsIdeHostAdapter/TargetAddIn/Common/ProcessUtil.cs
/// <summary>
/// Returns PID of the parent process.
/// The information may be unreliable: the process may already exited, etc.
/// </summary>
Returns PID of the parent process.
The information may be unreliable: the process may already exited, etc.
Returns PID of the parent process . The information may be unreliable : the process may already exited etc .
[SuppressMessage("Microsoft.Interoperability", "CA1404:CallGetLastErrorImmediatelyAfterPInvoke")]
        public static int GetParentProcessId(int processId)
        {
            EnsureProcessExists(processId);
            IntPtr snapshot = IntPtr.Zero;
            try
            {
                snapshot = NativeMethods.CreateToolhelp32Snapshot(NativeMethods.TH32CS_SNAPPROCESS, 0);
                if (snapshot.ToInt64() == NativeMethods.INVALID_HANDLE_VALUE)
                {
                    throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
                NativeMethods.PROCESSENTRY32 pe = new NativeMethods.PROCESSENTRY32();
                pe.dwSize = (uint)Marshal.SizeOf(pe);
                bool found = NativeMethods.Process32First(snapshot, ref pe);
                while (found)
                {
                    if (pe.th32ProcessID == processId)
                    {
                        return (int)pe.th32ParentProcessID;
                    }
                    found = NativeMethods.Process32Next(snapshot, ref pe);
                }
                NativeMethods.SetLastError(NativeMethods.ERROR_INVALID_PARAMETER);
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
            finally
            {
                if (snapshot != IntPtr.Zero)
                {
                    NativeMethods.CloseHandle(snapshot);
                }
            }
        }
[ SuppressMessage ( " " , " " ) ] public static int GetParentProcessId ( int processId ) { EnsureProcessExists ( processId ) ; IntPtr snapshot = IntPtr . Zero ; try { snapshot = NativeMethods . CreateToolhelp32Snapshot ( NativeMethods . TH32CS_SNAPPROCESS , 0 ) ; if ( snapshot . ToInt64 ( ) == NativeMethods . INVALID_HANDLE_VALUE ) { throw Marshal . GetExceptionForHR ( Marshal . GetHRForLastWin32Error ( ) ) ; } NativeMethods . PROCESSENTRY32 pe = new NativeMethods . PROCESSENTRY32 ( ) ; pe . dwSize = ( uint ) Marshal . SizeOf ( pe ) ; bool found = NativeMethods . Process32First ( snapshot , ref pe ) ; while ( found ) { if ( pe . th32ProcessID == processId ) { return ( int ) pe . th32ParentProcessID ; } found = NativeMethods . Process32Next ( snapshot , ref pe ) ; } NativeMethods . SetLastError ( NativeMethods . ERROR_INVALID_PARAMETER ) ; throw Marshal . GetExceptionForHR ( Marshal . GetHRForLastWin32Error ( ) ) ; } finally { if ( snapshot != IntPtr . Zero ) { NativeMethods . CloseHandle ( snapshot ) ; } } }
Returns PID of the parent process.
Returns PID of the parent process .
// This may be inconsistent with snapshot but when the call returns it it not guaranteed to be consistent anyway. // Report ArgumentException(processId) in the consistent way.
processId
int