0

When I failed executing a DirectDraw method, how to get the failure error string in DirectX 7?

if (FAILED(lpddPrimarySurface->SetPalette(lpddPalette)))
{
    MessageBox(NULL, **"I want to get the failure string here."**, "Error", MB_OK);
    return 0;
}

Here I want to pop up the error messge of the failure information. How to get the LPCSTR string of the error?

Maple Wan
  • 61
  • 1
  • 8
  • You're setting a palette? Are you trying to target 8-bit (256) color modes? That might not be supported on newer hardware. – selbie Aug 30 '13 at 17:14
  • Yeah, I always SetPalette failed. What should I use while I can't use 8-bit(256) color mode? Could you provide some details, thanks. – Maple Wan Aug 31 '13 at 18:02
  • You never said what error code SetPalette is returning. It's entirely possible you palette object is mis-initialized. I was just commenting that it seems entirely odd to use an 8-bit display mode in 2013. I don't know of a single graphic card manufactured in the last 10 years that doesn't support 24-bit modes (and hence, no palette is needed). – selbie Aug 31 '13 at 19:13
  • @selbie: unless you want to do a palette rotation effect or some palette mappings in an image. – SasQ Mar 28 '14 at 14:58

2 Answers2

1

For NTSTATUS errors the following is possible. Not sure if this will work for Direct Draw and Direct X HRESULT error codes, but it may do as they may be in the system message table. You don't need the ntdll handle either I don't think because the look up is done on the system message table. I have specified it just in case as I have never tested without it.

Excuse the static char array, this just to show example, not a good implementation :)

static const char *NTStatusToString(DWORD NtStatusCode)
{
    LPVOID lpMessageBuffer = 0;
    HMODULE hNTDll = GetModuleHandle("ntdll.dll");
    static char szBuffer[256];

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_FROM_HMODULE,
        hNTDll,
        NtStatusCode,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMessageBuffer,
        0,
        NULL);

    memset(szBuffer, 0, sizeof(szBuffer));
    _snprintf(szBuffer, sizeof(szBuffer)-1, "%s", lpMessageBuffer);

    LocalFree(lpMessageBuffer);

    return szBuffer;
}

This thread suggests it will work How should I use FormatMessage() properly in C++? however this one suggests it won't Is there a way to get the string representation of HRESULT value using win API? and you will have to do a bit more work than this.

Further reading: here and here and here

Community
  • 1
  • 1
djgandy
  • 1,125
  • 6
  • 15
0

There is no error string provided by DirectDraw. You have to look at the returned HRESULT and format your own string as needed. For example:

http://www.gamedev.net/topic/8268-ddraw-error-code/

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770