Извлечение кода последней ошибки

Solution

Here is my minimal C++ example using std::string/wstring.

  • Works with both Unicode and MBCS
  • Compatible from MSVC 6.0 -> VS2022 and GCC/MinGW (with -lstdc++). Most likely with Clang too.
  • Doesn’t require C++11
  • Works in Windows XP and later.
#include <windows.h>
#include <string>

typedef std::basic_string<TCHAR> String;

String errorMessage(DWORD dwError)
{
    LPTSTR lpBuffer = NULL;
    String ret = TEXT("");
    if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwError, 0, (LPTSTR)&lpBuffer, 0, NULL))
        ret = String(lpBuffer);
    LocalFree(lpBuffer);
    return ret;
}

It doesn’t have any error checking though and just returns an empty string if it can’t find the specified error. You can implement your own error checking if you like.

Why waste time write lot code, when few code do trick?


Additional information (you can skip this)

I pass 0 for dwLanguageId as it’s the right way to do it, as other answers failed to notice that MAKELANGID macro is deprecated and should not be used as it is inconsistent and doesn’t work at all for some languages.

Here is an excerpt from winnt.h in Windows SDK 10.0.19041.0 (2020-05-12) stating the issue:

//
// ** DEPRECATED ** DEPRECATED ** DEPRECATED ** DEPRECATED ** DEPRECATED **
//
//  DEPRECATED: The LCID/LANGID/SORTID concept is deprecated, please use
//  Locale Names instead, eg: "en-US" instead of an LCID like 0x0409.
//  See the documentation for GetLocaleInfoEx.
//
//  A language ID is a 16 bit value which is the combination of a
//  primary language ID and a secondary language ID.  The bits are
//  allocated as follows:
//
//       +-----------------------+-------------------------+
//       |     Sublanguage ID    |   Primary Language ID   |
//       +-----------------------+-------------------------+
//        15                   10 9                       0   bit
//
//  WARNING:  This pattern is broken and not followed for all languages.
//            Serbian, Bosnian & Croatian are a few examples.
//
//  WARNING:  There are > 6000 human languages.  The PRIMARYLANGID construct
//            cannot support all languages your application may encounter.
//            Please use Language Names, such as "en".
//
//  WARNING:  There are > 200 country-regions.  The SUBLANGID construct cannot
//            represent all valid dialects of languages such as English.
//            Please use Locale Names, such as "en-US".
//
//  WARNING:  Some languages may have more than one PRIMARYLANGID.  Please
//            use Locale Names, such as "en-FJ".
//
//  WARNING:  Some languages do not have assigned LANGIDs.  Please use
//            Locale Names, such as "tlh-Piqd".
//
//  It is recommended that applications test for locale names rather than
//  attempting to construct/deconstruct LANGID/PRIMARYLANGID/SUBLANGID
//
//  Language ID creation/extraction macros:
//
//    MAKELANGID    - construct language id from a primary language id and
//                    a sublanguage id.
//    PRIMARYLANGID - extract primary language id from a language id.
//    SUBLANGID     - extract sublanguage id from a language id.
//
//  Note that the LANG, SUBLANG construction is not always consistent.
//  The named locale APIs (eg GetLocaleInfoEx) are recommended.
//
//  DEPRECATED: Language IDs do not exist for all locales
//
// ** DEPRECATED ** DEPRECATED ** DEPRECATED ** DEPRECATED ** DEPRECATED **
//

Seems just that the information hasn’t made its’ way to the official MSDN doc of MAKELANGID yet.

Even if it did work correctly, it’s the worse option since it tries to find the error string on that specified LangID and only that one ID, failing if it doesn’t exist. Using 0 instead will very likely return at least something, even if that error isn’t localized to the user’s language.

Quote from MSDN FormatMessageW:

[in] dwLanguageId

The language identifier for the requested message. This parameter is ignored if dwFlags includes FORMAT_MESSAGE_FROM_STRING.

If you pass a specific LANGID in this parameter, FormatMessage will return a message for that LANGID only. If the function cannot find a message for that LANGID, it sets Last-Error to ERROR_RESOURCE_LANG_NOT_FOUND. If you pass in zero, FormatMessage looks for a message for LANGIDs in the following order:

  1. Language neutral
  2. Thread LANGID, based on the thread’s locale value
  3. User default LANGID, based on the user’s default locale value
  4. System default LANGID, based on the system default locale value
  5. US English

If FormatMessage does not locate a message for any of the preceding LANGIDs, it returns any language message string that is present. If that fails, it returns ERROR_RESOURCE_LANG_NOT_FOUND.



Функция GetLastError
извлекает значение кода последней ошибки
вызывающего потока. Код последней ошибки
сохраняется при посредстве базового
компонента потока. Многие потоки не
записывают поверх друг друга коды
последней ошибки.


Синтаксис


Visual Basic: Прикладные
программы должны вызывать
err.
LastDllError
вместо GetLastError.




DWORD GetLastError(VOID); 

Параметры

У этой функции нет параметров.

Возвращаемые значения


Величина возвращаемого значения является
значение кода последней ошибки вызывающего
потока. Функции устанавливают это значение
путем вызова функция
SetLastError.
Раздел Возвращаемые значения каждой
статьи справочника по функциям обращает
внимание на условия, при которых функция
устанавливает код последней ошибки.



Windows 95/98/Me: Функции, которые фактически
реализованы в 16-разрядном коде, не
устанавливают код последней ошибки. Вы
должны игнорировать код последней ошибки,
когда вызываете эти функции. Они включают в
себя функции управления окном, функции GDI
и функции
мультимедийных
средств. Для функций, которые устанавливают
код последней ошибки, Вы не должны
надеяться на
возврат
функцией
GetLastError
того же самого значения и в среде Windows 95 /98/Me
и в среде Windows NT.


Замечания


Чтобы получить строку  для кодов
системных ошибок, используйте функцию
FormatMessage.
За полным перечнем кодов ошибок,
предусмотренных операционной системой, см.
статью
Коды
системных ошибок
.


Возвращаемые функцией коды ошибок не
являются частью спецификации Windows API
и
могут различаться в операционных системах
или драйверах устройств. По этой причине,
имеются функции, документация на которые не
включает в себя список кодов ошибок,
которые могут быть возвращены.
 


Вы должны вызвать функцию GetLastError
непосредственно тогда, когда величина
возвращаемого значения функции указывает,
что такой вызов даст вам полезную
информацию. Это делается потому, что
некоторые функции, когда они завершаются
успешно, вызывают функцию
SetLastError
с нулем, чтобы ликвидировать код ошибки
установленный самой последней

неудавшейся функцией.


Большинство функций, которые
устанавливают последнее значение кода
ошибки потока, устанавливает его тогда,
когда они завершаются ошибкой; несколько
функций устанавливают код ошибки, когда они
завершаются успешно. Функция, которая
завершилась сбоем, обычно указывается
кодом ошибки величины возвращаемого
значения типа нуля, ПУСТО (NULL) или — (минус)1.
Некоторые функции вызывают
SetLastError
при условии успешного завершения; на такие
случаи обращается внимание в статьях
справочника для каждой функции.


Коды Ошибки — это 32-разрядные значения (бит
31 — старший значащий бит). Бит 29
зарезервирован для определяемых
программой кодов ошибок; нет ни одного кода
системных ошибок, которые бы имели
установленным этот бит. Если Вы определяете
код ошибки для своего приложения,
устанавливайте этот бит в единицу. Это
указывает, что код ошибки был задан
прикладной программой и гарантирует, что
ваш код ошибки не войдет в противоречие с
любыми кодами ошибок, определенными
системой.



Чтобы преобразовывать системную ошибку в
значение
HRESULT,
используйте макрокоманду


HRESULT_FROM_WIN32
.


Код примера


Пример смотри в статье Извлечение
кода последней ошибки
.


Смотри также


Краткий обзор Обработка ошибок, Функции
обработки ошибок,
FormatMessage,
HRESULT_FROM_WIN32,
SetLastError,
SetLastErrorEx




Размещение и совместимость GetLastError



Windows. NET Server


Да



Windows XP


Да



Windows 2000


Да



Windows NT


Да



Windows Me


Да



Windows 98


Да



Windows 95


Да


Используемая библиотека



Kernel32.lib



Заголовочный файл

 


— объявлено в



Winbase.h


— включено в



Windows.h



Unicode

Нет



Замечания по платформе


Не имеется

Hosted by uCoz

