[英]How do I pinvoke to GetWindowLongPtr and SetWindowLongPtr on 32-bit platforms?
I want to P/Invoke to GetWindowLongPtr and SetWindowLongPtr , and I'm seeing conflicting information about them. 我想P / Invoke到GetWindowLongPtr和SetWindowLongPtr ,我看到有关它们的相互矛盾的信息。
Some sources say that, on 32-bit platforms, GetWindowLongPtr is just a preprocessor macro that calls GetWindowLong, and GetWindowLongPtr doesn't exist as an entry point in user32.dll. 有些消息称,在32位平台上,GetWindowLongPtr只是一个调用GetWindowLong的预处理器宏,而GetWindowLongPtr不作为user32.dll中的入口点存在。 For example:
例如:
So these sources seem to indicate that the *Ptr entry points simply aren't there in the version of user32.dll that ships with, say, 32-bit Windows 7. 因此,这些来源似乎表明* Ptr入口点根本不存在于32位Windows 7附带的user32.dll版本中。
But I see no indication of this in the MSDN documentation. 但我在MSDN文档中没有看到这一点。 According to MSDN, SetWindowLongPtr supersedes SetWindowLong, plain and simple.
根据MSDN, SetWindowLongPtr取代了SetWindowLong,简单明了。 And according to the requirements section of the SetWindowLongPtr page , it appears that SetWindowLongPtr has been in user32.dll since Windows 2000 (both client and server editions).
根据SetWindowLongPtr页面的要求部分,看起来SetWindowLongPtr自Windows 2000(客户端和服务器版本)以来一直在user32.dll中。 Again, no mention of the entry points being missing in 32-bit OSes.
同样,没有提到32位操作系统中缺少的入口点。
I suspect that the truth is somewhere in between: that when you tell the C++ compiler to target older OSes (ie, to compile something that will run on Win9x and NT4), then the header files declare SetWindowLongPtr as a macro that calls SetWindowLong, but the entry point probably does exist in Windows 2000 and later and you'll get it directly (instead of the macro) if you tell the compiler to target those platforms. 我怀疑事实介于两者之间:当你告诉C ++编译器定位较旧的操作系统(即编译将在Win9x和NT4上运行的东西)时,头文件将SetWindowLongPtr声明为调用SetWindowLong的宏,但是入口点可能确实存在于Windows 2000及更高版本中,如果您告诉编译器定位这些平台,您将直接获取它(而不是宏)。 But that's just a guess;
但这只是猜测; I don't really have the resources or the knowhow to dig in and verify it.
我真的没有资源或技术可以深入挖掘并验证它。
It's also possible that the target platform plays a role -- that if you compile your app for the x86 platform, then you shouldn't call SetWindowLongPtr on a 64-bit OS. 目标平台也可能扮演一个角色 - 如果您为x86平台编译应用程序,那么就不应该在64位操作系统上调用SetWindowLongPtr。 Again, I know enough to think of the question, but I don't know how to find the answer.
再一次,我知道这个问题,但我不知道如何找到答案。 MSDN seems to suggest that SetWindowLongPtr is always correct.
MSDN似乎暗示SetWindowLongPtr始终是正确的。
Can anyone tell me whether it's safe to simply P/Invoke to SetWindowLongPtr and be done with it? 任何人都可以告诉我,简单的P / Invoke到SetWindowLongPtr是否安全并完成它? (Assume Windows 2000 and later.) Would P/Invoking to SetWindowLongPtr give me the correct entry point:
(假设Windows 2000及更高版本。)P /调用SetWindowLongPtr会给我正确的入口点:
I'd recommend you deal with this the way Windows Forms does it internally: 我建议你按照Windows Forms内部的方式处理这个问题:
public static IntPtr GetWindowLong(HandleRef hWnd, int nIndex)
{
if (IntPtr.Size == 4)
{
return GetWindowLong32(hWnd, nIndex);
}
return GetWindowLongPtr64(hWnd, nIndex);
}
[DllImport("user32.dll", EntryPoint="GetWindowLong", CharSet=CharSet.Auto)]
private static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint="GetWindowLongPtr", CharSet=CharSet.Auto)]
private static extern IntPtr GetWindowLongPtr64(HandleRef hWnd, int nIndex);
C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0A\\Include
C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0A\\Include
SetWindowLongPtr
/ GetWindowLongPtr
. SetWindowLongPtr
/ GetWindowLongPtr
所有实例。 _WIN64
is defined, they are functions; _WIN64
时,它们是函数; when it's not, they are #define
'd to SetWindowLong
/ GetWindowLong
. #define
to SetWindowLong
/ GetWindowLong
。 This implies that 32-bit OSes may not have SetWindowLongPtr
/ GetWindowLongPtr
as an actual function, so it would appear that the comment on pinvoke.net is correct. 这意味着32位操作系统可能没有
SetWindowLongPtr
/ GetWindowLongPtr
作为实际函数,因此看起来pinvoke.net上的注释是正确的。
Update (more clarification on _WIN64): 更新(关于_WIN64的更多说明):
_WIN64
is defined by the C/C++ compiler when compiling 64-bit code (that will only run on a 64-bit OS). _WIN64
由C / C ++编译器在编译64位代码时定义(仅在64位操作系统上运行)。 So this means that any 64-bit code using SetWindowLongPtr
/ GetWindowLongPtr
will use the actual functions, but any 32-bit code using them will use SetWindowLong
/ GetWindowLong
instead. 所以这意味着使用
SetWindowLongPtr
/ GetWindowLongPtr
任何64位代码都将使用实际函数,但使用它们的任何32位代码都将使用SetWindowLong
/ GetWindowLong
。 This includes 32-bit code running on a 64-bit OS. 这包括在64位操作系统上运行的32位代码。
To emulate the same behavior in C#, I recommend checking IntPtr.Size
as done by pinvoke.net; 要在C#中模拟相同的行为,我建议检查
IntPtr.Size
如IntPtr.Size
所做; that tells you whether you're running 32-bit or 64-bit code. 它告诉您是否正在运行32位或64位代码。 (Keeping in mind that 32-bit code may run on a 64-bit OS).
(请记住,32位代码可能在64位操作系统上运行)。 Using
IntPtr.Size
in managed code emulates the same behavior as _WIN64
does for native code. 在托管代码中使用
IntPtr.Size
可以模拟与_WIN64
对本机代码相同的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.