Skip to content

Instantly share code, notes, and snippets.

@Criteo-dotnet-blog
Last active November 16, 2018 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Criteo-dotnet-blog/7ee73b0e0947b3fc58044263abd66a64 to your computer and use it in GitHub Desktop.
Save Criteo-dotnet-blog/7ee73b0e0947b3fc58044263abd66a64 to your computer and use it in GitHub Desktop.
public static SafeProcessHandle OpenProcess(int processId, int access, bool throwIfExited)
{
SafeProcessHandle safeProcessHandle = NativeMethods.OpenProcess(access, false, processId);
int lastWin32Error = Marshal.GetLastWin32Error();
if (!safeProcessHandle.IsInvalid)
return safeProcessHandle;
// error handling
if (processId == 0)
throw new Win32Exception(5);
if (ProcessManager.IsProcessRunning(processId))
throw new Win32Exception(lastWin32Error);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment