C# 如何将 IntPtr 转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18173211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert IntPtr to int
提问by E Mett
A window handle sometimes of type int
and other times of type IntPtr
窗口句柄有时是类型int
,有时是类型IntPtr
int
example:
int
例子:
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);
IntPtr
example:
IntPtr
例子:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
I don't seem to be able to convert/cast from one to the other.
我似乎无法从一个转换/投射到另一个。
When I try this.ProcessID = GetWindowThreadProcessId(windowHandle.ToInt32(),0)
I get an error cannot implicitly convert from uint to int
当我尝试时this.ProcessID = GetWindowThreadProcessId(windowHandle.ToInt32(),0)
出现错误cannot implicitly convert from uint to int
采纳答案by xanatos
The SendMessage
signature is
该SendMessage
签名是
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
or this
或这个
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, StringBuilder lParam);
Don't exchange int
and IntPtr
. They are nearly equivalent only at 32 bits (equal in size). At 64 bits an IntPtr
is nearly equivalent to a long
(equal in size)
不要交换int
和IntPtr
。它们仅在 32 位(大小相等)时几乎等效。在 64 位时,anIntPtr
几乎等于 a long
(大小相等)
The GetWindowThreadProcessId
signature is
该GetWindowThreadProcessId
签名是
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
or
或者
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
In this case, a ref
or a out
to "something" are managed references to something, so they are internally converted to IntPtr
when passed to Native API. So out uint
is, from the point of view of the Native API, equivalent to IntPtr
.
在这种情况下,“某物”的aref
或 a 是out
对某物的托管引用,因此它们IntPtr
在传递给 Native API 时会在内部转换为。所以out uint
,从原生 API 的角度来看,相当于IntPtr
.
Explanation: what is important is that the "length" of the parameters is the correct one. int
and uint
are equal for the called API. And a 32bit IntPtr
is the same too.
说明:重要的是参数的“长度”是正确的。int
并且uint
对于被调用的 API 是相等的。32位IntPtr
也是一样的。
Note that some types (like bool
and char
) have special handling by the marshaler.
请注意,某些类型(如bool
和char
)由封送拆收器进行特殊处理。
You shouldn't EVER convert an int
to an IntPtr
. Keep it as an IntPtr
and live happy. If you have to make some math operations not supported by IntPtr
, use long
(it's 64 bits, so until we will have Windows 128, there won't be any problem :-) ).
您永远不应该将 an 转换int
为IntPtr
. 保持它作为一个IntPtr
并快乐地生活。如果您必须进行一些不支持的数学运算IntPtr
,请使用long
(它是 64 位,因此在我们拥有Windows 128 之前,不会有任何问题:-))。
IntPtr p = ...
long l = (long)p;
p = (IntPtr)l;
回答by RomanHotsiy
I think that error cannot implicitly convert from uint to int
refers to =
statement. The field this.ProcessID
is int
, but GetWindowThreadProcessId
returns uint
.
我认为该错误cannot implicitly convert from uint to int
是指=
声明。该字段this.ProcessID
是int
,但GetWindowThreadProcessId
返回uint
。
Try this
尝试这个
this.ProcessID = unchecked((int)GetWindowThreadProcessId(windowHandle.ToInt32(),0))
回答by DanielV
I was getting "Arithmetic operation resulted in an overflow", whenever I was:
每当我遇到以下情况时,我都会收到“算术运算导致溢出”的消息:
IntPtr handler = OpenSCManager(null, null, SC_MANAGER_CREATE_SERVICE);
if (handler.ToInt32() == 0) //throws Exception
instead of that:
而不是:
IntPtr handler = OpenSCManager(null, null, SC_MANAGER_CREATE_SERVICE);
if (handler == IntPtr.Zero) //OK