我正在使用以下代码:
const int GWL_STYLE = (-16);
const UInt32 WS_POPUP = 0x80000000;
const UInt32 WS_CHILD = 0x40000000;
[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong);
Run Code Online (Sandbox Code Playgroud)
在某个地方......
SetWindowLong(this.Handle, GWL_STYLE,
((GetWindowLong(this.Handle, GWL_STYLE) & ~(WS_POPUP)) | WS_CHILD));
Run Code Online (Sandbox Code Playgroud)
这会在32位和64位机器上正常运行吗?
如果没有,如果我编译我的应用程序作为x86进程运行,它仍然可以在64位计算机上正常工作吗?
如何在32位和64位计算机上重写以下代码?
我刚刚在 C# 中遇到了 DllImport 的奇怪行为,我无法解释。我想知道它是如何可能的,以及我可以在哪里阅读它。案例是通过 DllImport 可以调用不真正导出表单 dll 的函数。就我而言,它是 kernel32.dll 和函数 ZeroMemory(但具有复制/移动/填充内存这样的行为)。所以,我的代码:
[DllImport("kernel32", EntryPoint = "LoadLibraryW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadLibrary(string libName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr module, string procName);
//WTF???
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ZeroMemory(IntPtr address, int size);
static void TestMemory()
{
IntPtr mem = Marshal.AllocHGlobal(100); //Allocate memory block of 100 bytes size
Marshal.WriteByte(mem, 55); //Write some value in …
Run Code Online (Sandbox Code Playgroud) 我像这样使用GetWindowLong:
[DllImport("user32.dll")]
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
Run Code Online (Sandbox Code Playgroud)
但根据MSDN文档,我应该使用GetWindowLongPtr与64位兼容. http://msdn.microsoft.com/en-us/library/ms633584(VS.85).aspx
GetWindowLongPtr的MSDN文档说我应该像这样定义它(在C++中):
LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex);
Run Code Online (Sandbox Code Playgroud)
我曾经使用IntPtr作为返回类型,但是我将使用什么来替代LONG_PTR?我也看到在C#中将GetWindowLong定义为:
[DllImport("user32.dll")]
private static extern long GetWindowLong(IntPtr hWnd, int nIndex);
Run Code Online (Sandbox Code Playgroud)
什么是正确的,我如何确保正确的64位兼容性?
我正在尝试使用,GetWindowLongPtrA
但我一直收到“无法在DLL'user32.dll'中找到名为'GetWindowLongPtrA'的入口点”。(也会SetWindowLongPtrA
出现相同的错误)。我已经尝试了许多在Google上找到的解决方案,但是他们没有解决。
这是我编写的函数的声明:
[DllImport("user32.dll")]
public static extern IntPtr GetWindowLongPtrA(IntPtr hWnd, int nIndex);
Run Code Online (Sandbox Code Playgroud)
尝试将put EntryPoint = "GetWindowLongPtrA"
,更改GetWindowLongPtrA
为GetWindowLongPtr
,put CharSet = CharSet.Ansi
,切换为GetWindowLongPtrW
with CharSet = CharSet.Unicode
等,它们都不起作用。
我的计算机正好是“ 64位”(但是不能调用该64位WinAPI函数吗?)。操作系统是Windows 10。
这个问题有什么解决方案?