首页 文章

类型为lpcwstr的char *参数的参数

提问于
浏览
-4

我收到错误“类型为char * *类型lpcwstr的参数”在c怎么修复?

char text[MAX_PATH]= {};
sprintf(text, "Number of Words: %S", computerName);
sprintf(text, "Number of Sentences: %S", userName);
sprintf(text, "Number of Digits: %d", objSystemInfo.dwNumberOfProcessors);
sprintf(text, "Number of Upper Case: %d", bit);
MessageBox(NULL, text , L"Sistem Bilgisi", MB_OK);

3 回答

  • 1

    MessageBox接收LPCWSTR,你可以将它表示为wchar_t而不是char,并使用wsprintf写入wchar_t,如下所示:

    wchar_t text[MAX_PATH]= {};
    wsprintf(text, L"Number of Words: %s", computerName);
    wsprintf(text, L"Number of Sentences: %s", userName);
    wsprintf(text, L"Number of Digits: %d", objSystemInfo.dwNumberOfProcessors);
    wsprintf(text, L"Number of Upper Case: %d", bit);
    MessageBox(NULL, text , L"Sistem Bilgisi", MB_OK);
    
  • 0

    请注意,LPCWSTR在winnt标头中定义为: typedef const WCHAR* LPCWSTR ,表示指向const wide char的指针 . 在您的情况下,文本是char表 . 如果您的项目使用UNICODE,则MessageBox的第二个参数必须是LPCWSTR . 因此,如果您尝试通过 wchar_t text[MAX_PATH]= {}; 更改 char text[MAX_PATH]= {}; 的声明,它将解决您的编译问题 . 但是,您的MessageBox将显示空文本消息,如注释中所述,sprintf不会向您的文本变量添加文本 .

  • 0

    最简单的解决方案是调用 MessageBoxA()

    MessageBoxA(NULL, text, "Sistem Bilgisi", MB_OK);
    

    否则,如果继续调用 TCHARTCHAR 版本,则需要更新代码以使用 TCHAR 作为文本:

    TCHAR text[MAX_PATH] = {};
    _stprintf(text,
      _T("Computer Name: %ls\nUserName: %ls\nNumber of Processors: %u\nBit: %d"),
      computerName,
      userName,
      objSystemInfo.dwNumberOfProcessors,
      bit
    );
    MessageBox(NULL, text, TEXT("Sistem Bilgisi"), MB_OK);
    

    否则,请调用 MessageBox()WCHAR 版本并使用 WCHAR 作为您的文字:

    WCHAR text[MAX_PATH] = {};
    swprintf(text,
      L"Computer Name: %ls\nUserName: %ls\nNumber of Processors: %u\nBit: %d",
      computerName,
      userName,
      objSystemInfo.dwNumberOfProcessors,
      bit
    );
    MessageBoxW(NULL, text, L"Sistem Bilgisi", MB_OK);
    

相关问题