Les Snippets

Connexion

Obtenir le message d'erreur d'une fonction Win32

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 09/04/2007 00:09:47 et initié par Lutinore [Liste]
Date de mise à jour : 26/10/2007 14:22:16
Vue : 4739
Catégorie(s) : API
Langages dispo pour ce code :
- C# 1.x, C# 2.x
- VB 2005, VB.NET 1.x
- Voir tous les langages pour ce code snippet



Langage : C# 1.x , C# 2.x
Date ajout : 09/04/2007
Posté par Lutinore [Liste]

// using System.Runtime.InteropServices;
[ DllImport( "kernel32.dll", CharSet = CharSet.Auto/*, SetLastError = true */ ) ]
private static extern uint FormatMessage
(
    uint dwFlags,
    IntPtr lpSource,
    uint dwMessageId,
    uint dwLanguageId,
    ref IntPtr lpBuffer,
    uint nSize,
    IntPtr Arguments
);

public string GetLastWin32ErrorMessage( )
{
    return GetLastWin32ErrorMessage( Marshal.GetLastWin32Error( ) );
}

public string GetLastWin32ErrorMessage( int errorCode )
{
    IntPtr buffer = IntPtr.Zero;

    uint cnt = FormatMessage
    (
        0x00000100 |    // FORMAT_MESSAGE_ALLOCATE_BUFFER
        0x00000200 |    // FORMAT_MESSAGE_IGNORE_INSERTS
        0x00001000,     // FORMAT_MESSAGE_FROM_SYSTEM
        IntPtr.Zero,
        ( uint )errorCode,
        0,              // LANGIDs : Neutral, Thread, User, System, en-US
        ref buffer,
        0,
        IntPtr.Zero
    );

    if ( cnt == 0 || buffer == IntPtr.Zero )
        return String.Format( "Unknown error: {0}.", errorCode );

    string errorMessage = Marshal.PtrToStringAuto( buffer, ( int )cnt );
    Marshal.FreeHGlobal( buffer ); // LocalFree.

    return errorMessage;
}

Remarque :
Méthode alternative mais pas très "élégante" :

string errorMessage = new Win32Exception( Marshal.GetLastWin32Error( ) ).Message;



Codes sources en rapport avec : Erreur, Getlasterror, Formatmessage, Getlastwin32error

{C / C++ / C++.NET} EXPLICATIF D'ERREURS : EXPLICITELASTERROR() <= GETLASTERROR
Cette fonction donne l'explication de la derniere erreur levée récupérée avec GetLastError Elle...

{Visual Basic, VB6, VB.NET, VB 2005} COMBOBOX FAÇON ERRORPROVIDER
Ce que je reproche au contrôle ErrorProvider, dans le cas d'un formulaire contenant des onglets, c'e...

{PHP} TRAITER LES ERREURS PHP GRAVES, NON INTERCEPTABLES
Comment traiter les erreurs PHP qui ne peuvent être interceptées et gérées par script PHP? Impossibl...

{Delphi} RÉELS ET RÉALITÉ
Soucieux d'assurer un certain confort à mes vieux jours, je suis allé trouver mon banquier. Comme je...

{PHP} [PHP5] EXCEPTIONERROR PACKAGE : TRANSFORMER TOUTES LES ERREURS PHP EN EXCEPTIONS INTERCEPTABLES
Ce package permet de transformer toutes les erreurs PHP en exceptions interceptables. En clair, sur...

{Delphi} SIGNIFICATION DES CODES D'ERREUR DE L'API WIN32 - UTILISATION D'UNE TLISTVIEW
Les différents messages d'erreur Win32 sont présentés dans une ListView avec tri croissant/décroissa...

{JAVA / J2EE} DKSERRORFRAMEWORK : UN PETIT ENSEMBLE DE CLASSE POUR AUTOMATISER LES MESSAGES D'ERREUR
Je vous propose une toute petite source qui vous permet d'automatiser les messages d'erreurs. Rie...

{PHP} TRACE DES ERREURS EN PHP
Bonjour, Ci-joint un exemple permettant de voir un trace des fonctions qui ont abouti à une erreur...

{Javascript / DHTML} GESTION DES MESSAGES D'ERREUR 404 PERSONALISÉS ET PAGE DE MAINTENANCE
Je vous présente 2 petites astuces pour gérer les erreurs de type "ERREUR 404", ... et aussi comment...

{Delphi} LA LISTE DES ERREURS DU BDE
salut Voila un example qui affiche une liste complete des erreurs du BDE. je cherche longtemps pou...