Sign in to follow this  
flashinpan

Trying to compile simple Win32 application

Recommended Posts

flashinpan    100
I am trying to compile a simple Win32 application. I chose "Console" for my application type, then pasted in the following code:
#include <windows.h>

const char *ClsName = "BasicApp";
const char *WndName = "A Simple Window";

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
			   WPARAM wParam, LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
	MSG        Msg;
	HWND       hWnd;
	WNDCLASSEX WndClsEx;

	// Create the application window
	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
	WndClsEx.lpfnWndProc   = WndProcedure;
	WndClsEx.cbClsExtra    = 0;
	WndClsEx.cbWndExtra    = 0;
	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClsEx.lpszMenuName  = NULL;
	WndClsEx.lpszClassName = ClsName;
	WndClsEx.hInstance     = hInstance;
	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	// Register the application
	RegisterClassEx(&WndClsEx);

	// Create the window object
	hWnd = CreateWindow(ClsName,
			  WndName,
			  WS_OVERLAPPEDWINDOW,
			  CW_USEDEFAULT,
			  CW_USEDEFAULT,
			  CW_USEDEFAULT,
			  CW_USEDEFAULT,
			  NULL,
			  NULL,
			  hInstance,
			  NULL);
	
	// Find out if the window was created
	if( !hWnd ) // If the window was not created,
		return 0; // stop the application

	// Display the window to the user
	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	// Decode and treat the messages
	// as long as the application is running
	while( GetMessage(&Msg, NULL, 0, 0) )
	{
             TranslateMessage(&Msg);
             DispatchMessage(&Msg);
	}

	return Msg.wParam;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
			   WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    // If the user wants to close the application
    case WM_DESTROY:
        // then close it
        PostQuitMessage(WM_QUIT);
        break;
    default:
        // Process the left-over messages
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    // If something was not done, let it go
    return 0;
}


Here was the output (and the error message): ------ Build started: Project: TestWin32Prog, Configuration: Debug Win32 ------ Compiling... Main.cpp c:\TestWin32Prog\Main.cpp(62) : warning C4244: 'return' : conversion from 'WPARAM' to 'int', possible loss of data Linking... LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup Debug/TestWin32Prog.exe : fatal error LNK1120: 1 unresolved externals Build log was saved at "file://c:\TestWin32Prog\Debug\BuildLog.htm" TestWin32Prog - 2 error(s), 1 warning(s) ---------------------- Done ---------------------- Build: 0 succeeded, 1 failed, 0 skipped

Share this post


Link to post
Share on other sites
Kippesoep    892
Classic mistake. When you create a console program, your main program entry point is int main () or int main (int argc, char **argv). WinMain is for Win32 (non-console) programs.

Share this post


Link to post
Share on other sites
Perferati    138
Problem seems that your project is searching for a main(...) function which is required for a console application.

WinMain is the Win32 equiv. for a window application. In Visual Studio 6, there are two different projects - a Win32 Application and a Win32 console application.

The Win32 app. uses the WinMain and WndProc - console app just needs a main()

I too have noticed that my VIsual studio 2005 express only has a win32 console app, what to do for a standard win32 program is something I don't know - can anyone else help?

Share this post


Link to post
Share on other sites
flashinpan    100
Quote:
Original post by Kippesoep
Classic mistake. When you create a console program, your main program entry point is int main () or int main (int argc, char **argv). WinMain is for Win32 (non-console) programs.



Sigh...the tutorial told me to use the "Console Application" option. I could have sworn it told me to do that.


Here is the tutorial:

Win32 Tutorial

Share this post


Link to post
Share on other sites
flashinpan    100
From the tutorial:


"If you are using MSVC .Net, click Application Settings, then click the Console Application radio button, then click the Empty Project check box:"


ANYWAY...DUH...I GET IT.....it won't work the way I have it right now...

Share this post


Link to post
Share on other sites
Kippesoep    892
That tutorial has the correct instructions for MSVC6 (where it tells you to select Windows Application), but for MSVC.NET it tells you to create a Console application, which is wrong.

MSVC2k5E first says you're creating a Console application, but after entering the name, it pops up a dialog allowing you to distinguish between Windows app, Console app, DLL and static library (assuming you've correctly enabled them after installing the Platform SDK).

Share this post


Link to post
Share on other sites
flashinpan    100
Quote:
Original post by Kippesoep
That tutorial has the correct instructions for MSVC6 (where it tells you to select Windows Application), but for MSVC.NET it tells you to create a Console application, which is wrong.

MSVC2k5E first says you're creating a Console application, but after entering the name, it pops up a dialog allowing you to distinguish between Windows app, Console app, DLL and static library (assuming you've correctly enabled them after installing the Platform SDK).




I just went back and created a new project under C++, this time picking Windows as my application kind instead of Console Application.


NOW it works!!!


Thanks!!


Sorry for wasting everyone's time. I was not putting 2 and 2 together.

Share this post


Link to post
Share on other sites
xeddiex    238
Quote:
Original post by Perferati
Problem seems that your project is searching for a main(...) function which is required for a console application.

WinMain is the Win32 equiv. for a window application. In Visual Studio 6, there are two different projects - a Win32 Application and a Win32 console application.

The Win32 app. uses the WinMain and WndProc - console app just needs a main()

I too have noticed that my VIsual studio 2005 express only has a win32 console app, what to do for a standard win32 program is something I don't know - can anyone else help?


Here ya go: Enabling Win32 Windows Application type

xeddiex

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this