Link to home
Start Free TrialLog in
Avatar of yaroni188
yaroni188

asked on

tryin to GetErrorMessage(...) from CFileException cause an assertion.

catch(CFileException  *ex)
{
     TCHAR   szCause[255];          
     ex->GetErrorMessage(szCause,255);
        ex->Delete();
}

when the program enter the catch block and try to execute GetErrorMessage then an exception occur which cause a popup message "Debug assertion failed!" to appear.
I am using a console application project with MFC in DLL.

Avatar of Kocil
Kocil

catch(CFileException  ex)
{
    TCHAR   szCause[255];          
    ex.GetErrorMessage(szCause,255);
}
ASKER CERTIFIED SOLUTION
Avatar of Kocil
Kocil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If nothing helps, you might want to try this wrapper for FormatMessage I coded a while ago and pass the error code to it. Not sure if this would solve your problem:

CString GetLastErrorMessage(DWORD dwErr)
{
    try
    {
        LPVOID lpMsgBuf;

        DWORD dwError   =  dwErr ? dwErr : GetLastError();

        DWORD dwFlags   =  FORMAT_MESSAGE_ALLOCATE_BUFFER    |
                           FORMAT_MESSAGE_FROM_SYSTEM        |
                           FORMAT_MESSAGE_IGNORE_INSERTS;

        DWORD dwLang    =  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT);

        DWORD dwBytes   =  FormatMessage(dwFlags,NULL,dwError,dwLang,
                             (LPTSTR) &lpMsgBuf, 0, NULL);

        if (dwBytes == 0) {
            return CString("Unknown");
        }

        CString sMessage((char*)lpMsgBuf);
        LocalFree(lpMsgBuf);
        return sMessage;
    }

    catch (...)
    {
        return CString("Unknown");
    }
}


And then:

catch(CFileException *e)
{
    CString err = GetLastErrorMessage(e->m_lOsError);
}


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Kocil

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer