DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

GetErrorMessageWithId

Returns an error message string, in the current user's default language, that corresponds to the given OS error code. If the error code is zero, the empty string is returned.

function GetErrorMessageWithId(const OSErrorCode: Cardinal): string;
begin
  if OSErrorCode <> 0 then
    Result := SysUserErrorMessage(OSErrorCode)
  else
    Result := '';
end;

SysUserErrorMessage

Returns an error message string, in the current user's default language, that corresponds to the given OS error code.

function SysUserErrorMessage(const OSErrorCode: Cardinal): string;
var
  MessageBuffer: PChar;
begin
  Windows.FormatMessage(
    Windows.FORMAT_MESSAGE_ALLOCATE_BUFFER or Windows.FORMAT_MESSAGE_FROM_SYSTEM,
    nil,
    OSErrorCode,
    Windows.LANG_USER_DEFAULT,
    @MessageBuffer,
    0,
    nil
  );
  try
    Result := SysUtils.Trim(MessageBuffer);
  finally
    Windows.LocalFree(Windows.HLOCAL(MessageBuffer));
  end;
end;

View the whole database.

Go to the DelphiDabbler website.