Hello. Studying WINAPI functions according to Ganeev, I came across the fact that his program does not want to work. VS 10 finds errors here:

if (!RegClass(WndProc, szClassName, COLOR_WINDOW)) return FALSE; hwnd=CreateWindowEx(szClassName, "Ex1", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, NULL); 

Where is the mistake? Here is the whole source:

  #include"windows.h" extern HINSTANCE hInstance; BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HINSTANCE hInstance; char szClassName[]="WindowsAppClass"; INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hwnd; hInstance=hInst; if (!RegClass(WndProc, szClassName, COLOR_WINDOW)) return FALSE; hwnd=CreateWindowEx(szClassName, "Ex1", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, NULL); if(!hwnd) return FALSE; while(GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg); return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY:{PostQuitMessage(0); return 0;} } return DefWindowProc(hwnd, msg, wParam, lParam); } BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground) { WNDCLASSEX wc; wc.style = CS_HREDRAW | CS_VREDRAW; 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)(brBackground+1); wc.lpszMenuName = (LPCTSTR)NULL; wc.lpszClassName = szName; return (RegisterClassEx(&wc)!=0); } 
  • Which line shows which errors? - Ildar
  • Error C2660: 'CreateWindowExW': function does not take 11 arguments error C2664: 'RegClass': cannot convert parameter 2 from 'char [16]' to 'LPCTSTR' error C2731: 'WinMain': function cannot be overloaded - Alerr 5:56

3 answers 3

You have the first HWND WINAPI CreateWindowEx( __in DWORD dwExStyle, ...) parameter is missing.

CreateWindowEx function

    Error in passing the string szClassName (in the RegClass and CreateWindowEx ), type LPCTSTR - pointer to string.
    C ++ forgot, I could be wrong. Try passing &szClassName[0] , or apply a type cast, like this: (LPCTSTR)szClassName[0] .

    UPD
    I give my example, tested on VC ++ 6:

     #include "stdafx.h" #include <windows.h> HINSTANCE hInst; char szWindowClass[] = "WindowsAppClass"; char szTitle[] = "title"; ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; MyRegisterClass(hInstance); if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return (int)msg.wParam; } ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = 0; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)0; wcex.lpszClassName = szWindowClass; wcex.hIconSm = 0; return RegisterClassEx(&wcex); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; hWnd = CreateWindowEx(WS_EX_TOOLWINDOW, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } 

    UPD2
    Your example corrected:

     #include "StdAfx.h" #include "windows.h" HINSTANCE hInstance; BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); char szClassName[]="WindowsAppClass"; INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hwnd; hInstance=hInst; if (!RegClass(WndProc, szClassName, COLOR_WINDOW)) return FALSE; hwnd=CreateWindowEx(WS_EX_TOOLWINDOW, szClassName, "Ex1", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, NULL); if(!hwnd) return FALSE; while(GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg); return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY:{PostQuitMessage(0); return 0;} } return DefWindowProc(hwnd, msg, wParam, lParam); } BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground) { WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)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)(brBackground+1); wc.lpszMenuName = (LPCTSTR)NULL; wc.lpszClassName = szName; wc.hIconSm = 0; return (RegisterClassEx(&wc)!=0); } 
    • Errors continue .... (error C2660: 'CreateWindowExW': function does not take 11 arguments, etc .... - Alerr
    • Can you show the working version of the code? - Alerr
    • The answer is updated. The string 'szClassName' was perceived normally, the first parameter of 'CreateWindowEx' was omitted (function does not take 11 arguments - it has 12 parameters), there were other assumptions. - Ildar
    • one
      In Visual Studio 2010 <WinApiFunction> W (for example, CreateWindowExW) requires string parameters in the extended (two-byte) format (wchar_t instead of char). CreateWindowEx is by default equal to CreateWindowExW (it is defined there through define). Problem solving: CreateWindowEx (WS_EX_TOOLWINDOW, szClassName, L "Ex1", ...); OR CreateWindowExA (WS_EX_TOOLWINDOW, szClassName, "Ex1", ...); - Baho

    error C2664: 'RegClass': cannot convert parameter 2 from 'char [16]' to 'LPCTSTR'

    In the project settings you have Unicode set, therefore, CreateWindow is replaced by CreateWindowW, which accepts Unicode strings. The easiest option is to change to multibyte strings in the settings. To create a Unicode literal, you must use L before quotes. The character type must not be char, but wchar_t.

    About

    error C2731: 'WinMain': function cannot be overloaded

    Correct the signature as follows: INT WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInstance, LPCTSTR lpCmdLine, int nCmdShow) Instead of LPTSTR (non-constant string), you must send LPCTSTR (constant string). Only I am not sure that this will correct your mistake.

    error C2660: 'CreateWindowExW': function does not take 11 arguments

    Maybe instead of CreateWindowEx use CreateWindow?