Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将IntPtr转换为int_C#_Window Handles - Fatal编程技术网

C# 如何将IntPtr转换为int

C# 如何将IntPtr转换为int,c#,window-handles,C#,Window Handles,窗口句柄有时为int类型,有时为IntPtr int示例: [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, in

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

int
示例:

 [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);    
  [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
IntPtr
示例:

 [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);    
  [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
我似乎无法从一个转换到另一个


当我尝试
this.ProcessID=GetWindowThreadProcessId(windowHandle.ToInt32(),0)
时,我得到一个错误
无法从uint隐式转换为int
签名是

static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
还是这个

static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, StringBuilder lParam);
不要交换
int
IntPtr
。它们仅在32位(大小相等)时几乎相等。在64位时,
IntPtr
几乎等同于
long
(大小相等)

GetWindowThreadProcessId
签名为

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

在这种情况下,对“某物”的
ref
out
是对某物的托管引用,因此在传递到本机API时,它们在内部转换为
IntPtr
。因此,从本机API的角度来看,
out uint
相当于
IntPtr

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

请注意,某些类型(如
bool
char
)由封送处理程序进行特殊处理

您不应该将
int
转换为
IntPtr
。让它成为一个
IntPtr
并快乐地生活。如果您必须进行一些
IntPtr
不支持的数学运算,请使用
long
(它是64位的,因此在我们使用Windows 128之前,不会有任何问题:-))


我认为错误
不能隐式地从uint转换为int
引用
=
语句。字段
this.ProcessID
int
,但是
GetWindowThreadProcessId
返回
uint

试试这个

this.ProcessID = unchecked((int)GetWindowThreadProcessId(windowHandle.ToInt32(),0))
每当我:

IntPtr handler = OpenSCManager(null, null, SC_MANAGER_CREATE_SERVICE);
if (handler.ToInt32() == 0) //throws Exception
相反:

IntPtr handler = OpenSCManager(null, null, SC_MANAGER_CREATE_SERVICE);
if (handler == IntPtr.Zero) //OK

windowHandle.ToInt32()
将起作用。@Henk holterman我编辑了问题,但仍然没有编译您不应该编辑问题的关键部分,请添加下一部分。检查您对
this.ProcessID的声明