Simplify IsWow64Process interop (#39059)
authorJan Kotas <jkotas@microsoft.com>
Fri, 10 Jul 2020 13:41:34 +0000 (06:41 -0700)
committerGitHub <noreply@github.com>
Fri, 10 Jul 2020 13:41:34 +0000 (06:41 -0700)
Fixes #38910

src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs

index c7165ca..7c759f4 100644 (file)
@@ -82,6 +82,8 @@
              Link="Common\Interop\Windows\Kernel32\Interop.CloseHandle.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.PERF_INFO.cs"
              Link="Common\Interop\Windows\Advapi32\Interop.PERF_INFO.cs" />
+    <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.IsWow64Process_IntPtr.cs"
+             Link="Common\Interop\Windows\Kernel32\Interop.IsWow64Process_IntPtr.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.IsWow64Process_SafeProcessHandle.cs"
              Link="Common\Interop\Windows\Kernel32\Interop.IsWow64Process_SafeProcessHandle.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetExitCodeProcess.cs"
index 38512de..fa17d76 100644 (file)
@@ -93,33 +93,20 @@ namespace System.Diagnostics
                 // do the enumeration at all.  So we'll detect this case and bail out.
                 if (!succeeded)
                 {
-                    SafeProcessHandle hCurProcess = SafeProcessHandle.InvalidHandle;
-                    try
+                    if (!Interop.Kernel32.IsWow64Process(Interop.Kernel32.GetCurrentProcess(), out bool sourceProcessIsWow64))
                     {
-                        hCurProcess = ProcessManager.OpenProcess(Environment.ProcessId, Interop.Advapi32.ProcessOptions.PROCESS_QUERY_INFORMATION, true);
-
-                        if (!Interop.Kernel32.IsWow64Process(hCurProcess, out bool sourceProcessIsWow64))
-                        {
-                            throw new Win32Exception();
-                        }
-
-                        if (!Interop.Kernel32.IsWow64Process(processHandle, out bool targetProcessIsWow64))
-                        {
-                            throw new Win32Exception();
-                        }
+                        throw new Win32Exception();
+                    }
 
-                        if (sourceProcessIsWow64 && !targetProcessIsWow64)
-                        {
-                            // Wow64 isn't going to allow this to happen, the best we can do is give a descriptive error to the user.
-                            throw new Win32Exception(Interop.Errors.ERROR_PARTIAL_COPY, SR.EnumProcessModuleFailedDueToWow);
-                        }
+                    if (!Interop.Kernel32.IsWow64Process(processHandle, out bool targetProcessIsWow64))
+                    {
+                        throw new Win32Exception();
                     }
-                    finally
+
+                    if (sourceProcessIsWow64 && !targetProcessIsWow64)
                     {
-                        if (hCurProcess != SafeProcessHandle.InvalidHandle)
-                        {
-                            hCurProcess.Dispose();
-                        }
+                        // Wow64 isn't going to allow this to happen, the best we can do is give a descriptive error to the user.
+                        throw new Win32Exception(Interop.Errors.ERROR_PARTIAL_COPY, SR.EnumProcessModuleFailedDueToWow);
                     }
 
                     EnumProcessModulesUntilSuccess(processHandle, null, 0, out needed);