Jump to content
  • Advertisement
Sign in to follow this  
Sean_Seanston

Trouble setting up WinApi for DirectX

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm starting to learn DirectX but I can't get a basic WinApi program that displays a window to work for some reason. I've done it before but the code from 3 separate tutorials produces weird errors I'm not able to decipher right now. Something to do with not setting up Visual Studio 2008 Express for it maybe? Here's one version:
#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;
}
This gives me: 1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup 1>C:\Users\Seán\Documents\Visual Studio 2008\Projects\DirectX1\Debug\DirectX1.exe : fatal error LNK1120: 1 unresolved externals The other 2 give different errors, but maybe there's a common reason, I dunno. At least one of these tutorials worked perfectly for me before (but then that was on a different machine, with Visual Studio 2005 Express and running XP). Must be something simple. Any help please? ^_^

Share this post


Link to post
Share on other sites
Advertisement
That error means that you created a console project instead of a Windows project. Go to your linker settings and change the subsystem from console to Windows.

Share this post


Link to post
Share on other sites
You created the application as a console app instead of a windows app. You can change that in project settings (Alt+F7) -> Configuration Properties -> Linker -> System -> SubSystem. Change that to Windows (/SUBSYSTEM:WINDOWS).

Share this post


Link to post
Share on other sites
Also, please remove the default case of the switch and put the return DefWindowProc after the switch.

The tutorial that shows it this way needs to be shot.

It assumes every handled message will want 0 as a return value. Which is not the case. It will save you quite a few headaches if you add more messages to your handler.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!