1

Stackflow Members ---- i'm beginner in c++ WINAPI --- when i coding in my first WIN32 GUI, appeared some errors [OUTPUT] --- Are there any recommended books to be advanced in C++ --- my codes:

#include <windows.h>
const char g_szClassName[] = ("myWindowClass");
 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { 
   switch(msg)    {    
   case WM_CLOSE:     
   DestroyWindow(hwnd);    
   break;     
   case WM_DESTROY:  
   PostQuitMessage(0);  
   break;     
   default:    
   return DefWindowProc(hwnd, msg, wParam, lParam);  
   }  
   return 0; 
   }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  
  LPSTR lpCmdLine, int nCmdShow) {  
  WNDCLASSEX wc;   
  HWND hwnd;  
  MSG Msg;
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;  
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;  
    wc.cbWndExtra    = 0;  
    wc.hInstance     = hInstance;  
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);   
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);  
    wc.lpszMenuName  = NULL;  
    wc.lpszClassName = (g_szClassName);  
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    if(!RegisterClassEx(&wc))    {  
    MessageBox(NULL,
        L"Window Registration Failed!",
    L"Error!",     
    MB_ICONEXCLAMATION | MB_OK);  
    return 0;  
    }
    // Creating the Window   
    hwnd = CreateWindowEx(    
    WS_EX_CLIENTEDGE,   
    (g_szClassName),     
    L"The title of my window",      
    WS_OVERLAPPEDWINDOW,      
    CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,        NULL, NULL, hInstance, NULL);
    if(hwnd == NULL)    {  
    MessageBox(NULL, 
        L"Window Creation Failed!",
        L"Error!",    
    MB_ICONEXCLAMATION | MB_OK);     
    return 0;  
    }
    ShowWindow(hwnd, nCmdShow);  
    UpdateWindow(hwnd);
    // Step 3: The Message Loop    
    while(GetMessage(&Msg, NULL, 0, 0) > 0)    {     
    TranslateMessage(&Msg);       
    DispatchMessage(&Msg); 
    }  
    return Msg.wParam; 
    }

output :

    3   IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"  c:\Users\Youssef\Documents\Visual Studio 2012\Projects\Win32Project2\Win32Project2\Win32Project2.cpp    46  2   Win32Project2
    2   IntelliSense: a value of type "const char *" cannot be assigned to an entity of type "LPCWSTR"  c:\Users\Youssef\Documents\Visual Studio 2012\Projects\Win32Project2\Win32Project2\Win32Project2.cpp    34  19  Win32Project2
Error   1   error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?   c:\users\youssef\documents\visual studio 2012\projects\win32project2\win32project2\win32project2.cpp    65  1   Win32Project2
Youssef
  • 35
  • 3
  • 5
  • 1
    See this: http://stackoverflow.com/questions/27049481/problems-with-cfiledialog-instantiation/27049624#27049624 Basically, you are building an application that takes wide strings in the Windows functions, but the strings you created are not wide character strings. See the link for more info. – PaulMcKenzie Jun 12 '15 at 15:39
  • You already know how to prefix `L` to string literals to turn them into Unicode strings, just do it consistently. – Hans Passant Jun 12 '15 at 17:00
  • The other error says *"Did you forget to add '#include "stdafx.h"'* to your source?" , so include that in first line, or go to project properties -> C/C++ -> precompiled headers and change the file setting – Barmak Shemirani Jun 12 '15 at 18:19

1 Answers1

2

The compilation issue you're having has to do with string types and Windows programs.

Note that there are two character set "build types" for a Windows program. The first is MBCS and the second is Unicode. By default (assuming you're using Visual Studio), the build type is Unicode, meaning that the various Windows API functions will process wide strings, not char or char-based strings.

The easiest solution to your problem is to use TCHAR for character arrays and _T() macro for string literals meant to be sent and received from Windows API functions. This will ensure your app will compile for Unicode, as well as MBCS (although MBCS programs are now considered obsolete in this day and age).

This line:

 wc.lpszClassName = (g_szClassName);  

gives an issue, since you are compiling a Unicode application, and this member of WNDCLASSEX takes an LPCTSTR (a pointer to TCHAR), not a const char *.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633577%28v=vs.85%29.aspx

So for this problem,

const char g_szClassName[] = ("myWindowClass");

The change should be:

#include <tchar.h> // in case it isn't included 
//...
TCHAR g_szClassName[] = _T("myWindowClass");
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • 1
    Note that the `_T()` macro belongs to the C runtime library, for use with the `_TCHAR` type. The Win32 API, on the other hand, has its own `TCHAR` type and `TEXT()` macro. Technically, `_TCHAR` and `TCHAR`, `_T()` and `TEXT()`, all map to the same data type and thus are interchangeable, but logically they belong to different APIs and should not be mixed. Use the appropriate type/macro for each library call you make (in this case, the Win32 API), so `_T()` with `_TCHAR`, `TEXT()` with `TCHAR` - `TCHAR g_szClassName[] = TEXT("myWindowClass");` – Remy Lebeau Jun 12 '15 at 17:27
  • And don't forget the `MessageBox()` and `CreateWindowEx()` calls in this code are also `TCHAR`-based, so use the `TEXT()` macro for those literals as well, eg: `MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);`, otherwise use the `WCHAR` functions instead, eg: `MessageBoxW(NULL, L"Window Registration Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);` – Remy Lebeau Jun 12 '15 at 17:31