BetterWin32Errors
A better interface to the constants defined in winerror.h
Install / Use
/learn @mkropat/BetterWin32ErrorsREADME
BetterWin32Errors
A better interface to the constants defined in winerror.h
Simplified Error Handling
Instead of:
if (!SomeWin32ApiCall(...))
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}
You can do this:
if (!SomeWin32ApiCall(...))
{
throw new BetterWin32Errors.Win32Exception();
}
Access To Error Constants
Instead of re-defining platform constants in your code:
const int ERROR_FILE_NOT_FOUND = 2;
const int ERROR_PATH_NOT_FOUND = 3;
if (!SomeWin32ApiCall(...))
{
var error = Marshal.GetLastWin32Error();
if (error == ERROR_FILE_NOT_FOUND)
{
// ...do something...
}
else if (error == ERROR_PATH_NOT_FOUND)
{
// ...do something else...
}
else
{
throw new System.ComponentModel.Win32Exception(error);
}
}
You can do this:
if (!SomeWin32ApiCall(...))
{
var error = Win32Exception.GetLastWin32Error();
if (error == Win32Error.ERROR_FILE_NOT_FOUND)
{
// ...do something...
}
else if (error == Win32Error.ERROR_PATH_NOT_FOUND)
{
// ...do something else...
}
else
{
throw new Win32Exception(error);
}
}
Exception Has Both ID and Message
try
{
// ...some code...
}
catch (BetterWin32Errors.Win32Exception ex)
when (ex.Error == Win32Error.ERROR_FILE_NOT_FOUND) // use `Error` to get the error ID
{
Console.WriteLine("Warning: " + ex.ErrorMessage);
// Output: "Warning: The system cannot find the file specified"
}
Installation
Install-Package BetterWin32Errors
The only import you need to know is:
using BetterWin32Errors;
Limitations
There are thousands of error constants defined in <winerror.h>. All error constants included in this library were parsed using a script. As such, there is no guarantee that the error constants have the correct values in all cases and for all platforms. Feel free to submit an issue if you run into such a problem.
Related Skills
node-connect
344.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
99.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
