windows “wsprintfW”:无法将参数 1 从“char [80]”转换为“LPWSTR”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4703405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:59:17  来源:igfitidea点击:

'wsprintfW' : cannot convert parameter 1 from 'char [80]' to 'LPWSTR'

c++windowsvisual-studiovisual-studio-2010visual-c++

提问by xRobot

I have created this simple windows application:

我创建了这个简单的 Windows 应用程序:

#include <windows.h>
#include <stdio.h>


LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     int idmes = MessageBox (NULL, TEXT ("Hello!"), TEXT ("HelloMsg"), MB_ABORTRETRYIGNORE) ;

     static TCHAR szAppName[] = TEXT ("HelloWin") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) (COLOR_BACKGROUND+11) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }

     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("Programma di emiliano"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}




LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;
     static short xChar, yChar ;
     TEXTMETRIC   tm ;
     char         szBuffer[80];
     int          i ;


     switch (message)
     {
     case WM_CREATE:
          MessageBox (NULL, TEXT ("Ciao caro!"), TEXT ("Salve bellezza"), 0) ;
          hdc = GetDC (hwnd) ;
          GetTextMetrics (hdc, &tm) ;
          xChar = tm.tmAveCharWidth ;
          yChar = tm.tmHeight + tm.tmExternalLeading ;
          ReleaseDC (hwnd, hdc) ;
          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
      int iLength;

          for (i = 0 ; i < 10 ; i++)
            { iLength = wsprintf( szBuffer, "The number is %d.", i );
              TextOut (ps.hdc, xChar, yChar * (1 + i), szBuffer, 5) ;
             }
          EndPaint (hwnd, &ps) ;
          return 0 ;

     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

but I get this error:

但我收到此错误:

------ Build started: Project: primo, Configuration: Debug Win32 ------
Build started 16/01/2011 03:10:02.
InitializeBuildStatus:
  Creating "Debug\primo.unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  All outputs are up-to-date.
  primo.cpp
c:\users\larry\documents\visual studio 2010\projects\primo\primo\primo.cpp(79): warning C4244: '=' : conversion from 'LONG' to 'short', possible loss of data
c:\users\larry\documents\visual studio 2010\projects\primo\primo\primo.cpp(80): warning C4244: '=' : conversion from 'LONG' to 'short', possible loss of data
c:\users\larry\documents\visual studio 2010\projects\primo\primo\primo.cpp(90): error C2664: 'wsprintfW' : cannot convert parameter 1 from 'char [80]' to 'LPWSTR'
          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Build FAILED.

Time Elapsed 00:00:02.34
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Why ?

为什么 ?

回答by cbranch

Change your code as follows:

更改您的代码如下:

TCHAR szBuffer[80];
...
iLength = wsprintf( szBuffer, TEXT ("The number is %d."), i );

回答by Remus Rusanu

Be consistent with your ANSI/Unicode strings. You use TEXTand TCHARon most palces, but you then mix in hard coded Unicode wsprintfand hard coded ANSI char szBuffer[80];. I could tell you that you can simple change the declaration to WCHAR szBuffer[80];but that is not true.

与您的 ANSI/Unicode 字符串保持一致。您在大多数地方使用TEXTTCHAR,但随后将硬编码的 Unicodewsprintf和硬编码的 ANSI混合在一起char szBuffer[80];。我可以告诉你,你可以简单地将声明更改为,WCHAR szBuffer[80];但事实并非如此。

First and foremost, you need to be correct. Second, you need to be consistent.

首先,你需要是正确的。其次,你需要保持一致。

For correctness: wsprintftakes Unicode strings as first two arguments. No buts, no ifs. You pass in two ANSI strings, hence the compiler error:

为了正确性:wsprintf将 Unicode 字符串作为前两个参数。没有但是,没有如果。您传入两个 ANSI 字符串,因此编译器错误:

WCHAR wszBuffer[80];
...
wsprintf(wszBuffer, L"The number is %d", ...);

But you also need to be consistent. If you use TEXTand TCHAR, stick to _Tflavour of strings and functions everywhere. If you abandon the _T(which you should, there is no reason to compile ANSI code in now days), then remove it everywhere.

但你也需要保持一致。如果你使用TEXTTCHAR,坚持_T字符串和函数的香味无处不在。如果你放弃了_T(你应该放弃,现在没有理由编译 ANSI 代码),那么就到处删除它。

回答by JCV-MEX

I had the same problem. The answers above are missing the fact that the error is on the first parameter: szbuffer, and not on the string constant "The number is %id", which is the second parameter.

我有同样的问题。上面的答案忽略了这样一个事实,即错误出在第一个参数:szbuffer 上,而不是出在第二个参数字符串常量“The number is %id”上。

I solved the problem by simply casting the first parameter as:

我通过简单地将第一个参数转换为:

   (LPWSTR)szbuffer

It stopped the error the compiler was giving me.

它停止了编译器给我的错误。

回答by Edward Strange

First, you should limit the amount of code you post to that amount that is not working when you post questions. A great portion of what you posted is superfluous to the problem.

首先,您应该将发布的代码数量限制在发布问题时不起作用的数量。您发布的大部分内容对问题来说都是多余的。

Second, the answer to your problem is you need to use the 'L' prefix for wide character strings, which wsprintf expects (hense the w). So, "The number is %d."should read instead L"The number is %d.". That will fix your current problem.

其次,您的问题的答案是您需要对宽字符串使用“L”前缀,这是 wsprintf 期望的(hense w)。所以,"The number is %d."应该改为阅读L"The number is %d."。这将解决您当前的问题。