为什么通过 WinAPI 调用的 MessageBox 在不同的计算机上会给出不同的错误?
Why does a MessageBox called via WinAPI give different errors on different computers?
我的项目需要使用 WinAPI。在学习 WinAPI 时,我阅读了有关获取错误的方法 Marshal.GetLastWin32Error()。
我处理了网站 docs.microsoft.com 中的示例,并将控制台项目添加到 .Net Core 5.0。
using System;
using System.Runtime.InteropServices;
internal class Win32
{
// Use DllImportAttribute to inport the Win32 MessageBox
// function. Set the SetLastError flag to true to allow
// the function to set the Win32 error.
[DllImportAttribute("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hwnd, String text, String caption, uint type);
}
class Program
{
static void Run()
{
// Call the MessageBox with normal parameters.
Console.WriteLine("Calling Win32 MessageBox without error...");
Win32.MessageBox(new IntPtr(0), "Press OK...", "Press OK Dialog", 0);
// Get the last error and display it.
int error = Marshal.GetLastWin32Error();
Console.WriteLine("The last Win32 Error was: " + error);
// Call the MessageBox with an invalid window handle to
// produce a Win32 error.
Console.WriteLine("Calling Win32 MessageBox with error...");
Win32.MessageBox(new IntPtr(123132), "Press OK...", "Press OK Dialog", 0);
// Get the last error and display it.
error = Marshal.GetLastWin32Error();
Console.WriteLine("The last Win32 Error was: " + error);
}
static void Main(string[] args)
{
Run();
}
}
// This code example displays the following to the console:
//
// Calling Win32 MessageBox without error...
// The last Win32 Error was: 0
// Calling Win32 MessageBox with error...
// The last Win32 Error was: 1400
我试过的第一台电脑运行良好。
之后我买了新的并在那里尝试了我的项目。
代码开始工作时出现错误:错误 1400 总是在调用消息框后返回,但 window 显示并返回 1 (IDOK)。
第一台电脑:
- 系统厂商:MSI GF75 Thin 10UEK;
- Windows:Windows10 家 x64;
- 更新:20H2 (KB504237);
- BIOS:E17FSIMS.102
- 处理器:Intel i7-10750H;
第二台电脑:
- 系统制造商:HP ProBook 440 G8;
- Windows:Windows10 Pro x64;
- 更新:20H2 (KB504237)
- BIOS:T70 版本。 01.03.01
- 处理器:Intel i7-1165G7
如果我在调用消息框之前调用 GetLastWin32Error,我将得到错误 0。
有人有过这样的经历吗?问题不是很严重因为查了一下windowreturns。但是我想知道发生了什么。
Marshal.GetLastWin32Error
呼叫 GetLastError
.
呼叫 GetLastError
only makes sense when the function in question returned a failed code. For MessageBox
, that is 0
.
无论如何你都会调用 GetLastWin32Error
,所以你不能相信它的值 returns。
Most functions that set the thread's last-error code set it when they fail. However, some functions also set the last-error code when they succeed. If the function is not documented to set the last-error code, the value returned by this function is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not.
MessageBox
是 not documented 成功时将最后一个错误设置为 0。
我的项目需要使用 WinAPI。在学习 WinAPI 时,我阅读了有关获取错误的方法 Marshal.GetLastWin32Error()。
我处理了网站 docs.microsoft.com 中的示例,并将控制台项目添加到 .Net Core 5.0。
using System;
using System.Runtime.InteropServices;
internal class Win32
{
// Use DllImportAttribute to inport the Win32 MessageBox
// function. Set the SetLastError flag to true to allow
// the function to set the Win32 error.
[DllImportAttribute("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hwnd, String text, String caption, uint type);
}
class Program
{
static void Run()
{
// Call the MessageBox with normal parameters.
Console.WriteLine("Calling Win32 MessageBox without error...");
Win32.MessageBox(new IntPtr(0), "Press OK...", "Press OK Dialog", 0);
// Get the last error and display it.
int error = Marshal.GetLastWin32Error();
Console.WriteLine("The last Win32 Error was: " + error);
// Call the MessageBox with an invalid window handle to
// produce a Win32 error.
Console.WriteLine("Calling Win32 MessageBox with error...");
Win32.MessageBox(new IntPtr(123132), "Press OK...", "Press OK Dialog", 0);
// Get the last error and display it.
error = Marshal.GetLastWin32Error();
Console.WriteLine("The last Win32 Error was: " + error);
}
static void Main(string[] args)
{
Run();
}
}
// This code example displays the following to the console:
//
// Calling Win32 MessageBox without error...
// The last Win32 Error was: 0
// Calling Win32 MessageBox with error...
// The last Win32 Error was: 1400
我试过的第一台电脑运行良好。
之后我买了新的并在那里尝试了我的项目。
代码开始工作时出现错误:错误 1400 总是在调用消息框后返回,但 window 显示并返回 1 (IDOK)。
第一台电脑:
- 系统厂商:MSI GF75 Thin 10UEK;
- Windows:Windows10 家 x64;
- 更新:20H2 (KB504237);
- BIOS:E17FSIMS.102
- 处理器:Intel i7-10750H;
第二台电脑:
- 系统制造商:HP ProBook 440 G8;
- Windows:Windows10 Pro x64;
- 更新:20H2 (KB504237)
- BIOS:T70 版本。 01.03.01
- 处理器:Intel i7-1165G7
如果我在调用消息框之前调用 GetLastWin32Error,我将得到错误 0。
有人有过这样的经历吗?问题不是很严重因为查了一下windowreturns。但是我想知道发生了什么。
Marshal.GetLastWin32Error
呼叫 GetLastError
.
呼叫 GetLastError
only makes sense when the function in question returned a failed code. For MessageBox
, that is 0
.
无论如何你都会调用 GetLastWin32Error
,所以你不能相信它的值 returns。
Most functions that set the thread's last-error code set it when they fail. However, some functions also set the last-error code when they succeed. If the function is not documented to set the last-error code, the value returned by this function is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not.
MessageBox
是 not documented 成功时将最后一个错误设置为 0。