From: Morris on 12 Apr 2010 04:35
Comparing the handle received from GrtProcessByName and GetCurrentProcess on
the current pocess yield with different results.
Anyone knows why?
From: Peter Duniho on 12 Apr 2010 04:02
> Comparing the handle received from GrtProcessByName and GetCurrentProcess on
> the current pocess yield with different results.
> Anyone knows why?
The short answer: because there's no guarantee that the handle returned
should be the same.
The longer answer�
I haven't looked at the exact behavior. But I suspect that
Process.GetCurrentProcess() method calls the unmanaged
GetCurrentProcess() function, while the Process.GetProcessesByName()
method calls the unmanaged OpenProcess() function.
These two functions are different in at least two notable ways:
� GetCurrentProcess() is documented to return a _pseudo_-handle, with
value -1. That is, it doesn't even open a new handle; it just returns a
magic value that when used as a process handle, refers to the current
� OpenProcess() will create a new handle, not necessarily the same
handle value returned in a previous or subsequent call to OpenProcess()
for the same process ID.
Just as you should not compare strings for equality by comparing their
references, you should not compare processes for equality by comparing
the handle you have for each process. Use the process ID instead.