Click here to Skip to main content
Licence 
First Posted 14 Jun 2002
Views 46,861
Bookmarked 26 times

Easy handling number of HRESULTs

By | 14 Jun 2002 | Article
Handling HRESULT return values with TRY_HRESULT/CATCH_HRESULT macros

Introduction

Handling HRESULT return values is always hard job for creative programmers. I found a solution which uses exceptions mechanism and all HRESULTs can be handled at once. It's simple - override the operator = for function calls and use it for error handling.

TRY/CATCH HRESULT macros

void OutputHresult(HRESULT hr, UINT nTextID);
#define TRY_HRESULT(hr) HRESULT_EX hr; try {
#define CATCH_HRESULT(hr, nTextID) } catch(HRESULT hr) 
    { if(hr != S_OK) OutputHresult(hr, nTextID); }

struct HRESULT_EX
{
    void operator = (const HRESULT& hr)
    { 
        if(FAILED(hr))
            throw hr;
    }
};

void OutputHresult(HRESULT hr, UINT nTextID)
{
    CString sText = (LPCSTR)nTextID;
    LPTSTR szError;
    if(HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
        hr = HRESULT_CODE(hr);
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
        (LPTSTR)&szError, 0, NULL);
    CHAR s[MAX_PATH];
    sprintf(s, "%s\n\nError number 0x%x\n%s", 
        sText, hr, szError);
    MessageBox(NULL, s, "Error", 
        MB_ICONWARNING);
    LocalFree(szError);
}

Standard HRESULT return values handling

#include <afxoledb.h>

HRESULT OpenDatabase()
{
    HRESULT hr;

    CDataSource db;
    CSession session;

    CDBPropSet dbinit(DBPROPSET_DBINIT);
    dbinit.AddProperty(
        DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
    dbinit.AddProperty(DBPROP_INIT_DATASOURCE,
        "MyDatabase");
    dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
    dbinit.AddProperty(DBPROP_INIT_LCID, (long)1029);

    hr = db.OpenWithServiceComponents("MSDASQL.1",
        &dbinit);
    if(FAILED(hr)) // check for errors
        return hr;

    hr = session.Open(db);
    if(FAILED(hr)) // check for errors
        return hr;

    return hr;
}

HRESULT handling with TRY_HRESULT/CATCH_HRESULT macros

bool OpenDatabase2()
{
    bool bRetVal = false;

    // --- begin of HRESULT checking ---

    TRY_HRESULT(hr)                          
        // try statement, HRESULT_EX definition

        CDataSource db;
    CSession session;

    CDBPropSet dbinit(DBPROPSET_DBINIT);
    dbinit.AddProperty(
        DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
    dbinit.AddProperty(DBPROP_INIT_DATASOURCE, 
        "MyDatabase");
    dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
    dbinit.AddProperty(DBPROP_INIT_LCID, (long)1029);

    hr = db.OpenWithServiceComponents(
        "MSDASQL.1", &dbinit);
    // do not need to check for errors
    hr = session.Open(db);
    // do not need to check for errors

    bRetVal = true; // all HRESULTs succeeded

    CATCH_HRESULT(hr, IDS_ERROR_HR_OLEDB) // catch statement

        // --- end of HRESULT checking ---

        return bRetVal;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Petr Stejskal

Web Developer

Czech Republic Czech Republic

Member

I've been studying economy for 10 years but naturally I'm software developer (and will still be of course Smile | :) .
My computer experience includes C64, Amiga (assembler), currently C++/MFC/ATL on Windows.
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionJust a small addition for HRESULT_EX PinmemberAlexey Savartsov4:01 23 Sep '11  
GeneralThis is so cool ! PinmemberWREY8:04 22 Jun '03  
GeneralRe: This is so cool ! Pinmemberfrugalmail13:11 7 Apr '05  
GeneralMacros TRY_HRESULT and CATCH_HRESULT not a good idea PinmemberRama Krishna3:06 16 Jun '02  
GeneralRe: Macros TRY_HRESULT and CATCH_HRESULT not a good idea PinmemberSteen Krogsgaard2:36 17 Jun '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Jun 2002
Article Copyright 2002 by Petr Stejskal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid