 |
|
 |
Hello Ashutosh
Can you tell me how can I convert my own error codes into HRESULT so that I can return it from my methods. Suppose DoValidation is my method which returns HRESULT. If the validation fails, I want to return ERROR_INVALIDMEMBER which is defined as #define ERROR_INVALIDMEMBER 1000. My ultimate aim is to let the user know about the description of error instead of returing failure or success.
Any help would be greatly appreciated.
Ajay Sonawane
Webtech Developers Pvt Ltd,Pune ( India )
|
|
|
|
 |
|
 |
HRESULT structure basically contains information about Severity, Context, Facility & Error code. Following URL contains complete information about the HRESULT structure for 16 bit and 32 bit platforms http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/error_5g6r.asp. To create our own HRESULT we need to set proper bits in this as per the specification. One can put the error information into this structure to create our own HRESULT.
Let me know if you have any further questions.
|
|
|
|
 |
|
 |
Thats what I wanted to know. How can one put informatio into this structure to create our own HRESULT. Is there any function to do so ? AtlReportError does something like that. Can u please elaborate on your views ?
How one can return its own error code so that we can see the returm error codes at client side ?
|
|
|
|
 |
|
 |
Thats what I wanted to know. How can one put informatio into this structure to create our own HRESULT. Is there any function to do so ? AtlReportError does something like that. Can u please elaborate on your views ?
How one can return its own error code so that we can see the returm error codes at client side ?
Ajay
|
|
|
|
 |
|
 |
yes U can use
MAKE_HRESULT macro
HRESULT strct is like this
total 32 bits
30-31 Severity->sucess or failed
15-30 FACILITY DATA
0-15 RETURN VALUE
------------------------------
Its not the fall that kills you; it's the
sudden stop at the end.
|
|
|
|
 |
|
 |
There is an ATL method entitled AtlReportError which sets up the IErrorInfo interface to provide error information to clients of the object (virtually identical to what this code is doing).
According to the MSDN, it has the following forms:
- HRESULT WINAPI AtlReportError(const CLSID& clsid, LPCOLESTR lpszDesc, const IID& iid = GUID_NULL, HRESULT hRes = 0);
- HRESULT WINAPI AtlReportError(const CLSID& clsid, LPCOLESTR lpszDesc, DWORD dwHelpID, LPCOLESTR lpszHelpFile, const IID& iid = GUID_NULL, HRESULT hRes = 0);
- HRESULT WINAPI AtlReportError(const CLSID& clsid, LPCSTR lpszDesc, const IID& iid = GUID_NULL, HRESULT hRes = 0);
- HRESULT WINAPI AtlReportError(const CLSID& clsid, LPCSTR lpszDesc, DWORD dwHelpID, LPCSTR lpszHelpFile, const IID& iid = GUID_NULL, HRESULT hRes = 0);
- HRESULT WINAPI AtlReportError(const CLSID& clsid, UINT nID, const IID& iid = GUID_NULL, HRESULT hRes = 0, HINSTANCE hInst = _AtlBaseModule.GetResourceInstance());
- HRESULT WINAPI AtlReportError(const CLSID& clsid, UINT nID, DWORD dwHelpID, LPCOLESTR lpszHelpFile, const IID& iid = GUID_NULL, HRESULT hRes = 0, HINSTANCE hInst = _AtlBaseModule.GetResourceInstance());
Parameters:
- clsid [in] The CLSID of the object reporting the error.
- lpszDesc [in] The string describing the error. The Unicode versions specify that lpszDesc is of type LPCOLESTR; the ANSI version specifies a type of LPCSTR.
- iid [in] The IID of the interface defining the error or GUID_NULL if the error is defined by the operating system.
- hRes [in] The HRESULT you want returned to the caller.
- nID [in] The resource identifier where the error description string is stored. This value should lie between 0x0200 and 0xFFFF, inclusively. In debug builds, an ASSERT will result if nID does not index a valid string. In release builds, the error description string will be set to "Unknown Error."
- dwHelpID [in] The help context identifier for the error.
- lpszHelpFile [in] The path and name of the help file describing the error.
- hInst [in] The handle to the resource. By default, this parameter is __AtlBaseModuleModule::GetResourceInstance, where __AtlBaseModuleModule is the global instance of CAtlBaseModule or a class derived from it.
In its simplest form it could be called in a manner such as:
if (succeeded)
return S_OK;
else
return AtlReportError(GetObjectCLSID(), "My error message");
For more information on this, you can visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_atl_atlreporterror.asp
--
Matt Berther
matt@mattberther.com
|
|
|
|
 |
|
 |
Hi Matt,
Thanks a lot for reminding me about this function, but I was probably aiming at the internals of this one. Secondly, although this function has got a lot of overloaded forms, none of the forms takes TCHAR strings as parameters, which is why I went ahead and wrote my own version, because I try to make use of LPTSTRs in place of LPSTRs wherever possible. Thirdly, my very first aim of presenting the code was to tell the users about the proper usage of ATL constructs and macros too, for which this function gave me good opportunity.
All said, I would like to thank you again for adding to my knowledge, as I was never aware that AtlReportError() had so many overloaded forms. Maybe it's time I should start reading the stuff by paying a little more attention
Thanks.
|
|
|
|
 |
|
 |
Ashutosh Arya wrote:
Secondly, although this function has got a lot of overloaded forms, none of the forms takes TCHAR strings as parameters, which is why I went ahead and wrote my own version, because I try to make use of LPTSTRs in place of LPSTRs wherever possible.
Ashutosh,
You can always use the _T() macro for TCHAR data type mapping. For example:
AtlReportError(GetObjectCLSID(), _T("My Error Message"));
I appreciate your article, as I was able to finally understand exactly how this is happening and how a person could accomplish this without using the macro.
--
Matt Berther
matt@mattberther.com
|
|
|
|
 |
|
 |
Thanks a lot this satisfies my concern...
|
|
|
|
 |
|
 |
You can actually call the Method Error (which calls AtlReportError)
Error is a method of CComCoClass
e.g.
return Error(_bstr_t(m_sErrorDescription).copy(), IID_IObj, hr);
|
|
|
|
 |
|
 |
Man! I never expected that this could be done in so many intuitive ways. Thanks a lot for posting this method. I think the readers now have got lot of choices.
Keep mailing guys!!!
|
|
|
|
 |
|
 |
return Error(_bstr_t(m_sErrorDescription).copy(), IID_IObj, hr);
I think that line of code results in a memory leak... remember that
bstr_t's .copy() allocates memory, and this instance it never gets
released. Just a reminder! (My 2 cent's worth)
~Ryno
|
|
|
|
 |
|
 |
Hi Matt,
I don't know why but I'm using Visual Studio 6 and the method AtlReportError doesn't have these forms. There are not parameters by default. So I have to define an HRESULT in the parameters. My Problem is that in the client code (VBA) the description is not the one I've defined in the parameters but the HRESULT's one !
An explanation please ... ?
Kamel
|
|
|
|
 |