|
Prev: DLL export
Next: COM message pump
From: June Lee on 26 Mar 2008 21:37 is HRESULT only used in COM interface programming? I see this quite often in a COM module call.
From: Tom Serface on 26 Mar 2008 21:39 I've seen it mostly used with COM and Windows message calls, but I think HRESULT is just a long that is encode with attributes so you could likely use it wherever. Here are some interesting articles: http://en.wikipedia.org/wiki/HRESULT http://msdn2.microsoft.com/en-us/library/dcy94zz2(VS.80).aspx#vcconattributeprogrammmingfaqanchor1 Tom "June Lee" <iiuu66(a)yahoo.com> wrote in message news:4gulu3prunl23jd6fvvihal7ps73kef8j6(a)4ax.com... > is HRESULT only used in COM interface programming? > > I see this quite often in a COM module call.
From: Ajay Kalra on 26 Mar 2008 22:40 "June Lee" <iiuu66(a)yahoo.com> wrote in message news:4gulu3prunl23jd6fvvihal7ps73kef8j6(a)4ax.com... > is HRESULT only used in COM interface programming? > > I see this quite often in a COM module call. I have only seen it used in COM related method calls. IIRC, it was introduced at the same time when COM came out. --- Ajay
From: Joseph M. Newcomer on 26 Mar 2008 22:44 It was defined for the purposes of COM programming, but it is just a 32-bit value. You are free to use it for your own purposes if you return values which are compatible with the COM philosophy. joe On Wed, 26 Mar 2008 17:37:16 -0800, June Lee <iiuu66(a)yahoo.com> wrote: >is HRESULT only used in COM interface programming? > >I see this quite often in a COM module call. Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Giovanni Dicanio on 27 Mar 2008 10:57
"June Lee" <iiuu66(a)yahoo.com> ha scritto nel messaggio news:4gulu3prunl23jd6fvvihal7ps73kef8j6(a)4ax.com... > is HRESULT only used in COM interface programming? > > I see this quite often in a COM module call. To add to what others correctly wrote, I would like to suggest you to use HRESULT only in COM code or ATL development (e.g. if you are developing a COM component). One of COM requirements was that it must support several languages, including C. And C has no exception mechanism for error handling (instead, you can use exceptions in C++). So, to be compatible also with C, in COM every error condition is signaled returning HRESULT. This may lead to some problems in "quality" of code, for example, in typical COM code, you may have: <code> HRESULT SomeFunction(...) { // erorr code HRESULT hr; ... hr = pX->DoSomething(...); if ( FAILED(hr) ) ... cleanup return hr; hr = pY->DoSomethingElse(...); if ( FAILED(hr) ) ... cleanup return hr; ... ... // Everything OK return S_OK; } </code> The problem is duplicating "cleanup". To avoid duplicating cleanup code in each "if ( FAILED(hr)) ..." branch, 'goto' statements may be used, e.g.: <code> hr = pX->DoSomething(...); if ( FAILED(hr) ) goto ErrorCleanup; hr = pY->DoSomethingElse(...); if ( FAILED(hr) ) goto ErrorCleanup; ... // All right return S_OK; // *** CLEANUP [on error] *** ErrorCleanup: // Do cleanup here... ... // Return error code return hr; </code> The above code could be simplified if some quality tools are used, like smart pointers (like CComPtr COM smart pointer template; in fact, smart pointers do proper cleanup, like calling IUnknown::Release, when the smart pointer goes out of scope; so, in that case, returning an error code could be fine, without explicit cleanup: the smart pointer will do the cleanup automatically). However, if you are not targeting COM interface programming, and e.g. you are doing "pure" C++ (or even MFC programming), I would suggest you to *not* use HRESULTs. At least, I would define custom enum for error codes, these are more readable than HRESULTs, IMHO. e.g. <code> class Something { public: enum ErrorCode { Ok = 0, // All right MemoryFailure, CantOpenFile, ... }; // Better custom error code enum than HRESULT ErrorCode DoSomething(...); }; </code> Or you may also consider using C++ exceptions (and use RAII pattern, to develop exception-safe code). Giovanni |