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

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

为什么?

最佳答案

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

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

关于c++ - 'wsprintfW' : cannot convert parameter 1 from 'char [80]' to 'LPWSTR' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4703405/

相关文章:

c++ - 嵌入式STL智能指针

c++ - 无法使用 C++ 中的 Windows 服务检索可移动设备上的数据

php - 使用 tcp 套接字将数据从 php 发送到 c++

IIS 上的 PHP-CGI.exe 崩溃

c++ - 如何读取当前连接的显示器 ID?

c++ - C++/CX 中 ^ 变量的含义

windows - TShark 输出重定向

c++ - #define USE_SQLDB (定义(USE_SQLITE) || 定义(USE_MYSQL))

mysql - Select where 子句 = label.text 为什么不起作用

visual-studio - 如何将环境变量添加到 VS 解决方案 (.sln)