获取上一个错误的宽字符版本?

时间:2016-02-03 13:08:03

标签: c++ error-handling exit widechar

这是微软的code

void ErrorExit(LPCWSTR lpszFunction, DWORD NTStatusMessage)
{
    // Retrieve the system error message for the last-error code
    DWORD dww = 0;
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;

    if (NTStatusMessage)
    {
        dww = NTStatusMessage;
        FormatMessageW( 
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM | 
        FORMAT_MESSAGE_FROM_HMODULE,
        hdlNtCreateFile,
        dww,  
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPWSTR) &lpMsgBuf,  
        0,  
        NULL );
    }
    else
    {

        dww = GetLastError();

    FormatMessageW(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dww,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),  (LPWSTR)&lpMsgBuf,0, NULL);
    }
    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));


    StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %lu: %s"), lpszFunction, dww, lpMsgBuf);
    printf("\a");  //audible bell not working yet
    MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, L"Error", MB_OK);
    LocalFree(lpDisplayBuf);

    LocalFree(lpMsgBuf);

    //ExitProcess(dw);
}

我为NTstatus添加了一个部分,并将arg更改为LPCWSTR。它基本上没有功能,只需更改为LPVOID类型的StringCchPrintfW段错误。什么方式使它广泛友好?

1 个答案:

答案 0 :(得分:0)

通过将 C / C ++中的预处理器定义>来解决此问题。预处理器。
OP代码中的 MessageBox 调用调整了一点:感谢Microsoft论坛上的someone knowledgeable