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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:27:49  来源:igfitidea点击:

How to convert IntPtr to int

c#window-handles

提问by E Mett

A window handle sometimes of type intand other times of type IntPtr

窗口句柄有时是类型int,有时是类型IntPtr

intexample:

int例子:

 [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);    

IntPtrexample:

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 SendMessagesignature 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 intand IntPtr. They are nearly equivalent only at 32 bits (equal in size). At 64 bits an IntPtris nearly equivalent to a long(equal in size)

不要交换intIntPtr。它们仅在 32 位(大小相等)时几乎等效。在 64 位时,anIntPtr几乎等于 a long(大小相等)

The GetWindowThreadProcessIdsignature is

GetWindowThreadProcessId签名是

static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

or

或者

static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

In this case, a refor a outto "something" are managed references to something, so they are internally converted to IntPtrwhen passed to Native API. So out uintis, 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. intand uintare equal for the called API. And a 32bit IntPtris the same too.

说明:重要的是参数的“长度”是正确的。int并且uint对于被调用的 API 是相等的。32位IntPtr也是一样的。

Note that some types (like booland char) have special handling by the marshaler.

请注意,某些类型(如boolchar)由封送拆收器进行特殊处理。

You shouldn't EVER convert an intto an IntPtr. Keep it as an IntPtrand 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 转换intIntPtr. 保持它作为一个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 intrefers to =statement. The field this.ProcessIDis int, but GetWindowThreadProcessIdreturns uint.

我认为该错误cannot implicitly convert from uint to int是指=声明。该字段this.ProcessIDint,但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