我如何的PInvoke来GetWindowLongPtr和SetWindowLongPtr在32位平台? [英] How do I pinvoke to GetWindowLongPtr and SetWindowLongPtr on 32-bit platforms?

查看:263
本文介绍了我如何的PInvoke来GetWindowLongPtr和SetWindowLongPtr在32位平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想P / Invoke来 GetWindowLongPtr 和的 SetWindowLongPtr 和我看到冲突的相关信息。

I want to P/Invoke to GetWindowLongPtr and SetWindowLongPtr, and I'm seeing conflicting information about them.

有消息说的是,在32位平台上,GetWindowLongPtr只是preprocessor宏调用GetWindowLong和GetWindowLongPtr不存在作为user32.dll中的切入点。例如:

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. For example:

  • The pinvoke.net entry for SetWindowLongPtr has a static method that checks IntPtr.Size and then calls either SetWindowLong or SetWindowLongPtr, with a comment saying that "legacy OSes do not support SetWindowLongPtr". There's no explanation of what is meant by "legacy OSes".
  • An answer on StackOverflow states "On 32bit systems GetWindowLongPtr is just a C macro that points to GetWindowLong".

所以,这些来源似乎表明,在* PTR入口点根本不存在user32.dll中的版本附带的,比方说,32位Windows 7。

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.

但我看到MSDN文档中没有说明这一点。根据MSDN, SetWindowLongPtr 取代的SetWindowLong,简单明了。而根据 SetWindowLongPtr页的需求部分,看来SetWindowLongPtr自Windows 2000(客户端和服务器版本)已经在user32.dll中。再次,没有提到的入口点被遗漏在32位操作系统。

But I see no indication of this in the MSDN documentation. According to MSDN, SetWindowLongPtr supersedes SetWindowLong, plain and simple. 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). Again, no mention of the entry points being missing in 32-bit OSes.

我的犯罪嫌疑人的真相则介于两者之间:当你告诉C ++编译器为目标较旧的操作系统(即编译的东西,将运行在Win9x和NT4),然后将头文件声明SetWindowLongPtr,作为调用SetWindowLong函数宏,但入口点可能确实存在于Windows 2000及以后,你会直接得到它(而不是宏)如果你告诉编译器针对这些平台。但是,这只是一个猜测;我真的没有资源或专有技术来挖掘和验证。

I suspect that the truth is somewhere in between: that when you tell the C++ compiler to target older OSes (i.e., 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. But that's just a guess; I don't really have the resources or the knowhow to dig in and verify it.

这也有可能是目标平台发挥了作用 - 如果你编译你的应用程序的x86平台,那么你不应该叫SetWindowLongPtr在64位操作系统。再次,我非常清楚,想的问题,但我不知道如何找到答案。 MSDN似乎表明SetWindowLongPtr始终是正确的。

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. 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.

谁能告诉我它是否是安全的简单的P / Invoke来SetWindowLongPtr,并用它做? (假设Windows 2000及更高版本。)将P /调用到SetWindowLongPtr给我正确的进入点:

Can anyone tell me whether it's safe to simply P/Invoke to SetWindowLongPtr and be done with it? (Assume Windows 2000 and later.) Would P/Invoking to SetWindowLongPtr give me the correct entry point:

  • 如果我运行针对x86平台上的32位操作系统的应用程序?
  • 如果我运行针对x86平台上的64位操作系统的应用程序?
  • 如果我运行针对x64平台上的64位操作系统的应用程序?

推荐答案

我建议你处理这个Windows窗体的方式在内部做的:

I'd recommend you deal with this the way Windows Forms does it internally:

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);

这篇关于我如何的PInvoke来GetWindowLongPtr和SetWindowLongPtr在32位平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