1

I am trying to create a button but I always got an error

"cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'"

I tried:

HWND hwndButton = CreateWindow( 
    L"BUTTON",  
    L"OK",      
    WS_TABSTOP | WS_VISIBLE | BS_DEFPUSHBUTTON,  // Styles 
    10,         
    10,        
    100,       
    100,        
    NULL,     
    NULL,       
    (HINSTANCE)GetWindowLongPtr(NULL, GWLP_HINSTANCE), 
    NULL);     

Can someone help me to fix this???

Asesh
  • 3,186
  • 2
  • 21
  • 31
  • 3
    You have to enable unicode support in your project settings, or you should not use wchar and use ASCII strings instead. – Devolus Jun 07 '21 at 16:38
  • 1
    The `L` prefix will create `wchar_t` ([see here](https://stackoverflow.com/questions/1810343/is-a-wide-character-string-literal-starting-with-l-like-lhello-world-guarantee)) which may be desirable if you are intending to support unicode, but if you want a LPCSTR just pass `"BUTTON"` – Cory Kramer Jun 07 '21 at 16:39
  • 3
    Another alternative is to wrap your strings in `_T("BUTTON")` macro. – Devolus Jun 07 '21 at 16:40
  • Call `CreateWindowW` (note the trailing `W`). Once that is done, make sure to read [What is the HINSTANCE passed to CreateWindow and RegisterClass used for?](https://devblogs.microsoft.com/oldnewthing/20050418-59/?p=35873). The better alternative is to get real learning material, like Petzold's [Programming Windows](https://www.amazon.com/dp/157231995X). – IInspectable Jun 07 '21 at 16:55
  • Also, creating a button that has no parent is largely useless. There's no one listening for messages. – IInspectable Jun 07 '21 at 16:56

2 Answers2

0

The fact that it's expanding to an A (ANSI) function suggests that your project is configured to use the multi-byte character set rather than Unicode. Windows is faster with the W functions and you don't really need the "charset agnostic" TCHAR and _T macros: Is TCHAR still relevant?

You haven't stated which IDE or compiler you're using so it's hard to suggest how to make it default to the Unicode character set instead. Try looking for "project properties" or similar.

If you mean to use the W functions, just call them directly. I explicitly say I'm calling say, wWinMain, not _tWinMain, SetWindowTextW,not SetWindowText, MAKEINTRESOURCEW, not MAKEINTRESOURCE, etc. Targeting and supporting the A functions is a concern that is very, very, seldom relevant these days. Since you're hard-coding the string arguments as L"" I assume you don't care to support ANSI either.

HWND hwndButton = CreateWindowW( 
    L"BUTTON",  
    L"OK",      
    WS_TABSTOP | WS_VISIBLE | BS_DEFPUSHBUTTON,  // Styles 
    10,         
    10,        
    100,       
    100,        
    NULL,     
    NULL,       
    (HINSTANCE)GetWindowLongPtr(NULL, GWLP_HINSTANCE), 
    NULL
);     
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
0
#include <Windows.h>

int main(INT argc, WCHAR* argv[])
{
    MSG msg;
    HWND hWnd = CreateWindowExW(0L, L"button", L"Hello, World!!!", WS_VISIBLE | WS_POPUP, 10, 10, 100, 25, NULL, NULL, NULL, NULL);
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    while (GetMessageW(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }

    return msg.wParam;
}
Kotlin 101
  • 16
  • 1
  • Please explain your solution. Answers which do not have an explanation and are only code get flagged as low effort. – cursorrux Sep 05 '21 at 11:14
  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 05 '21 at 11:14