Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu
Online

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.163 Beiträge
 
Delphi 12 Athens
 
#8

AW: DLL/Lib: Parameter Validierung

  Alt 28. Mär 2018, 17:56
Exceptions gibt es im C auch, nur kennt C natürlich die Delphi-Exceptionklassen nicht und kann da dann nichtmal den im unbekannten Message-Text auswerten.

Nja, du kannst natürlich auch LongBool-Results nehmen (in C++ ist der BOOL gern 32 Bit groß),
definierst dir eine Reihe Fehlercodes, nach dem Muster der WinAPI, wie z.B. HRESULT,
und nutzt fleißig MSDN-Library durchsuchenSetLastError. (natürlich nur wenn es auch einen Fehler gab, also nur bei Result=False)

FACILITY ist ein Code für deine Komponente/Library.

Delphi-Quellcode:
{------------------------------}
{     OLE Error Codes          }
{------------------------------}

(*
  The return value of OLE APIs and methods is an HRESULT.
  This is not a handle to anything, but is merely a 32-bit value
  with several fields encoded in the value.  The parts of an
  HRESULT are shown below.

  HRESULTs are 32 bit values layed out as follows:

  3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  +-+-+-+-+-+---------------------+-------------------------------+
  |S|R|C|N|r|    Facility        |              Code            |
  +-+-+-+-+-+---------------------+-------------------------------+

  where

      S - Severity - indicates success/fail
          0 - Success
          1 - Fail (COERROR)

      R - reserved portion of the facility code, corresponds to NT's
              second severity bit.

      C - reserved portion of the facility code, corresponds to NT's
              C field.

      N - reserved portion of the facility code. Used to indicate a
              mapped NT status value.

      r - reserved portion of the facility code. Reserved for internal
              use. Used to indicate HRESULT values that are not status
              values, but are instead message ids for display strings.

      Facility - is the facility code

      Code - is the facility's status code
*)


const
  SEVERITY_SUCCESS = 0;
  SEVERITY_ERROR = 1;

function Succeeded(Status: HRESULT): BOOL; inline;
function Failed(Status: HRESULT): BOOL; inline;
function IsError(Status: HRESULT): BOOL; inline;
function HResultCode(hr: HRESULT): Integer; inline;
function HResultFacility(hr: HRESULT): Integer; inline;
function HResultSeverity(hr: HRESULT): Integer; inline;
function MakeResult(sev, fac, code: Integer): HResult; inline;

const
  { HRESULT value definitions }
  { Codes $4000-$40ff are reserved for OLE }

  S_OK = $00000000;
  S_FALSE = $00000001;

  NOERROR = 0;

  E_UNEXPECTED = HRESULT($8000FFFF);
  E_NOTIMPL = HRESULT($80004001);
  E_OUTOFMEMORY = HRESULT($8007000E);
  E_INVALIDARG = HRESULT($80070057);
  E_NOINTERFACE = HRESULT($80004002);
  ...
oder
Delphi-Quellcode:
{ DOS and OS/2 Compatible Error Code definitions returned by the Win32 Base
  API functions. }



{ Translated from WINERROR.H }
{ Error code definitions for the Win32 API functions }

(*
  Values are 32 bit values layed out as follows:
  3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  +---+-+-+-----------------------+-------------------------------+
  |Sev|C|R|    Facility          |              Code            |
  +---+-+-+-----------------------+-------------------------------+

  where
      Sev - is the severity code
          00 - Success
          01 - Informational
          10 - Warning
          11 - Error

      C - is the Customer code flag
      R - is a reserved bit
      Facility - is the facility code
      Code - is the facility's status code
*)


{ Define the facility codes }

const
  FACILITY_WINDOWS = 8;
  {$EXTERNALSYM FACILITY_WINDOWS}
  FACILITY_STORAGE = 3;
  {$EXTERNALSYM FACILITY_STORAGE}
  FACILITY_RPC = 1;
  {$EXTERNALSYM FACILITY_RPC}
  FACILITY_SSPI = 9;
  {$EXTERNALSYM FACILITY_SSPI}
  FACILITY_WIN32 = 7;
  {$EXTERNALSYM FACILITY_WIN32}
  FACILITY_CONTROL = 10;
  {$EXTERNALSYM FACILITY_CONTROL}
  FACILITY_NULL = 0;
  {$EXTERNALSYM FACILITY_NULL}
  FACILITY_INTERNET = 12;
  {$EXTERNALSYM FACILITY_INTERNET}
  FACILITY_ITF = 4;
  {$EXTERNALSYM FACILITY_ITF}
  FACILITY_DISPATCH = 2;
  {$EXTERNALSYM FACILITY_DISPATCH}
  FACILITY_CERT = 11;
  {$EXTERNALSYM FACILITY_CERT}

{ Define the severity codes }

  ERROR_SUCCESS = 0;
  NO_ERROR = 0;

  ERROR_INVALID_FUNCTION = 1;
  ERROR_FILE_NOT_FOUND = 2;
  ERROR_PATH_NOT_FOUND = 3;
  ERROR_TOO_MANY_OPEN_FILES = 4;
  ...
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu (28. Mär 2018 um 17:59 Uhr)
  Mit Zitat antworten Zitat