Содержание

  1. Функция GetLastError (errhandlingapi.h)
  2. Синтаксис
  3. Возвращаемое значение
  4. Комментарии
  5. Функция GetLastError
  6. Возвращаемые значения
  7. Замечания
  8. Код примера
  9. Смотри также
  10. GetLastError function (errhandlingapi.h)
  11. Syntax
  12. Return value
  13. Remarks
  14. Win32 API Сообщение об ошибках и обработка
  15. замечания
  16. Вступление
  17. Ошибка, сообщенная только возвратным значением
  18. Сообщается об ошибке с сообщением об ошибке
  19. Заметки о вызове GetLastError() на других языках программирования
  20. .net (C #, VB и т. д.)
  21. Сообщается об ошибке с дополнительной информацией о сбоях и успехах
  22. Ошибка, сообщенная как значение HRESULT
  23. Преобразование кода ошибки в строку сообщения
  24. System Error Codes (0-499)

Функция GetLastError (errhandlingapi.h)

Извлекает значение кода последней ошибки вызывающего потока. Код последней ошибки поддерживается для каждого потока. Несколько потоков не перезаписывают код последней ошибки друг друга.

Visual Basic: Приложения должны вызывать err. LastDllError вместо GetLastError.

Синтаксис

Возвращаемое значение

Возвращаемое значение — это код последней ошибки вызывающего потока.

Раздел возвращаемого значения документации для каждой функции, которая задает код последней ошибки, отмечает условия, при которых функция задает код последней ошибки. Большинство функций, которые задают код последней ошибки потока, устанавливают его при сбое. Однако некоторые функции также задают код последней ошибки при успешном выполнении. Если функция не задокументирована для задания кода последней ошибки, то значение, возвращаемое этой функцией, является просто последним кодом последней ошибки, которую необходимо задать; Некоторые функции устанавливают для кода последней ошибки значение 0 при успешном выполнении, а другие — нет.

Комментарии

Функции, выполняемые вызывающим потоком, задают это значение путем вызова функции SetLastError . Функция GetLastError должна вызываться немедленно, когда возвращаемое значение функции указывает на то, что такой вызов вернет полезные данные. Это связано с тем, что некоторые функции вызывают SetLastError с нулевым значением при успешном выполнении, вытирая код ошибки, заданный последней неудачной функцией.

Чтобы получить строку ошибки для системных кодов ошибок, используйте функцию FormatMessage . Полный список кодов ошибок, предоставляемых операционной системой, см. в разделе «Коды системных ошибок».

Коды ошибок, возвращаемые функцией, не являются частью спецификации API Windows и могут различаться в зависимости от операционной системы или драйвера устройства. По этой причине мы не можем предоставить полный список кодов ошибок, которые могут быть возвращены каждой функцией. Существует также множество функций, документация которых не включает даже частичный список кодов ошибок, которые могут быть возвращены.

Коды ошибок представляют собой 32-разрядные значения (бит 31 является наиболее значимым битом). Бит 29 зарезервирован для кодов ошибок, определенных приложением; В этом битовом коде ошибки системы нет. Если вы определяете код ошибки для приложения, установите этот бит в один. Это означает, что код ошибки определен приложением и гарантирует, что код ошибки не конфликтует с кодами ошибок, определенными системой.

Чтобы преобразовать системную ошибку в значение HRESULT , используйте макрос HRESULT_FROM_WIN32 .

Источник

Функция GetLastError

Функция GetLastError извлекает значение кода последней ошибки вызывающего потока. Код последней ошибки сохраняется при посредстве базового компонента потока. Многие потоки не записывают поверх друг друга коды последней ошибки.

Visual Basic: Прикладные программы должны вызывать err. LastDllError вместо GetLastError .

У этой функции нет параметров.

Возвращаемые значения

Величина возвращаемого значения является значение кода последней ошибки вызывающего потока. Функции устанавливают это значение путем вызова функция SetLastError . Раздел Возвращаемые значения каждой статьи справочника по функциям обращает внимание на условия, при которых функция устанавливает код последней ошибки.

Windows 95/98/Me: Функции, которые фактически реализованы в 16-разрядном коде, не устанавливают код последней ошибки. Вы должны игнорировать код последней ошибки, когда вызываете эти функции. Они включают в себя функции управления окном, функции GDI и функции мультимедийных средств. Для функций, которые устанавливают код последней ошибки, Вы не должны надеяться на возврат функцией GetLastError того же самого значения и в среде Windows 95 /98/Me и в среде Windows NT.

Замечания

Чтобы получить строку для кодов системных ошибок, используйте функцию FormatMessage . За полным перечнем кодов ошибок, предусмотренных операционной системой, см. статью Коды системных ошибок .

Возвращаемые функцией коды ошибок не являются частью спецификации Windows API и могут различаться в операционных системах или драйверах устройств. По этой причине, имеются функции, документация на которые не включает в себя список кодов ошибок, которые могут быть возвращены.

Вы должны вызвать функцию GetLastError непосредственно тогда, когда величина возвращаемого значения функции указывает, что такой вызов даст вам полезную информацию. Это делается потому, что некоторые функции, когда они завершаются успешно, вызывают функцию SetLastError с нулем, чтобы ликвидировать код ошибки установленный самой последней неудавшейся функцией.

Большинство функций, которые устанавливают последнее значение кода ошибки потока, устанавливает его тогда, когда они завершаются ошибкой; несколько функций устанавливают код ошибки, когда они завершаются успешно. Функция, которая завершилась сбоем, обычно указывается кодом ошибки величины возвращаемого значения типа нуля, ПУСТО (NULL) или — (минус)1. Некоторые функции вызывают SetLastError при условии успешного завершения; на такие случаи обращается внимание в статьях справочника для каждой функции.

Коды Ошибки — это 32-разрядные значения (бит 31 — старший значащий бит). Бит 29 зарезервирован для определяемых программой кодов ошибок; нет ни одного кода системных ошибок, которые бы имели установленным этот бит. Если Вы определяете код ошибки для своего приложения, устанавливайте этот бит в единицу. Это указывает, что код ошибки был задан прикладной программой и гарантирует, что ваш код ошибки не войдет в противоречие с любыми кодами ошибок, определенными системой.

Чтобы преобразовывать системную ошибку в значение HRESULT , используйте макрокоманду HRESULT_FROM_WIN32 .

Код примера

Пример смотри в статье Извлечение кода последней ошибки .

Смотри также

Краткий обзор Обработка ошибок, Функции обработки ошибок, FormatMessage , HRESULT_FROM_WIN32 , SetLastError , SetLastErrorEx

Источник

GetLastError function (errhandlingapi.h)

Retrieves the calling thread’s last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other’s last-error code.

Visual Basic:В В Applications should call err.LastDllError instead of GetLastError.

Syntax

Return value

The return value is the calling thread’s last-error code.

The Return Value section of the documentation for each function that sets the last-error code notes the conditions under which the function sets the last-error code. 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.

Functions executed by the calling thread set this value by calling the SetLastError function. You should call the GetLastError function immediately when a function’s return value indicates that such a call will return useful data. That is because some functions call SetLastError with a zero when they succeed, wiping out the error code set by the most recently failed function.

To obtain an error string for system error codes, use the FormatMessage function. For a complete list of error codes provided by the operating system, see System Error Codes.

The error codes returned by a function are not part of the Windows API specification and can vary by operating system or device driver. For this reason, we cannot provide the complete list of error codes that can be returned by each function. There are also many functions whose documentation does not include even a partial list of error codes that can be returned.

Error codes are 32-bit values (bit 31 is the most significant bit). Bit 29 is reserved for application-defined error codes; no system error code has this bit set. If you are defining an error code for your application, set this bit to one. That indicates that the error code has been defined by an application, and ensures that your error code does not conflict with any error codes defined by the system.

To convert a system error into an HRESULT value, use the HRESULT_FROM_WIN32 macro.

Источник

Win32 API
Сообщение об ошибках и обработка

замечания

Каждый поток будет иметь свой последний код ошибки. Windows API установит последний код ошибки в вызывающем потоке.

Вы всегда должны вызывать GetLastError сразу после проверки возвращаемого значения функции API Windows.

Большинство функций Windows API устанавливают последний код ошибки, когда они терпят неудачу. Некоторые также установят последний код ошибки, когда они преуспеют. Существует ряд функций, которые не устанавливают последний код ошибки. Всегда обращайтесь к документации по функциям Windows API.

Невозможно использовать FORMAT_MESSAGE_FROM_SYSTEM без FORMAT_MESSAGE_IGNORE_INSERTS при использовании функции FormatMessage для получения описания кода ошибки.

Вступление

API Windows предоставляется с помощью C-вызываемого интерфейса. Успех или сбой вызова API сообщаются строго через возвращаемые значения. Исключения не являются частью документированного контракта (хотя некоторые реализации API могут вызывать исключения SEH , например, при передаче аргумента lpCommandLine только для чтения в CreateProcess ).

Сообщение об ошибке грубо относится к одной из четырех категорий:

Документация для каждого вызова API явно вызывается, как сообщаются ошибки. Всегда обращайтесь к документации.

Ошибка, сообщенная только возвратным значением

Некоторые вызовы API возвращают единый флаг отказа / успеха без какой-либо дополнительной информации (например, GetObject ):

Сообщается об ошибке с сообщением об ошибке

В дополнение к возвращаемому значению отказа / успеха некоторые вызовы API также устанавливают последнюю ошибку при сбое (например, CreateWindow ). Документация обычно содержит следующую стандартную формулировку для этого случая:

Если функция завершается успешно, возвращаемое значение .
Если функция не работает, возвращаемое значение . Чтобы получить расширенную информацию об ошибке, вызовите GetLastError .

Очень важно, что вы вызываете GetLastError() НЕМЕДЛЕННО. Последний код ошибки может быть перезаписан любой другой функцией, поэтому, если есть дополнительная функция вызова между неудавшейся функцией и вызовом GetLastError() , возврат из GetLastError() больше не будет надежным. Будьте особенно осторожны при работе с конструкторами C ++.

Как только вы получите код ошибки, вам нужно будет его интерпретировать. Вы можете получить полный список кодов ошибок в MSDN на странице Системные коды ошибок (Windows) . Кроме того, вы можете посмотреть в своих файлах заголовков системы; файл со всеми константами кода ошибки — winerror.h . (Если у вас есть официальный SDK от Microsoft для Windows 8 или новее, это находится в shared папке с папкой include.)

Заметки о вызове GetLastError() на других языках программирования

.net (C #, VB и т. д.)

С .net вы не должны P / Invoke в GetLastError() напрямую. Это связано с тем, что среда выполнения .net сделает другие вызовы Windows API одним и тем же потоком за вашей спиной. Например, сборщик мусора может вызвать VirtualFree() если он найдет достаточно памяти, которую он больше не использует, и это может произойти между вашим назначенным вызовом функции и вашим вызовом GetLastError() .

Вместо этого .net предоставляет Marshal.GetLastWin32Error() , которая будет извлекать последнюю ошибку из последнего вызова P / Invoke, который вы сами сделали. Используйте это вместо прямого вызова GetLastError() .

(.net, похоже, не мешает вам импортировать GetLastError() любом случае, я не уверен, почему.)

Различные средства, предоставляемые Go для вызова DLL-функций (которые находятся как в syscall пакета, syscall и в пакете golang.org/x/sys/windows ), возвращают три значения: r1 , r2 и err . r2 никогда не используется; вы можете использовать пустой идентификатор. r1 — возвращаемое значение функции. err является результатом вызова GetLastError() но преобразуется в тип, реализующий error , поэтому вы можете передать его вызывающим функциям для обработки.

Поскольку Go не знает, когда вызывать GetLastError() а когда нет, он всегда будет возвращать ошибку nil . Поэтому типичная идиома обработки ошибок Go

не будет работать. Вместо этого вы должны проверить r1 точно так же, как и на C, и использовать только err если это указывает, что функция возвратила ошибку:

Сообщается об ошибке с дополнительной информацией о сбоях и успехах

Некоторые вызовы API могут преуспеть или сбой более чем одним способом. API обычно возвращают дополнительную информацию как для успешных вызовов, так и для ошибок (например, CreateMutex ).

Ошибка, сообщенная как значение HRESULT

HRESULT s — числовые 32-битные значения, где биты или диапазоны бит кодируют четко определенную информацию. MSB — это флаг отказа / успеха, а остальные бит хранят дополнительную информацию. Отказ или успех можно определить с помощью макросов FAILED или SUCCEEDED . HRESULT s обычно используются совместно с COM, но также отображаются в реализациях, отличных от COM (например, StringCchPrintf ).

Преобразование кода ошибки в строку сообщения

GetLastError возвращает числовой код ошибки. Чтобы получить описательное сообщение об ошибке ( например , для отображения пользователю), вы можете вызвать FormatMessage :

В C ++ вы можете значительно упростить интерфейс, используя класс std::string :

ПРИМЕЧАНИЕ. Эти функции также работают для значений HRESULT . Просто измените первый параметр из DWORD dwErrorCode на HRESULT hResult . Остальная часть кода может оставаться неизменной.

Источник

System Error Codes (0-499)

This information is intended for developers debugging system errors. For other errors, such as issues with Windows Update, there is a list of resources on the Error codes page.

The following list describes system error codes (errors 0 to 499). They are returned by the GetLastError function when many functions fail. To retrieve the description text for the error in your application, use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag.

ERROR_SUCCESS

The operation completed successfully.

ERROR_INVALID_FUNCTION

ERROR_FILE_NOT_FOUND

The system cannot find the file specified.

ERROR_PATH_NOT_FOUND

The system cannot find the path specified.

ERROR_TOO_MANY_OPEN_FILES

The system cannot open the file.

ERROR_ACCESS_DENIED

Access is denied.

ERROR_INVALID_HANDLE

The handle is invalid.

ERROR_ARENA_TRASHED

The storage control blocks were destroyed.

ERROR_NOT_ENOUGH_MEMORY

Not enough memory resources are available to process this command.

ERROR_INVALID_BLOCK

The storage control block address is invalid.

ERROR_BAD_ENVIRONMENT

The environment is incorrect.

ERROR_BAD_FORMAT

An attempt was made to load a program with an incorrect format.

ERROR_INVALID_ACCESS

The access code is invalid.

ERROR_INVALID_DATA

The data is invalid.

ERROR_OUTOFMEMORY

Not enough storage is available to complete this operation.

ERROR_INVALID_DRIVE

The system cannot find the drive specified.

ERROR_CURRENT_DIRECTORY

The directory cannot be removed.

ERROR_NOT_SAME_DEVICE

The system cannot move the file to a different disk drive.

ERROR_NO_MORE_FILES

There are no more files.

ERROR_WRITE_PROTECT

The media is write protected.

ERROR_BAD_UNIT

The system cannot find the device specified.

ERROR_NOT_READY

The device is not ready.

ERROR_BAD_COMMAND

The device does not recognize the command.

ERROR_CRC

Data error (cyclic redundancy check).

ERROR_BAD_LENGTH

The program issued a command but the command length is incorrect.

ERROR_SEEK

The drive cannot locate a specific area or track on the disk.

ERROR_NOT_DOS_DISK

The specified disk or diskette cannot be accessed.

ERROR_SECTOR_NOT_FOUND

The drive cannot find the sector requested.

ERROR_OUT_OF_PAPER

The printer is out of paper.

ERROR_WRITE_FAULT

The system cannot write to the specified device.

ERROR_READ_FAULT

The system cannot read from the specified device.

ERROR_GEN_FAILURE

A device attached to the system is not functioning.

ERROR_SHARING_VIOLATION

The process cannot access the file because it is being used by another process.

ERROR_LOCK_VIOLATION

The process cannot access the file because another process has locked a portion of the file.

ERROR_WRONG_DISK

The wrong diskette is in the drive. Insert %2 (Volume Serial Number: %3) into drive %1.

ERROR_SHARING_BUFFER_EXCEEDED

Too many files opened for sharing.

ERROR_HANDLE_EOF

Reached the end of the file.

ERROR_HANDLE_DISK_FULL

The disk is full.

ERROR_NOT_SUPPORTED

The request is not supported.

ERROR_REM_NOT_LIST

Windows cannot find the network path. Verify that the network path is correct and the destination computer is not busy or turned off. If Windows still cannot find the network path, contact your network administrator.

ERROR_DUP_NAME

You were not connected because a duplicate name exists on the network. If joining a domain, go to System in Control Panel to change the computer name and try again. If joining a workgroup, choose another workgroup name.

ERROR_BAD_NETPATH

The network path was not found.

ERROR_NETWORK_BUSY

The network is busy.

ERROR_DEV_NOT_EXIST

The specified network resource or device is no longer available.

ERROR_TOO_MANY_CMDS

The network BIOS command limit has been reached.

ERROR_ADAP_HDW_ERR

A network adapter hardware error occurred.

ERROR_BAD_NET_RESP

The specified server cannot perform the requested operation.

ERROR_UNEXP_NET_ERR

An unexpected network error occurred.

ERROR_BAD_REM_ADAP

The remote adapter is not compatible.

ERROR_PRINTQ_FULL

The printer queue is full.

ERROR_NO_SPOOL_SPACE

Space to store the file waiting to be printed is not available on the server.

ERROR_PRINT_CANCELLED

Your file waiting to be printed was deleted.

ERROR_NETNAME_DELETED

The specified network name is no longer available.

ERROR_NETWORK_ACCESS_DENIED

Network access is denied.

ERROR_BAD_DEV_TYPE

The network resource type is not correct.

ERROR_BAD_NET_NAME

The network name cannot be found.

ERROR_TOO_MANY_NAMES

The name limit for the local computer network adapter card was exceeded.

ERROR_TOO_MANY_SESS

The network BIOS session limit was exceeded.

ERROR_SHARING_PAUSED

The remote server has been paused or is in the process of being started.

ERROR_REQ_NOT_ACCEP

No more connections can be made to this remote computer at this time because there are already as many connections as the computer can accept.

ERROR_REDIR_PAUSED

The specified printer or disk device has been paused.

ERROR_FILE_EXISTS

The file exists.

ERROR_CANNOT_MAKE

The directory or file cannot be created.

ERROR_FAIL_I24

ERROR_OUT_OF_STRUCTURES

Storage to process this request is not available.

ERROR_ALREADY_ASSIGNED

The local device name is already in use.

ERROR_INVALID_PASSWORD

The specified network password is not correct.

ERROR_INVALID_PARAMETER

The parameter is incorrect.

ERROR_NET_WRITE_FAULT

A write fault occurred on the network.

ERROR_NO_PROC_SLOTS

The system cannot start another process at this time.

ERROR_TOO_MANY_SEMAPHORES

Cannot create another system semaphore.

ERROR_EXCL_SEM_ALREADY_OWNED

The exclusive semaphore is owned by another process.

ERROR_SEM_IS_SET

The semaphore is set and cannot be closed.

ERROR_TOO_MANY_SEM_REQUESTS

The semaphore cannot be set again.

ERROR_INVALID_AT_INTERRUPT_TIME

Cannot request exclusive semaphores at interrupt time.

ERROR_SEM_OWNER_DIED

The previous ownership of this semaphore has ended.

ERROR_SEM_USER_LIMIT

Insert the diskette for drive %1.

ERROR_DISK_CHANGE

The program stopped because an alternate diskette was not inserted.

ERROR_DRIVE_LOCKED

The disk is in use or locked by another process.

ERROR_BROKEN_PIPE

The pipe has been ended.

ERROR_OPEN_FAILED

The system cannot open the device or file specified.

ERROR_BUFFER_OVERFLOW

The file name is too long.

ERROR_DISK_FULL

There is not enough space on the disk.

ERROR_NO_MORE_SEARCH_HANDLES

No more internal file identifiers available.

ERROR_INVALID_TARGET_HANDLE

The target internal file identifier is incorrect.

ERROR_INVALID_CATEGORY

The IOCTL call made by the application program is not correct.

ERROR_INVALID_VERIFY_SWITCH

The verify-on-write switch parameter value is not correct.

ERROR_BAD_DRIVER_LEVEL

The system does not support the command requested.

ERROR_CALL_NOT_IMPLEMENTED

This function is not supported on this system.

ERROR_SEM_TIMEOUT

The semaphore timeout period has expired.

ERROR_INSUFFICIENT_BUFFER

The data area passed to a system call is too small.

ERROR_INVALID_NAME

The filename, directory name, or volume label syntax is incorrect.

ERROR_INVALID_LEVEL

The system call level is not correct.

ERROR_NO_VOLUME_LABEL

The disk has no volume label.

ERROR_MOD_NOT_FOUND

The specified module could not be found.

ERROR_PROC_NOT_FOUND

The specified procedure could not be found.

ERROR_WAIT_NO_CHILDREN

There are no child processes to wait for.

ERROR_CHILD_NOT_COMPLETE

The %1 application cannot be run in Win32 mode.

ERROR_DIRECT_ACCESS_HANDLE

Attempt to use a file handle to an open disk partition for an operation other than raw disk I/O.

ERROR_NEGATIVE_SEEK

An attempt was made to move the file pointer before the beginning of the file.

ERROR_SEEK_ON_DEVICE

The file pointer cannot be set on the specified device or file.

ERROR_IS_JOIN_TARGET

A JOIN or SUBST command cannot be used for a drive that contains previously joined drives.

ERROR_IS_JOINED

An attempt was made to use a JOIN or SUBST command on a drive that has already been joined.

ERROR_IS_SUBSTED

An attempt was made to use a JOIN or SUBST command on a drive that has already been substituted.

ERROR_NOT_JOINED

The system tried to delete the JOIN of a drive that is not joined.

ERROR_NOT_SUBSTED

The system tried to delete the substitution of a drive that is not substituted.

ERROR_JOIN_TO_JOIN

The system tried to join a drive to a directory on a joined drive.

ERROR_SUBST_TO_SUBST

The system tried to substitute a drive to a directory on a substituted drive.

ERROR_JOIN_TO_SUBST

The system tried to join a drive to a directory on a substituted drive.

ERROR_SUBST_TO_JOIN

The system tried to SUBST a drive to a directory on a joined drive.

ERROR_BUSY_DRIVE

The system cannot perform a JOIN or SUBST at this time.

ERROR_SAME_DRIVE

The system cannot join or substitute a drive to or for a directory on the same drive.

ERROR_DIR_NOT_ROOT

The directory is not a subdirectory of the root directory.

ERROR_DIR_NOT_EMPTY

The directory is not empty.

ERROR_IS_SUBST_PATH

The path specified is being used in a substitute.

ERROR_IS_JOIN_PATH

Not enough resources are available to process this command.

ERROR_PATH_BUSY

The path specified cannot be used at this time.

ERROR_IS_SUBST_TARGET

An attempt was made to join or substitute a drive for which a directory on the drive is the target of a previous substitute.

ERROR_SYSTEM_TRACE

System trace information was not specified in your CONFIG.SYS file, or tracing is disallowed.

ERROR_INVALID_EVENT_COUNT

The number of specified semaphore events for DosMuxSemWait is not correct.

ERROR_TOO_MANY_MUXWAITERS

DosMuxSemWait did not execute; too many semaphores are already set.

ERROR_INVALID_LIST_FORMAT

The DosMuxSemWait list is not correct.

ERROR_LABEL_TOO_LONG

The volume label you entered exceeds the label character limit of the target file system.

ERROR_TOO_MANY_TCBS

Cannot create another thread.

ERROR_SIGNAL_REFUSED

The recipient process has refused the signal.

ERROR_DISCARDED

The segment is already discarded and cannot be locked.

ERROR_NOT_LOCKED

The segment is already unlocked.

ERROR_BAD_THREADID_ADDR

The address for the thread ID is not correct.

ERROR_BAD_ARGUMENTS

One or more arguments are not correct.

ERROR_BAD_PATHNAME

The specified path is invalid.

ERROR_SIGNAL_PENDING

A signal is already pending.

ERROR_MAX_THRDS_REACHED

No more threads can be created in the system.

ERROR_LOCK_FAILED

Unable to lock a region of a file.

ERROR_BUSY

The requested resource is in use.

ERROR_DEVICE_SUPPORT_IN_PROGRESS

Device’s command support detection is in progress.

ERROR_CANCEL_VIOLATION

A lock request was not outstanding for the supplied cancel region.

ERROR_ATOMIC_LOCKS_NOT_SUPPORTED

The file system does not support atomic changes to the lock type.

ERROR_INVALID_SEGMENT_NUMBER

The system detected a segment number that was not correct.

ERROR_INVALID_ORDINAL

The operating system cannot run %1.

ERROR_ALREADY_EXISTS

Cannot create a file when that file already exists.

ERROR_INVALID_FLAG_NUMBER

The flag passed is not correct.

ERROR_SEM_NOT_FOUND

The specified system semaphore name was not found.

ERROR_INVALID_STARTING_CODESEG

The operating system cannot run %1.

ERROR_INVALID_STACKSEG

The operating system cannot run %1.

ERROR_INVALID_MODULETYPE

The operating system cannot run %1.

ERROR_INVALID_EXE_SIGNATURE

Cannot run %1 in Win32 mode.

ERROR_EXE_MARKED_INVALID

The operating system cannot run %1.

ERROR_BAD_EXE_FORMAT

%1 is not a valid Win32 application.

ERROR_ITERATED_DATA_EXCEEDS_64k

The operating system cannot run %1.

ERROR_INVALID_MINALLOCSIZE

The operating system cannot run %1.

ERROR_DYNLINK_FROM_INVALID_RING

The operating system cannot run this application program.

ERROR_IOPL_NOT_ENABLED

The operating system is not presently configured to run this application.

ERROR_INVALID_SEGDPL

The operating system cannot run %1.

ERROR_AUTODATASEG_EXCEEDS_64k

The operating system cannot run this application program.

ERROR_RING2SEG_MUST_BE_MOVABLE

The code segment cannot be greater than or equal to 64K.

ERROR_RELOC_CHAIN_XEEDS_SEGLIM

The operating system cannot run %1.

ERROR_INFLOOP_IN_RELOC_CHAIN

The operating system cannot run %1.

ERROR_ENVVAR_NOT_FOUND

The system could not find the environment option that was entered.

ERROR_NO_SIGNAL_SENT

No process in the command subtree has a signal handler.

ERROR_FILENAME_EXCED_RANGE

The filename or extension is too long.

ERROR_RING2_STACK_IN_USE

The ring 2 stack is in use.

ERROR_META_EXPANSION_TOO_LONG

The global filename characters, * or ?, are entered incorrectly or too many global filename characters are specified.

ERROR_INVALID_SIGNAL_NUMBER

The signal being posted is not correct.

ERROR_THREAD_1_INACTIVE

The signal handler cannot be set.

ERROR_LOCKED

The segment is locked and cannot be reallocated.

ERROR_TOO_MANY_MODULES

Too many dynamic-link modules are attached to this program or dynamic-link module.

ERROR_NESTING_NOT_ALLOWED

Cannot nest calls to LoadModule.

ERROR_EXE_MACHINE_TYPE_MISMATCH

This version of %1 is not compatible with the version of Windows you’re running. Check your computer’s system information and then contact the software publisher.

ERROR_EXE_CANNOT_MODIFY_SIGNED_BINARY

The image file %1 is signed, unable to modify.

ERROR_EXE_CANNOT_MODIFY_STRONG_SIGNED_BINARY

The image file %1 is strong signed, unable to modify.

ERROR_FILE_CHECKED_OUT

This file is checked out or locked for editing by another user.

ERROR_CHECKOUT_REQUIRED

The file must be checked out before saving changes.

ERROR_BAD_FILE_TYPE

The file type being saved or retrieved has been blocked.

ERROR_FILE_TOO_LARGE

The file size exceeds the limit allowed and cannot be saved.

ERROR_FORMS_AUTH_REQUIRED

Access Denied. Before opening files in this location, you must first add the web site to your trusted sites list, browse to the web site, and select the option to login automatically.

ERROR_VIRUS_INFECTED

Operation did not complete successfully because the file contains a virus or potentially unwanted software.

ERROR_VIRUS_DELETED

This file contains a virus or potentially unwanted software and cannot be opened. Due to the nature of this virus or potentially unwanted software, the file has been removed from this location.

ERROR_PIPE_LOCAL

The pipe is local.

ERROR_BAD_PIPE

The pipe state is invalid.

ERROR_PIPE_BUSY

All pipe instances are busy.

ERROR_NO_DATA

The pipe is being closed.

ERROR_PIPE_NOT_CONNECTED

No process is on the other end of the pipe.

ERROR_MORE_DATA

More data is available.

ERROR_VC_DISCONNECTED

The session was canceled.

ERROR_INVALID_EA_NAME

The specified extended attribute name was invalid.

ERROR_EA_LIST_INCONSISTENT

The extended attributes are inconsistent.

WAIT_TIMEOUT

The wait operation timed out.

ERROR_NO_MORE_ITEMS

No more data is available.

ERROR_CANNOT_COPY

The copy functions cannot be used.

ERROR_DIRECTORY

The directory name is invalid.

ERROR_EAS_DIDNT_FIT

The extended attributes did not fit in the buffer.

ERROR_EA_FILE_CORRUPT

The extended attribute file on the mounted file system is corrupt.

ERROR_EA_TABLE_FULL

The extended attribute table file is full.

ERROR_INVALID_EA_HANDLE

The specified extended attribute handle is invalid.

ERROR_EAS_NOT_SUPPORTED

The mounted file system does not support extended attributes.

ERROR_NOT_OWNER

Attempt to release mutex not owned by caller.

ERROR_TOO_MANY_POSTS

Too many posts were made to a semaphore.

ERROR_PARTIAL_COPY

Only part of a ReadProcessMemory or WriteProcessMemory request was completed.

ERROR_OPLOCK_NOT_GRANTED

The oplock request is denied.

ERROR_INVALID_OPLOCK_PROTOCOL

An invalid oplock acknowledgment was received by the system.

ERROR_DISK_TOO_FRAGMENTED

The volume is too fragmented to complete this operation.

ERROR_DELETE_PENDING

The file cannot be opened because it is in the process of being deleted.

ERROR_INCOMPATIBLE_WITH_GLOBAL_SHORT_NAME_REGISTRY_SETTING

Short name settings may not be changed on this volume due to the global registry setting.

ERROR_SHORT_NAMES_NOT_ENABLED_ON_VOLUME

Short names are not enabled on this volume.

ERROR_SECURITY_STREAM_IS_INCONSISTENT

The security stream for the given volume is in an inconsistent state. Please run CHKDSK on the volume.

ERROR_INVALID_LOCK_RANGE

A requested file lock operation cannot be processed due to an invalid byte range.

ERROR_IMAGE_SUBSYSTEM_NOT_PRESENT

The subsystem needed to support the image type is not present.

ERROR_NOTIFICATION_GUID_ALREADY_DEFINED

The specified file already has a notification GUID associated with it.

ERROR_INVALID_EXCEPTION_HANDLER

An invalid exception handler routine has been detected.

ERROR_DUPLICATE_PRIVILEGES

Duplicate privileges were specified for the token.

ERROR_NO_RANGES_PROCESSED

No ranges for the specified operation were able to be processed.

ERROR_NOT_ALLOWED_ON_SYSTEM_FILE

Operation is not allowed on a file system internal file.

ERROR_DISK_RESOURCES_EXHAUSTED

The physical resources of this disk have been exhausted.

ERROR_INVALID_TOKEN

The token representing the data is invalid.

ERROR_DEVICE_FEATURE_NOT_SUPPORTED

The device does not support the command feature.

ERROR_MR_MID_NOT_FOUND

The system cannot find message text for message number 0x%1 in the message file for %2.

ERROR_SCOPE_NOT_FOUND

The scope specified was not found.

ERROR_UNDEFINED_SCOPE

The Central Access Policy specified is not defined on the target machine.

ERROR_INVALID_CAP

The Central Access Policy obtained from Active Directory is invalid.

ERROR_DEVICE_UNREACHABLE

The device is unreachable.

ERROR_DEVICE_NO_RESOURCES

The target device has insufficient resources to complete the operation.

ERROR_DATA_CHECKSUM_ERROR

A data integrity checksum error occurred. Data in the file stream is corrupt.

ERROR_INTERMIXED_KERNEL_EA_OPERATION

An attempt was made to modify both a KERNEL and normal Extended Attribute (EA) in the same operation.

ERROR_FILE_LEVEL_TRIM_NOT_SUPPORTED

Device does not support file-level TRIM.

ERROR_OFFSET_ALIGNMENT_VIOLATION

The command specified a data offset that does not align to the device’s granularity/alignment.

ERROR_INVALID_FIELD_IN_PARAMETER_LIST

The command specified an invalid field in its parameter list.

ERROR_OPERATION_IN_PROGRESS

An operation is currently in progress with the device.

ERROR_BAD_DEVICE_PATH

An attempt was made to send down the command via an invalid path to the target device.

ERROR_TOO_MANY_DESCRIPTORS

The command specified a number of descriptors that exceeded the maximum supported by the device.

ERROR_SCRUB_DATA_DISABLED

Scrub is disabled on the specified file.

ERROR_NOT_REDUNDANT_STORAGE

The storage device does not provide redundancy.

ERROR_RESIDENT_FILE_NOT_SUPPORTED

An operation is not supported on a resident file.

ERROR_COMPRESSED_FILE_NOT_SUPPORTED

An operation is not supported on a compressed file.

ERROR_DIRECTORY_NOT_SUPPORTED

An operation is not supported on a directory.

ERROR_NOT_READ_FROM_COPY

The specified copy of the requested data could not be read.

ERROR_FAIL_NOACTION_REBOOT

No action was taken as a system reboot is required.

ERROR_FAIL_SHUTDOWN

The shutdown operation failed.

ERROR_FAIL_RESTART

The restart operation failed.

ERROR_MAX_SESSIONS_REACHED

The maximum number of sessions has been reached.

ERROR_THREAD_MODE_ALREADY_BACKGROUND

The thread is already in background processing mode.

ERROR_THREAD_MODE_NOT_BACKGROUND

The thread is not in background processing mode.

ERROR_PROCESS_MODE_ALREADY_BACKGROUND

The process is already in background processing mode.

ERROR_PROCESS_MODE_NOT_BACKGROUND

The process is not in background processing mode.

Источник

замечания

Каждый поток будет иметь свой последний код ошибки. Windows API установит последний код ошибки в вызывающем потоке.

Вы всегда должны вызывать GetLastError сразу после проверки возвращаемого значения функции API Windows.

Большинство функций Windows API устанавливают последний код ошибки, когда они терпят неудачу. Некоторые также установят последний код ошибки, когда они преуспеют. Существует ряд функций, которые не устанавливают последний код ошибки. Всегда обращайтесь к документации по функциям Windows API.

Невозможно использовать FORMAT_MESSAGE_FROM_SYSTEM без FORMAT_MESSAGE_IGNORE_INSERTS при использовании функции FormatMessage для получения описания кода ошибки.

Вступление

API Windows предоставляется с помощью C-вызываемого интерфейса. Успех или сбой вызова API сообщаются строго через возвращаемые значения. Исключения не являются частью документированного контракта (хотя некоторые реализации API могут вызывать исключения SEH , например, при передаче аргумента lpCommandLine только для чтения в CreateProcess ).

Сообщение об ошибке грубо относится к одной из четырех категорий:

  • Только возвращаемое значение
  • Возвращаемое значение с дополнительной информацией о сбое
  • Возвращаемое значение с дополнительной информацией об отказе и успехе
  • Возвращаемое значение HRESULT

Документация для каждого вызова API явно вызывается, как сообщаются ошибки. Всегда обращайтесь к документации.

Ошибка, сообщенная только возвратным значением

Некоторые вызовы API возвращают единый флаг отказа / успеха без какой-либо дополнительной информации (например, GetObject ):

if ( GetObjectW( obj, 0, NULL ) == 0 ) {
    // Failure: no additional information available.
}

Сообщается об ошибке с сообщением об ошибке

В дополнение к возвращаемому значению отказа / успеха некоторые вызовы API также устанавливают последнюю ошибку при сбое (например, CreateWindow ). Документация обычно содержит следующую стандартную формулировку для этого случая:

Если функция завершается успешно, возвращаемое значение <значение успеха API> .
Если функция не работает, возвращаемое значение <значение ошибки API> . Чтобы получить расширенную информацию об ошибке, вызовите GetLastError .

if ( CreateWindowW( ... ) == NULL ) {
    // Failure: get additional information.
    DWORD dwError = GetLastError();
} else {
    // Success: must not call GetLastError.
}

Очень важно, что вы вызываете GetLastError() НЕМЕДЛЕННО. Последний код ошибки может быть перезаписан любой другой функцией, поэтому, если есть дополнительная функция вызова между неудавшейся функцией и вызовом GetLastError() , возврат из GetLastError() больше не будет надежным. Будьте особенно осторожны при работе с конструкторами C ++.

Как только вы получите код ошибки, вам нужно будет его интерпретировать. Вы можете получить полный список кодов ошибок в MSDN на странице Системные коды ошибок (Windows) . Кроме того, вы можете посмотреть в своих файлах заголовков системы; файл со всеми константами кода ошибки — winerror.h . (Если у вас есть официальный SDK от Microsoft для Windows 8 или новее, это находится в shared папке с папкой include.)

Заметки о вызове GetLastError() на других языках программирования

.net (C #, VB и т. д.)

С .net вы не должны P / Invoke в GetLastError() напрямую. Это связано с тем, что среда выполнения .net сделает другие вызовы Windows API одним и тем же потоком за вашей спиной. Например, сборщик мусора может вызвать VirtualFree() если он найдет достаточно памяти, которую он больше не использует, и это может произойти между вашим назначенным вызовом функции и вашим вызовом GetLastError() .

Вместо этого .net предоставляет Marshal.GetLastWin32Error() , которая будет извлекать последнюю ошибку из последнего вызова P / Invoke, который вы сами сделали. Используйте это вместо прямого вызова GetLastError() .

(.net, похоже, не мешает вам импортировать GetLastError() любом случае, я не уверен, почему.)

Идти

Различные средства, предоставляемые Go для вызова DLL-функций (которые находятся как в syscall пакета, syscall и в пакете golang.org/x/sys/windows ), возвращают три значения: r1 , r2 и err . r2 никогда не используется; вы можете использовать пустой идентификатор. r1 — возвращаемое значение функции. err является результатом вызова GetLastError() но преобразуется в тип, реализующий error , поэтому вы можете передать его вызывающим функциям для обработки.

Поскольку Go не знает, когда вызывать GetLastError() а когда нет, он всегда будет возвращать ошибку nil . Поэтому типичная идиома обработки ошибок Go

r1, _, err := syscall.Syscall12(CreateWindowW.Addr(), ...)
if err != nil {
    // handle err
}
// use r1

не будет работать. Вместо этого вы должны проверить r1 точно так же, как и на C, и использовать только err если это указывает, что функция возвратила ошибку:

r1, _, err := syscall.Syscall12(CreateWindowW.Addr(), ...)
if r1 == 0 {
    // handle err
}
// use r1

Сообщается об ошибке с дополнительной информацией о сбоях и успехах

Некоторые вызовы API могут преуспеть или сбой более чем одним способом. API обычно возвращают дополнительную информацию как для успешных вызовов, так и для ошибок (например, CreateMutex ).

if ( CreateMutexW( NULL, TRUE, L"GlobalMyNamedMutex" ) == NULL ) {
    // Failure: get additional information.
    DWORD dwError = GetLastError();
} else {
    // Success: Determine which mutex was returned.
    if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
        // Existing mutex object returned.
    } else {
        // Newly created mutex object returned.
    }
}

Ошибка, сообщенная как значение HRESULT

HRESULT s — числовые 32-битные значения, где биты или диапазоны бит кодируют четко определенную информацию. MSB — это флаг отказа / успеха, а остальные бит хранят дополнительную информацию. Отказ или успех можно определить с помощью макросов FAILED или SUCCEEDED . HRESULT s обычно используются совместно с COM, но также отображаются в реализациях, отличных от COM (например, StringCchPrintf ).

const size_t cchBuf = 5;
wchar_t buffer[cchBuf] = { 0 };
HRESULT hr = StringCchPrintfW( buffer, cchBuf, L"%s", L"Hello, world!" );
if ( FAILED( hr ) ) {
    // Failure: Determine specific reason.
    switch ( hr ) {
    case STRSAFE_E_INSUFFICIENT_BUFFER:
        // Buffer too small; increase buffer and retry.
        ...
    case STRSAFE_E_INVALID_PARAMETER:
        // Invalid parameter; implement custom error handling (e.g. logging).
        ...
    default:
        // Some other error code; implement custom error handling (e.g. logging).
        ...
    }
}

Преобразование кода ошибки в строку сообщения

GetLastError возвращает числовой код ошибки. Чтобы получить описательное сообщение об ошибке ( например , для отображения пользователю), вы можете вызвать FormatMessage :

// This functions fills a caller-defined character buffer (pBuffer)
// of max length (cchBufferLength) with the human-readable error message
// for a Win32 error code (dwErrorCode).
// 
// Returns TRUE if successful, or FALSE otherwise.
// If successful, pBuffer is guaranteed to be NUL-terminated.
// On failure, the contents of pBuffer are undefined.
BOOL GetErrorMessage(DWORD dwErrorCode, LPTSTR pBuffer, DWORD cchBufferLength)
{
    if (cchBufferLength == 0)
    {
        return FALSE;
    }

    DWORD cchMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                                 NULL,  /* (not used with FORMAT_MESSAGE_FROM_SYSTEM) */
                                 dwErrorCode,
                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                 pBuffer,
                                 cchBufferLength,
                                 NULL);
    return (cchMsg > 0);
}

В C ++ вы можете значительно упростить интерфейс, используя класс std::string :

#include <Windows.h>
#include <exception>
#include <stdexcept>
#include <memory>
#include <string>
typedef std::basic_string<TCHAR> String;

String GetErrorMessage(DWORD dwErrorCode)
{
    LPTSTR psz = NULL;
    const DWORD cchMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
                                         | FORMAT_MESSAGE_IGNORE_INSERTS
                                         | FORMAT_MESSAGE_ALLOCATE_BUFFER,
                                       NULL, // (not used with FORMAT_MESSAGE_FROM_SYSTEM)
                                       dwErrorCode,
                                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                       reinterpret_cast<LPTSTR>(&psz),
                                       0,
                                       NULL);
    if (cchMsg > 0)
    {
        // Assign buffer to smart pointer with custom deleter so that memory gets released
        // in case String's c'tor throws an exception.
        auto deleter = [](void* p) { ::HeapFree(::GetProcessHeap(), 0, p); };
        std::unique_ptr<TCHAR, decltype(deleter)> ptrBuffer(psz, deleter);
        return String(ptrBuffer.get(), cchMsg);
    }
    else
    {
        throw std::runtime_error("Failed to retrieve error message string.");
    }
}

