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

Wide Char Version of Get last Error?

这是微软的 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 段错误。有什么方法可以让它变得更友好?

通过在 C/C++ > Preprocessor.
中的预处理器定义中添加 _UNICODE 和 UNICODE 解决了这个问题 OP 代码中的 MessageBox 调用稍作调整:感谢 Microsoft 论坛上的 someone knowledgeable