来自Charles Petzold的纯C中的MsgBoxPrintf(win32 api)给出了中文输出...我做错了什么?

时间:2022-01-02 20:12:16

VS2013 Community edidtion OS:Win7 sp1

VS2013社区edidtion操作系统:Win7 sp1

#include<Windows.h>
#include<stdio.h>


    //VS2013 Community edidtion OS:Win7 sp1 

    //Using wchar_t for unicode and L"  " for strings

    //The MessageBoxPrintf from the book modified to take wide chars

    int CDECL MsgBoxPrintf(wchar_t *szCaption,const wchar_t *szFormat, ...)
    {
        wchar_t szBuffer[1024];
        va_list pArgsList;
        va_start(pArgsList, szFormat);

     //Using _vsnwprintf_s since _vsntprintf is deprecated   
        _vsnwprintf_s(szBuffer, sizeof(szBuffer)/sizeof(wchar_t), 1024-1, szBuffer, pArgsList);
        va_end(pArgsList);
    //Using MessageBoxW instead of MessageBox
        return MessageBoxW(0, szBuffer, szCaption, 0);
    }

    int
    WINAPI
    WinMain(HINSTANCE hInstance,
            HINSTANCE hPrevInstance,
            PSTR szCmdLine,
            int iCmdShow)
    {
        int cxScreen, cyScreen;
        cxScreen = GetSystemMetrics(SM_CXSCREEN);
        cyScreen = GetSystemMetrics(SM_CYSCREEN);
        MsgBoxPrintf(L"ScreenSize",L"The Screen is %i Pixels Width and %i Pixels Height.***Resolution(%ix%i)***",
            cxScreen, cyScreen, cxScreen, cyScreen
        );


        return(0);

    }

Everything works fine except the character ouput which is all the same chinese? symbol.No matter which kind of printf i use i cant get a correct result. What have i done wrong?

一切都运行正常,除了字符输出是完全相同的中文? symbol。无论我使用哪种printf我都无法得到正确的结果。我做错了什么?

1 个解决方案

#1


5  

Your next-to-last argument for _vsnwprintf_sis wrong.

_vsnwprintf_sis的倒数第二个参数错误。

_vsnwprintf_s(szBuffer, sizeof(szBuffer)/sizeof(wchar_t), 1024-1, szBuffer, pArgsList);
//                                                       this ======^

should be szFormat; not szBuffer

应该是szFormat;不是szBuffer

#1


5  

Your next-to-last argument for _vsnwprintf_sis wrong.

_vsnwprintf_sis的倒数第二个参数错误。

_vsnwprintf_s(szBuffer, sizeof(szBuffer)/sizeof(wchar_t), 1024-1, szBuffer, pArgsList);
//                                                       this ======^

should be szFormat; not szBuffer

应该是szFormat;不是szBuffer