ПРИМЕЧАНИЕ. Эти функции также работают для значений HRESULT . Просто измените первый параметр из DWORD dwErrorCode на HRESULT hResult . Остальная часть кода может оставаться неизменной.

description ms.assetid title ms.topic ms.date

When many system functions fail, they set the last-error code.

4cc626ac-7574-44ce-8377-e0bdd8e74b7e

Retrieving the Last-Error Code

article

05/31/2018

Retrieving the Last-Error Code

When many system functions fail, they set the last-error code. If your application needs more details about an error, it can retrieve the last-error code using the GetLastError function and display a description of the error using the FormatMessage function.

The following example includes an error-handling function that prints the error message and terminates the process. The lpszFunction parameter is the name of the function that set the last-error code.

#include <windows.h>
#include <strsafe.h>

void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
}

void main()
{
    // Generate an error

    if(!GetProcessId(NULL))
        ErrorExit(TEXT("GetProcessId"));
}

Each thread will have its own last error code. The Windows API will set the last error code on the calling thread.

You should always call the GetLastError function immediately after checking a Windows API function’s return value.

The majority of Windows API functions set the last error code when they fail. Some will also set the last error code when they succeed. There are a number of functions that do not set the last error code. Always refer to the Windows API function’s documentation.

It is unsafe to use FORMAT_MESSAGE_FROM_SYSTEM without FORMAT_MESSAGE_IGNORE_INSERTS when using the FormatMessage function to get a description of an error code.

