以下為 Windows 系統 API (GetLastError、FormatMessage、HRESULT 低 16 位) 常見錯誤代碼對照,適用於程式開發除錯、系統日誌分析、腳本錯誤處理、驅動開發等場景。
來源:Microsoft Docs System Error Codes,持續更新中。
| 十進位 | 十六進位 | 符號常數 (WinError.h) | 英文描述 | 中文說明 |
|---|---|---|---|---|
| 0 | 0x00000000 | ERROR_SUCCESS | The operation completed successfully. | 作業成功完成。 |
| 1 | 0x00000001 | ERROR_INVALID_FUNCTION | Incorrect function. | 不正確的函數。 |
| 2 | 0x00000002 | ERROR_FILE_NOT_FOUND | The system cannot find the file specified. | 系統找不到指定的檔案。 |
| 3 | 0x00000003 | ERROR_PATH_NOT_FOUND | The system cannot find the path specified. | 系統找不到指定的路徑。 |
| 4 | 0x00000004 | ERROR_TOO_MANY_OPEN_FILES | The system cannot open the file. | 系統無法開啟檔案 (開啟檔案過多)。 |
| 5 | 0x00000005 | ERROR_ACCESS_DENIED | Access is denied. | 拒絕存取 (權限不足)。 |
| 6 | 0x00000006 | ERROR_INVALID_HANDLE | The handle is invalid. | 無效的控制代碼/句柄。 |
| 7 | 0x00000007 | ERROR_ARENA_TRASHED | The storage control blocks were destroyed. | 儲存體控制區塊已損毀 (堆積區損壞)。 |
| 8 | 0x00000008 | ERROR_NOT_ENOUGH_MEMORY | Not enough storage is available to process this command. | 儲存體空間不足,無法處理此指令 (記憶體不足)。 |
| 9 | 0x00000009 | ERROR_INVALID_BLOCK | The storage control block address is invalid. | 儲存體控制區塊位址無效。 |
| 10 | 0x0000000A | ERROR_BAD_ENVIRONMENT | The environment is incorrect. | 環境不正確 (環境變數區塊損壞)。 |
| 11 | 0x0000000B | ERROR_BAD_FORMAT | An attempt was made to load a program with an incorrect format. | 嘗試載入格式錯誤的程式 (如 32位載入 64位、PE 損壞)。 |
| 12 | 0x0000000C | ERROR_INVALID_ACCESS | The access code is invalid. | 存取碼錯誤。 |
| 13 | 0x0000000D | ERROR_INVALID_DATA | The data is invalid. | 資料錯誤 (CRC/校驗失敗)。 |
| 14 | 0x0000000E | ERROR_OUTOFMEMORY | Not enough storage is available to complete this operation. | 儲存體空間不夠,無法完成此作業 (虛擬記憶體耗盡)。 |
| 15 | 0x0000000F | ERROR_INVALID_DRIVE | The system cannot find the drive specified. | 系統找不到指定的磁碟機。 |
| 16 | 0x00000010 | ERROR_CURRENT_DIRECTORY | The directory cannot be removed. | 無法移除目錄 (當前目錄無法刪除)。 |
| 17 | 0x00000011 | ERROR_NOT_SAME_DEVICE | The system cannot move the file to a different disk drive. | 系統無法將檔案移至不同的磁碟機 (跨磁碟移動需複製後刪除)。 |
| 18 | 0x00000012 | ERROR_NO_MORE_FILES | There are no more files. | 沒有更多檔案 (列舉結束)。 |
| 19 | 0x00000013 | ERROR_WRITE_PROTECT | The media is write protected. | 媒體為寫保護狀態 (SD 卡鎖、USB 寫保護、光碟)。 |
| 20 | 0x00000014 | ERROR_BAD_UNIT | The system cannot find the device specified. | 系統找不到指定的裝置。 |
| 21 | 0x00000015 | ERROR_NOT_READY | The device is not ready. | 裝置尚未就緒 (光碟機無碟、USB 未就緒)。 |
| 22 | 0x00000016 | ERROR_BAD_COMMAND | The device does not recognize the command. | 裝置無法識別指令 (IOCTL/裝置驅動錯誤)。 |
| 23 | 0x00000017 | ERROR_CRC | Data error (cyclic redundancy check). | 資料錯誤 (循環冗餘檢查 CRC 失敗),資料損毀/傳輸錯誤/壞軌。 |
| 24 | 0x00000018 | ERROR_BAD_LENGTH | The program issued a command but the command length was incorrect. | 程式發出指令但指令長度錯誤。 |
| 25 | 0x00000019 | ERROR_SEEK | The drive cannot locate a specific area or track on the disk. | 磁碟機在磁碟上找不到指定的扇區或磁道 (尋找失敗)。 |
| 26 | 0x0000001A | ERROR_NOT_DOS_DISK | The specified disk or diskette cannot be accessed. | 指定的磁碟或磁片無法存取 (非 DOS 格式/檔案系統損毀)。 |
| 27 | 0x0000001B | ERROR_SECTOR_NOT_FOUND | The drive cannot find the sector requested. | 磁碟機找不到要求的扇區 (硬碟壞軌/定位錯誤)。 |
| 28 | 0x0000001C | ERROR_OUT_OF_PAPER | The printer is out of paper. | 印表機沒有紙。 |
| 29 | 0x0000001D | ERROR_WRITE_FAULT | The system cannot write to the specified device. | 系統無法將資料寫入指定的裝置 (寫入失敗/壞軌/權限/離線)。 |
| 30 | 0x0000001E | ERROR_READ_FAULT | The system cannot read from the specified device. | 系統無法從指定裝置讀取資料 (讀取失敗/壞軌/權限/離線)。 |
| 31 | 0x0000001F | ERROR_GEN_FAILURE | A device attached to the system is not functioning. | 連接到系統的某個裝置無法運作 (通用裝置故障)。 |
| 32 | 0x00000020 | ERROR_SHARING_VIOLATION | The process cannot access the file because it is being used by another process. | 檔案被另一進程使用中無法存取 (共用違規/檔案鎖定)。 |
| 33 | 0x00000021 | ERROR_LOCK_VIOLATION | A part of the file is locked. | 檔案的一部份被鎖定 (區域鎖定衝突)。 |
| 34 | 0x00000022 | ERROR_WRONG_DISK | The wrong diskette is in the drive. | 磁碟機中的磁片不正確 (換錯磁片/磁碟機標籤錯誤)。 |
| 36 | 0x00000024 | ERROR_SHARING_BUFFER_EXCEEDED | Too many files opened for sharing. | 開啟的共用檔案數量過多 (超過 SMB/伺服器限制)。 |
| 38 | 0x00000026 | ERROR_HANDLE_EOF | Reached the end of the file. | 到達檔案結尾 (EOF)。 |
| 39 | 0x00000027 | ERROR_DISK_FULL | The disk is full. | 磁碟已滿 (磁碟空間不足)。 |
| 50 | 0x00000032 | ERROR_NOT_SUPPORTED | The request is not supported. | 不支援此網路要求/功能 (網路重導向器/檔案系統不支援操作)。 |
net helpmsg <錯誤碼> (如 net helpmsg 5 →「拒絕存取」)。[System.ComponentModel.Win32Exception]<錯誤碼>.Message。Error Lookup 工具 (Tools > Error Lookup) 或 err.exe (舊版 SDK)。ErrorCode / Status 多為此代碼或 HRESULT (0x8007xxxx = Win32 錯誤碼 + 0x80070000)。// C/C++
DWORD err = GetLastError();
if (err != ERROR_SUCCESS) {
LPWSTR msg = nullptr;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&msg, 0, NULL);
// 使用 msg...
LocalFree(msg);
}
// C# / .NET
try { ... }
catch (System.ComponentModel.Win32Exception ex) {
Console.WriteLine($"Error {ex.NativeErrorCode}: {ex.Message}");
}
// 或 P/Invoke GetLastError()
// PowerShell
$err = [System.ComponentModel.Win32Exception]::new(5)
$err.Message # "Access is denied"