ok. so i'm having trouble with the Windows API. i'm just learing how to use it, so i don't know much about it.

anyways, i just wanted to create a window and have it displayed then catch the exit message and close, but VC Compiler is throwing errors.

1>c:\users\furrix\documents\visual studio 2008\projects\test\test\test.cpp(9) : error C2144: syntax error : 'int' should be preceded by ';'

1>c:\users\furrix\documents\visual studio 2008\projects\test\test\test.cpp(24) : error C2065: 'WindowProc' : undeclared identifier

1>c:\users\furrix\documents\visual studio 2008\projects\test\test\test.cpp(32) : error C2664: 'CreateWindowExW' : cannot convert parameter 10 from 'HINSTANCE' to 'HMENU'

1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

could someone please help me with this? i'm a bit confused.

code:

#include <windows.h>
#include <windowsx.h>

//WinProc Function prototype
LRESULT CALLBACK WinProc (HWND hWnd, UINT message, WPARAM wparam, LPARAM lparam)

//entry point for any windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//window handle
	HWND hWnd;

	//holds informaintion for windows class
	WNDCLASSEX wc;

	//clear windows were using
	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	
	//WC structure data
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.lpszClassName = L"WindowsClass1";

	
	//generate the window
	hWnd = CreateWindowEx(NULL, L"WindowsClass1", L"Windows Form Frame 1", WS_OVERLAPPEDWINDOW, 300, 300, 500, 400, NULL, hInstance, NULL, NULL);

	//Show generated Window
	ShowWindow(hWnd, nCmdShow);

	MSG msg;

	// wait for the next message in the queue, store the result in 'msg'
    while(GetMessage(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam;

}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch(message)
    {
        // this message is read when the window is closed
        case WM_DESTROY:
            {
                // close the application entirely
                PostQuitMessage(0);
                return 0;
            } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc (hWnd, message, wParam, lParam);
}

Recommended Answers

All 5 Replies

Here is a tutorial that might help...

http://www.jose.it-berater.org/smfforum/index.php?topic=3389.0

The first problem I saw in your code is that you foreward declare WinProc, but you named your actual Window Procedure 'WindowProc', and so when you used the term ...

wc.lpfnWndProc = WindowProc;

...it was unrecognized by the compiler (it wasn't foreward declared.

Also, the 10th parameter of the CreateWindowEx() call should have a cast like so...

...,(HMENU)NULL,...

Here is a tutorial that might help...

http://www.jose.it-berater.org/smfforum/index.php?topic=3389.0

The first problem I saw in your code is that you foreward declare WinProc, but you named your actual Window Procedure 'WindowProc', and so when you used the term ...

wc.lpfnWndProc = WindowProc;

...it was unrecognized by the compiler (it wasn't foreward declared.

Also, the 10th parameter of the CreateWindowEx() call should have a cast like so...

...,(HMENU)NULL,...

Just spotted your first error about the int. You didn't put a semicolon after this declare...

LRESULT CALLBACK WinProc (HWND hWnd, UINT message, WPARAM wparam, LPARAM lparam)

...so it gave an error on the next line. So you've a lot of problems here.

I've fixed your program so that it will at least compile and run AltXError. You made a lot of serious errors. I consider this to be a very poor attempt at something that is rather exacting. I'd encourage you to study harder and be more careful. Here is your somewhat repaired program. It still isn't exactly right, but its closer.

#include <windows.h>
#include <windowsx.h>

//WinProc Function prototype
LRESULT CALLBACK WindowProc (HWND hWnd, UINT message, WPARAM wparam, LPARAM lparam);

//entry point for any windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    wchar_t szClassName[]=L"WindowsClass1"; 
	//window handle
	HWND hWnd;

	//holds informaintion for windows class
	WNDCLASSEXW wc;

	//clear windows were using
	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	
	//WC structure data
	wc.cbSize = sizeof(WNDCLASSEXW);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.lpszClassName = szClassName;
    RegisterClassExW(&wc);
	
	//generate the window
	hWnd = CreateWindowExW(0, L"WindowsClass1", L"Windows Form Frame 1", WS_OVERLAPPEDWINDOW, 300, 300, 500, 400, NULL, (HMENU)NULL, hInstance, 0);

	//Show generated Window
	ShowWindow(hWnd, nCmdShow);

	MSG msg;

	// wait for the next message in the queue, store the result in 'msg'
    while(GetMessage(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam;

}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch(message)
    {
        // this message is read when the window is closed
        case WM_DESTROY:
            {
                // close the application entirely
                PostQuitMessage(0);
                return 0;
            } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc (hWnd, message, wParam, lParam);
}

I fixed a few more of your multitudinous errors

#include <windows.h>
#include <windowsx.h>

//WinProc Function prototype
LRESULT CALLBACK WindowProc (HWND hWnd, UINT message, WPARAM wparam, LPARAM lparam);

//entry point for any windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    wchar_t szClassName[]=L"WindowsClass1"; 
	//window handle
	HWND hWnd;

	//holds informaintion for windows class
	WNDCLASSEXW wc;

	//clear windows were using
	ZeroMemory(&wc, sizeof(WNDCLASSEXW));

	
	//WC structure data
	wc.cbSize = sizeof(WNDCLASSEXW);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.lpszClassName = szClassName;           //You can't assign a char array with = sign!
    RegisterClassExW(&wc);   //you forgot to register the window class
	
	//generate the window
	hWnd = CreateWindowExW(0, L"WindowsClass1", L"Windows Form Frame 1", WS_OVERLAPPEDWINDOW, 300, 300, 500, 400, NULL, (HMENU)NULL, hInstance, 0);

	//Show generated Window
	ShowWindow(hWnd, nCmdShow);

	MSG msg;

	// wait for the next message in the queue, store the result in 'msg'
    while(GetMessageW(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessageW(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam;

}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch(message)
    {
        // this message is read when the window is closed
        case WM_DESTROY:
            {
                // close the application entirely
                PostQuitMessage(0);
                return 0;
            } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProcW (hWnd, message, wParam, lParam);
}

From looking at your code here are some things you need to learn;

1) How to use the TCHAR macros and the difference between ansi and wide character strings;
2) Basic architecture of Win Api program;
3) What you can and can't do with character arrays;
4) How to be careful.

ok. well this is still a bit new to me. I'm used to programming in .net where windows handles aren't used often, if at all. thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.