Introduction

The Windows API is provided by means of a C-callable interface. Success or failure of an API call is reported strictly through return values. Exceptions aren’t part of the documented contract (although some API implementations can raise SEH exceptions, e.g. when passing a read-only lpCommandLine argument to CreateProcess).

Error reporting roughly falls into one of four categories:

  • Return value only
  • Return value with additional information on failure
  • Return value with additional information on failure and success
  • HRESULT return value

The documentation for each API call explicitly calls out, how errors are reported. Always consult the documentation.

Error reported by return value only

Some API calls return a single failure/success flag, without any additional information (e.g. GetObject):

if ( GetObjectW( obj, 0, NULL ) == 0 ) {
    // Failure: no additional information available.
}

Error reported with additional information on failure

In addition to a failure/success return value, some API calls also set the last error on failure (e.g. CreateWindow). The documentation usually contains the following standard wording for this case:

If the function succeeds, the return value is <API-specific success value>.
If the function fails, the return value is <API-specific error value>. To get extended error information, call GetLastError.

if ( CreateWindowW( ... ) == NULL ) {
    // Failure: get additional information.
    DWORD dwError = GetLastError();
} else {
    // Success: must not call GetLastError.
}

It is vital that you call GetLastError() IMMEDIATELY. The last error code can be overwritten by any other function, so if there’s an extra function call between the function that failed and the call to GetLastError(), the return from GetLastError() will no longer be reliable. Take extra caution when dealing with C++ constructors.

