Trying to compile simple Win32 application

This topic is 4470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance     = hInstance;

// 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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by KippesoepClassic 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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by KippesoepThat 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 on other sites
Quote:
 Original post by PerferatiProblem 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