We help IT Professionals succeed at work.

What is the problem with this look up error code - under ATL- ?

MohammadA
MohammadA asked
on
I have a function check the HRESULT value declared like this:
void CClass::CheckError(HRESULT hr)
{
     if(hr == S_OK)
       return;
     if(HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
          hr = HRESULT_CODE(hr);
     LPVOID lpMsgBuf = NULL;
     ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
               FORMAT_MESSAGE_FROM_STRING|
               FORMAT_MESSAGE_IGNORE_INSERTS,
               NULL,
               hr,
               MAKELANGID   (LANG_NEUTRAL,SUBLANG_DEFAULT),
               (OLECHAR *) &lpMsgBuf,
               0,
               NULL);

     LPTSTR  buf;
     wsprintf(buf,(LPCTSTR)"%s\nhr = 0x%lx",(LPCTSTR)lpMsgBuf, hr);
     MessageBox(NULL,buf,(LPCTSTR)"Error",MB_OK);
}

when i debugg this function,there is a message comes up after execute the ::FormatMessage(..) function said:
-----------------------------------------------
"CClass interface Error: Method '~' of object '~' faild."
-------------------------------------------------
I use this function under in-proccess component.


Can anybody tell me what is the problem??
Regards.
Comment
Watch Question

Commented:
~ is destructor i think...isn't it...
do u have one....like following example?

__fastcall CClass::~CClass(void)
{
// Destuctor
}

Author

Commented:
No I do not have,but if I have I do not show any relation ship between destructor and the error above.
This error often appears when some component actually crashes or does not supply proper error information.

You need to look at the component that is generating the error to see what it is doing.
but error has been raised after the ::FormatMessage(..) excuted!!!!!
Where exactly?  Are you saying that there is a reasonable message in the buffer after you execute FormatMessage?
when i debugg this function,there is a message comes up after execute the ::FormatMessage(..) function
said:
-----------------------------------------------
"CClass interface Error: Method '~' of object '~' faild."
-------------------------------------------------

The confusing thing is that this is what this code is *supposed* to do, i.e. it formats and displays an error message.

Is what you are saying that the error message appears before the MessageBox statement is reached?

Looking again, I suspect you have the wrong parameters to the method, try something like this:


::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                hr,
                MAKELANGID   (LANG_NEUTRAL,SUBLANG_DEFAULT),
                (OLECHAR *) &lpMsgBuf,
                0,
                NULL);

i.e. if you are passing a HRESULT as the source, you need to get the message from the system, specifying it is string means that the method things it is looking for a null terminated string in the source parameter.
I agree with jasonclarke that your problem is specifying FORMAT_MESSAGE_FROM_STRING.  However, I see another problem you're going to want to watch out for.  After the FormatMessage call you have:

    LPTSTR  buf;
    wsprintf(buf,(LPCTSTR)"%s\nhr = 0x%lx",(LPCTSTR)lpMsgBuf, hr);
    MessageBox(NULL,buf,(LPCTSTR)"Error",MB_OK);

Won't this crash?  'buf' is going to be pointing to some random memory location that you're immediately going to try to write to!

Good luck!
> Won't this crash?

almost certainly it will, I missed this one.  You need to allocate some memory for this buffer.

Author

Commented:
Ok,you r always right I have change FORMAT_MESSAGE_FROM_STRING and I allocate a memory for 'buf' and the above problem disapeared but the message the i have got is nothing just a rubish code.?????!!!!!

Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jasonclarke, The Master" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Per recommendation, force-accepted by
Netminder
CS Moderator

The_Master: points for you at http://experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20282350

Explore More ContentExplore courses, solutions, and other research materials related to this topic.