Once you get an error code, you will need to interpret it. You can get a comprehensive list of error codes on MSDN, at the System Error Codes (Windows) page. Alternatively, you can look in your system header files; the file with all the error code constants is winerror.h. (If you have Microsoft’s official SDK for Windows 8 or newer, this is in the shared subfolder of the include folder.)

Notes on calling GetLastError() in other programming languages

.net languages (C#, VB, etc.)

With .net, you should not P/Invoke to GetLastError() directly. This is because the .net runtime will make other Windows API calls on the same thread behind your back. For instance, the garbage collector might call VirtualFree() if it finds enough memory that it is no longer using, and this can happen between your intended function call and your call to GetLastError().

Instead, .net provides the Marshal.GetLastWin32Error() function, which will retrieve the last error from the last P/Invoke call that you yourself made. Use this instead of calling GetLastError() directly.

(.net does not seem to stop you from importing GetLastError() anyway; I’m not sure why.)

Go

The various facilities provided by Go for calling DLL functions (which reside in both package syscall and package golang.org/x/sys/windows) return three values: r1, r2, and err. r2 is never used; you can use the blank identifier there. r1 is the function’s return value. err is the result of calling GetLastError() but converted into a type that implements error, so you can pass it up to calling functions to handle.

Because Go does not know when to call GetLastError() and when not to, it will always return a non-nil error. Therefore, the typical Go error-handling idiom

r1, _, err := syscall.Syscall12(CreateWindowW.Addr(), ...)
if err != nil {
    // handle err
}
// use r1

will not work. Instead, you must check r1, exactly as you would in C, and only use err if that indicates the function returned an error:

r1, _, err := syscall.Syscall12(CreateWindowW.Addr(), ...)
if r1 == 0 {
    // handle err
}
// use r1

Error reported with additional information on failure and success

Some API calls can succeed or fail in more than one way. The APIs commonly return additional information for both successful invocations as well as errors (e.g. CreateMutex).

if ( CreateMutexW( NULL, TRUE, L"GlobalMyNamedMutex" ) == NULL ) {
    // Failure: get additional information.
    DWORD dwError = GetLastError();
} else {
    // Success: Determine which mutex was returned.
    if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
        // Existing mutex object returned.
    } else {
        // Newly created mutex object returned.
    }
}

