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

时间:2011-01-16 02:15:49

标签: c++ windows visual-studio visual-studio-2010 visual-c++

我创建了这个简单的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) ;
}

但我收到此错误:

------ 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 ==========

为什么?

4 个答案:

答案 0 :(得分:4)

按如下方式更改您的代码:

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

答案 1 :(得分:3)

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

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

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

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

但你也需要保持一致。如果您使用TEXTTCHAR,请坚持_T字符串和函数无处不在。如果您放弃了_T(您应该这样做,现在没有理由编译ANSI代码),那么将其删除无处不在

答案 2 :(得分:1)

首先,您应该将发布的代码数量限制为在发布问题时无法使用的数量。你发布的很大一部分都是多余的问题。

其次,你的问题的答案是你需要使用'L'前缀来表示宽字符串,wsprintf期望(hense w)。因此,"The number is %d."应该改为L"The number is %d."。这将解决您当前的问题。

答案 3 :(得分:1)

我遇到了同样的问题。上面的答案错过了错误在第一个参数:szbuffer上的事实,而不是字符串常量“数字是%id”,这是第二个参数。

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

来解决问题
   (LPWSTR)szbuffer

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