Error reported as HRESULT value

HRESULTs are numeric 32-bit values, where bits or bit ranges encode well-defined information. The MSB is a failure/success flag, with the remaining bits storing additional information. Failure or success can be determined using the FAILED or SUCCEEDED macros. HRESULTs are commonly used with COM, but appear in non-COM implementations as well (e.g. StringCchPrintf).

const size_t cchBuf = 5;
wchar_t buffer[cchBuf] = { 0 };
HRESULT hr = StringCchPrintfW( buffer, cchBuf, L"%s", L"Hello, world!" );
if ( FAILED( hr ) ) {
    // Failure: Determine specific reason.
    switch ( hr ) {
    case STRSAFE_E_INSUFFICIENT_BUFFER:
        // Buffer too small; increase buffer and retry.
        ...
    case STRSAFE_E_INVALID_PARAMETER:
        // Invalid parameter; implement custom error handling (e.g. logging).
        ...
    default:
        // Some other error code; implement custom error handling (e.g. logging).
        ...
    }
}

Converting an error code into a message string

GetLastError returns a numerical error code. To obtain a descriptive error message (e.g., to display to a user), you can call FormatMessage:

// This functions fills a caller-defined character buffer (pBuffer)
// of max length (cchBufferLength) with the human-readable error message
// for a Win32 error code (dwErrorCode).
// 
// Returns TRUE if successful, or FALSE otherwise.
// If successful, pBuffer is guaranteed to be NUL-terminated.
// On failure, the contents of pBuffer are undefined.
BOOL GetErrorMessage(DWORD dwErrorCode, LPTSTR pBuffer, DWORD cchBufferLength)
{
    if (cchBufferLength == 0)
    {
        return FALSE;
    }

    DWORD cchMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                                 NULL,  /* (not used with FORMAT_MESSAGE_FROM_SYSTEM) */
                                 dwErrorCode,
                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                 pBuffer,
                                 cchBufferLength,
                                 NULL);
    return (cchMsg > 0);
}

In C++, you can simplify the interface considerably by using the std::string class:

#include <Windows.h>
#include <exception>
#include <stdexcept>
#include <memory>
#include <string>
typedef std::basic_string<TCHAR> String;

String GetErrorMessage(DWORD dwErrorCode)
{
    LPTSTR psz = NULL;
    const DWORD cchMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
                                         | FORMAT_MESSAGE_IGNORE_INSERTS
                                         | FORMAT_MESSAGE_ALLOCATE_BUFFER,
                                       NULL, // (not used with FORMAT_MESSAGE_FROM_SYSTEM)
                                       dwErrorCode,
                                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                       reinterpret_cast<LPTSTR>(&psz),
                                       0,
                                       NULL);
    if (cchMsg > 0)
    {
        // Assign buffer to smart pointer with custom deleter so that memory gets released
        // in case String's c'tor throws an exception.
        auto deleter = [](void* p) { ::HeapFree(::GetProcessHeap(), 0, p); };
        std::unique_ptr<TCHAR, decltype(deleter)> ptrBuffer(psz, deleter);
        return String(ptrBuffer.get(), cchMsg);
    }
    else
    {
        throw std::runtime_error("Failed to retrieve error message string.");
    }
}

NOTE: These functions also work for HRESULT values. Just change the first parameter from DWORD dwErrorCode to HRESULT hResult. The rest of the code can remain unchanged.


Функция GetLastError
извлекает значение кода последней ошибки
вызывающего потока. Код последней ошибки
сохраняется при посредстве базового
компонента потока. Многие потоки не
записывают поверх друг друга коды
последней ошибки.

Синтаксис

Visual Basic: Прикладные
программы должны вызывать
err.
LastDllError
вместо GetLastError.

DWORD GetLastError(VOID); 

Параметры

У этой функции нет параметров.

Возвращаемые значения

Величина возвращаемого значения является
значение кода последней ошибки вызывающего
потока. Функции устанавливают это значение
путем вызова функция
SetLastError.
Раздел Возвращаемые значения каждой
статьи справочника по функциям обращает
внимание на условия, при которых функция
устанавливает код последней ошибки.

Windows 95/98/Me: Функции, которые фактически
реализованы в 16-разрядном коде, не
устанавливают код последней ошибки. Вы
должны игнорировать код последней ошибки,
когда вызываете эти функции. Они включают в
себя функции управления окном, функции GDI
и функции
мультимедийных
средств. Для функций, которые устанавливают
код последней ошибки, Вы не должны
надеяться на
возврат
функцией
GetLastError
того же самого значения и в среде Windows 95 /98/Me
и в среде Windows NT.

Замечания

Чтобы получить строку  для кодов
системных ошибок, используйте функцию
FormatMessage.
За полным перечнем кодов ошибок,
предусмотренных операционной системой, см.
статью
Коды
системных ошибок
.

Возвращаемые функцией коды ошибок не
являются частью спецификации Windows API
и
могут различаться в операционных системах
или драйверах устройств. По этой причине,
имеются функции, документация на которые не
включает в себя список кодов ошибок,
которые могут быть возвращены.
 

Вы должны вызвать функцию GetLastError
непосредственно тогда, когда величина
возвращаемого значения функции указывает,
что такой вызов даст вам полезную
информацию. Это делается потому, что
некоторые функции, когда они завершаются
успешно, вызывают функцию
SetLastError
с нулем, чтобы ликвидировать код ошибки
установленный самой последней

неудавшейся функцией.

Большинство функций, которые
устанавливают последнее значение кода
ошибки потока, устанавливает его тогда,
когда они завершаются ошибкой; несколько
функций устанавливают код ошибки, когда они
завершаются успешно. Функция, которая
завершилась сбоем, обычно указывается
кодом ошибки величины возвращаемого
значения типа нуля, ПУСТО (NULL) или — (минус)1.
Некоторые функции вызывают
SetLastError
при условии успешного завершения; на такие
случаи обращается внимание в статьях
справочника для каждой функции.

Коды Ошибки — это 32-разрядные значения (бит
31 — старший значащий бит). Бит 29
зарезервирован для определяемых
программой кодов ошибок; нет ни одного кода
системных ошибок, которые бы имели
установленным этот бит. Если Вы определяете
код ошибки для своего приложения,
устанавливайте этот бит в единицу. Это
указывает, что код ошибки был задан
прикладной программой и гарантирует, что
ваш код ошибки не войдет в противоречие с
любыми кодами ошибок, определенными
системой.

Чтобы преобразовывать системную ошибку в
значение
HRESULT,
используйте макрокоманду


HRESULT_FROM_WIN32
.

Код примера

Пример смотри в статье Извлечение
кода последней ошибки
.

Смотри также


Краткий обзор Обработка ошибок, Функции
обработки ошибок,
FormatMessage,
HRESULT_FROM_WIN32,
SetLastError,
SetLastErrorEx



Размещение и совместимость GetLastError



Windows. NET Server


Да



Windows XP


Да



Windows 2000


Да



Windows NT


Да



Windows Me


Да



Windows 98


Да



Windows 95


Да


Используемая библиотека



Kernel32.lib



Заголовочный файл

 


— объявлено в



Winbase.h


— включено в



Windows.h



Unicode

Нет



Замечания по платформе


Не имеется

Hosted by uCoz

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

error_get_lastGet the last occurred error

Description

error_get_last(): ?array

Parameters

This function has no parameters.

Return Values

Returns an associative array describing the last error with keys «type»,
«message», «file» and «line». If the error has been caused by a PHP
internal function then the «message» begins with its name.
Returns null if there hasn’t been an error yet.

Examples

Example #1 An error_get_last() example


<?php
echo $a;
print_r(error_get_last());
?>

The above example will output
something similar to:

Array
(
    [type] => 8
    [message] => Undefined variable: a
    [file] => C:WWWindex.php
    [line] => 2
)

See Also

  • Error constants
  • Variable $php_errormsg
  • error_clear_last() — Clear the most recent error
  • Directive display_errors
  • Directive html_errors
  • Directive xmlrpc_errors

dmgx dot michael at gmail dot com

12 years ago


If an error handler (see set_error_handler ) successfully handles an error then that error will not be reported by this function.

nicolas dot grekas+php at gmail dot com

9 years ago


[Editor's note: as of PHP 7.0.0 there is error_clear_last() to clear the most recent error.]

To clear error_get_last(), or put it in a well defined state, you should use the code below. It works even when a custom error handler has been set.

<?php
// var_dump or anything else, as this will never be called because of the 0set_error_handler('var_dump', 0);

@

$undef_var;restore_error_handler();
// error_get_last() is now in a well known state:

// Undefined variable: undef_var

... // Do something
$e = error_get_last();

...

?>

Skrol29

12 years ago


Function error_get_last() will return an error information even if the error is hidden because you've used character @, because of  the "error_reporting" directive in the php.ini file, or because you've used function error_reporting().

Examples:

<?php
error_reporting
(E_ALL ^ E_NOTICE);
$y = $x;
$err = error_get_last();
var_export($err);
?>
Will display: array ( 'type' => 8, 'message' => 'Undefined variable: x', 'file' => 'test.php', 'line' => 4, )

<?php
$y
= @$x;
$err = error_get_last();
var_export($err);
?>
Will display: array ( 'type' => 8, 'message' => 'Undefined variable: x', 'file' => 'test.php', 'line' => 4, )

michael at getsprink dot com

13 years ago


The error_get_last() function will give you the most recent error even when that error is a Fatal error.

Example Usage:

<?php

register_shutdown_function

('handleFatalPhpError');

function

handleFatalPhpError() {
  
$last_error = error_get_last();
   if(
$last_error['type'] === E_ERROR) {
      echo
"Can do custom output and/or logging for fatal error here...";
   }
}
?>

vike2000 at google mail domain

10 years ago


To know if something happened between two statements one can of course use a special string with user_error() (in lieu of a built-in special reset mentioned by mail at mbaierl dot com): <?php
@user_error($error_get_last_mark='error_get_last mark');
$not_set;
$error_get_last=error_get_last();
$something_happened=($error_get_last['message']!=$error_get_last_mark); ?>

If your <?php set_error_handler(function) ?> function returns true then you'll have to roll you own error_get_last functionality. (Shortly mentioned by dmgx dot michael at gmail dot com).

To manual moderators: Re php.net/manual/add-note.php: Since i guess the above technically sorts under "References to other notes" i feel the need to defend myself with that i'm thinking it might show for usability where other's say it fails and no, i haven't got any other medium to reach the readers of the php manual notes.
Also, you could have some examples of what notes you think is okay. Thanks for your moderation.

Brad

14 years ago


Like $php_errormsg, the return value of this function may not be updated if a user-defined error handler returns non-FALSE. Tested on PHP 5.2.6.

<?php
var_dump
(PHP_VERSION);
// Outputs: string(5) "5.2.6"@trigger_error("foo");
$e=error_get_last();
var_dump($e['message']);
// Outputs: string(3) "foo"set_error_handler(create_function('$a,$b',''));

@

trigger_error("bar");
$e=error_get_last();
var_dump($e['message']);
// Outputs: string(3) "foo"set_error_handler(create_function('$a,$b','return false;'));

@

trigger_error("baz");
$e=error_get_last();
var_dump($e['message']);
// Outputs: string(3) "baz"
?>

iant at clickwt dot com

13 years ago


Beware that registing a shutdown function to catch errors won't work if other shutdown functions throw errors.

<?php

register_shutdown_function

('cleanupObjects');
register_shutdown_function('handleFatalPhpError');

function

cleanupObjects() {
  
trigger_error('An insignificant problem', E_USER_WARNING);
}

function

handleFatalPhpError() {
  
$last_error = error_get_last();
   if(
$last_error['type'] === E_ERROR || $last_error['type'] === E_USER_ERROR) {
      echo
"Can do custom output and/or logging for fatal error here...";
   }
}
trigger_error('Something serious', E_USER_ERROR);?>

In the above code, $last_error will contain the warning, becuase cleanupObjects() is called first.

Brad

14 years ago


It can't be completely reset, but you can "clear" it well enough for all practical purposes:
<?php
@trigger_error("");
// do stuff...
$e=error_get_last();
if(
$e['message']!==''){
   
// An error occurred
}
?>

php at joert dot net

11 years ago


To simulate this function in a horrid way for php <5.2, you can use something like this.
<?php
if( !function_exists('error_get_last') ) {
   
set_error_handler(
       
create_function(
           
'$errno,$errstr,$errfile,$errline,$errcontext',
           
'
                global $__error_get_last_retval__;
                $__error_get_last_retval__ = array(
                    'type'        => $errno,
                    'message'        => $errstr,
                    'file'        => $errfile,
                    'line'        => $errline
                );
                return false;
            '
       
)
    );
    function
error_get_last() {
        global
$__error_get_last_retval__;
        if( !isset(
$__error_get_last_retval__) ) {
            return
null;
        }
        return
$__error_get_last_retval__;
    }
}
?>

admin at manomite dot net

5 years ago


This is a simple debugging script for mail functions...

<?php
//Built By Manomite for Debuggingclass Error{

function

__construct(){error_reporting ( E_ALL ^ E_NOTICE );
$err = error_get_last ();

if(

$err){$res = "An error has occurred in your application sir.n Details Include " .$err.""mail("admin@manomite.net","Error Occurred",$res,$from);
}
}
}
?>

scott at eyefruit dot com

12 years ago


If you have the need to check whether an error was a fatal error  before PHP 5.2 (in my case, within an output buffer handler), you can use the following hack:

<?php
# Check if there was a PHP fatal error.
# Using error_get_last is the "right" way, but it requires PHP 5.2+. The back-up is a hack.
if (function_exists('error_get_last')) {
   
$lastPHPError = error_get_last();
   
$phpFatalError = isset($lastPHPError) && $lastPHPError['type'] === E_ERROR;
} else {
   
$phpFatalError = strstr($output, '<b>Fatal error</b>:') && ! strstr($output, '</html>');
}
?>

This is, of course, language-dependent, so it wouldn't be good in widely-distributed code, but it may help in certain cases (or at least be the base of something that would work).

mail at mbaierl dot com

14 years ago


This function is pretty useless, as it can not be reset, so there is no way to know if the error really happened on the line before this function call.

phil at wisb dot net

14 years ago


While mail at mbaierl dot com makes the point that this function isn't best for reporting the possible error condition of the most recently executed step, there are situations in which it is especially helpful to know the last error—regardless of when it occurred.

As an example, imagine if you had some code that captured the output from dynamic pages, and cached it for faster delivery to subsequent visitors. A final sanity check would be to see if an error has occurred anywhere during the execution of the script. If there has been an error, we probably don't want to cache that page.

  • Извлекать ошибки что значит
  • Измените загрузку сушильная машина miele ошибка
  • Изменение состояния профиля дбд ошибка
  • Изменение содержания текста исправление ошибок это
  • Изменение площади земельного участка при исправлении реестровой ошибки