Win32 API why wont the window load Error: 183

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1182 Views - Last Post: 18 January 2012 - 05:22 PM Rate Topic: -----

#1 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Win32 API why wont the window load Error: 183

Posted 17 January 2012 - 07:03 PM

I have been trying for a while now to get this working and the GetLastError() returns 183 so... I looked it up and found out that is meant the the file already exists... That doesn't make much sense to me.

#include <Windows.h>
#include <MMSystem.h>
#include <iostream>
#include "resource.h"

HWND mainWnd;
HMENU hMenu;

LRESULT CALLBACK
	WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	return 0;
}

float lastTime = (float)timeGetTime();

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd)
{
	LOGBRUSH lb;
	lb.lbColor = RGB(200,0,0);
	lb.lbStyle = BS_HATCHED;
	lb.lbHatch = HS_BDIAGONAL;
	HBRUSH t = CreateBrushIndirect(&lb);

	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = t;
	wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
	wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "mainWnd";
	wc.lpszMenuName = 0;
	wc.style = CS_HREDRAW | CS_VREDRAW;

	hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1));

	RegisterClass(&wc);

	mainWnd = CreateWindow("mainWnd", "windowName", WS_OVERLAPPEDWINDOW, 0,0, 500, 500, 0, hMenu, hInstance, 0);

	char error[260];
	sprintf_s(error, "ERROR WINDOW FAIL: %d", GetLastError());

	if(mainWnd == 0)
	{
		MessageBox(0, error, "ERror", MB_OK);
		return -1;
	}

	ShowWindow(mainWnd, showCmd);
	UpdateWindow(mainWnd);

	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while(msg.message != WM_QUIT)
	{
		if(PeekMessage(&msg, 0,0,0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			float currTime = (float)timeGetTime();


			float deltaTime = (currTime - lastTime)*0.001f;

			lastTime = currTime;
		}
	}
}


void fps(float deltaTime)
{
	static int frames = 0;
	static float timeElapsed = 0.f;
	static char buffer[256];

	++frames;

	timeElapsed += deltaTime;

	sprintf_s(buffer, "FPS = %d", frames);

	if(timeElapsed >= 1.0f)
	{
		SetWindowText(mainWnd, buffer);
	}
}



thanks,
Dartos

This post has been edited by dartoscoder: 17 January 2012 - 07:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Win32 API why wont the window load Error: 183

#2 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: Win32 API why wont the window load Error: 183

Posted 17 January 2012 - 07:09 PM

Sorry for repost Dont know why that always happens
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4464
  • View blog
  • Posts: 24,909
  • Joined: 10-May 07

Re: Win32 API why wont the window load Error: 183

Posted 17 January 2012 - 07:12 PM

Is it possible you could post resource.h?
Was This Post Helpful? 0
  • +
  • -

#4 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: Win32 API why wont the window load Error: 183

Posted 17 January 2012 - 07:41 PM

Okay here it is


#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDR_MENU1                               100
#define IDM_TEST1                               40000


Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 719
  • View blog
  • Posts: 1,976
  • Joined: 28-March 11

Re: Win32 API why wont the window load Error: 183

Posted 17 January 2012 - 08:01 PM

Where do you have IDI_APPLICATION defined?

this is wrong: LoadCursor(hInstance, IDC_ARROW);

to load stock cursors it should be:
LoadCursor(NULL, IDC_ARROW);

And another thing, LoadIcon is depreciated, use LoadImage instead

This post has been edited by GunnerInc: 17 January 2012 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: Win32 API why wont the window load Error: 183

Posted 17 January 2012 - 09:41 PM

should i use LoadImage for the icon too or just the cursor?

Changing those parameters did not seem to help sadly
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 977
  • View blog
  • Posts: 3,393
  • Joined: 19-February 09

Re: Win32 API why wont the window load Error: 183

Posted 17 January 2012 - 11:04 PM

Your CreateWindow() fails because WndProc doesn't handle any messages.
Was This Post Helpful? 0
  • +
  • -

#8 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: Win32 API why wont the window load Error: 183

Posted 18 January 2012 - 04:13 AM

Does it need to yet? I don't want the window to do anything.

I set the WndProc to handle WM_QUIT and WM_DESTROY and its still giving me error 183

Also I dont know if this means anything but RegisterClass() returns 5006

This post has been edited by dartoscoder: 18 January 2012 - 04:15 AM

Was This Post Helpful? 0
  • +
  • -

#9 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 719
  • View blog
  • Posts: 1,976
  • Joined: 28-March 11

Re: Win32 API why wont the window load Error: 183

Posted 18 January 2012 - 03:29 PM

A few things:

RegisterClass and WNDCLASS have been superseded by RegisterClassEx and WNDCLASSEX try those instead

Does it need to yet? I don't want the window to do anything.

This shows you have absolutely no idea about messages, callback procs, window creation.

Even if your window does nothing, there is ONE call that you HAVE to make - a call to DefWindowProc, that is all you need in a blank window proc. Since you are not handling any messages, you pass all messages on to windows and let the OS deal with them.

If there is anything that should be short and simple (optimized the best you can) is the message loop, you only need 3 api calls there, Get/PeekMessage, TranslateMessage and Dispatch message. That loop is handling MANY messages sometimes a second.

You don't call GetLastError right after CreateWindow, you call it only if CreateWindow returns 0.

So RegisterClass is failing? Do some detective work - What does LoadIcon return, what does load cursor return, what does CreateBrushIndirect return?

LoadImage is used to load images not cursors. My bad on the IDI_APPLICATION, I forgot that is the default app icon. It is a stock icon, anytime you load a stock object from the OS, you pass NULL as the hInstance.

Your resource file: where is your menu? You have it defined but it is not in the rc file?

This post has been edited by GunnerInc: 18 January 2012 - 03:31 PM

Was This Post Helpful? 1
  • +
  • -

#10 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: Win32 API why wont the window load Error: 183

Posted 18 January 2012 - 04:23 PM

yeah i just realized that i forgot DefWindowProc.

BTW no need to be mean about me being new to WIN32 jeez
Was This Post Helpful? 0
  • +
  • -

#11 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 719
  • View blog
  • Posts: 1,976
  • Joined: 28-March 11

Re: Win32 API why wont the window load Error: 183

Posted 18 January 2012 - 04:32 PM

Mean? I am going out of my way trying to troubleshoot your "bits and pieces" of code.

Your trying to build a rocket ship by taking parts and putting it together without even reading the manual.

-1 for trying to help you? Fine, good luck in your programming journey. I am done.
Was This Post Helpful? 0
  • +
  • -

#12 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: Win32 API why wont the window load Error: 183

Posted 18 January 2012 - 04:35 PM

I did not give you a minus one. I don't know who did.

This was mean.

View PostGunnerInc, on 18 January 2012 - 04:32 PM, said:

This shows you have absolutely no idea about messages, callback procs, window creation.

Was This Post Helpful? 0
  • +
  • -

#13 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 719
  • View blog
  • Posts: 1,976
  • Joined: 28-March 11

Re: Win32 API why wont the window load Error: 183

Posted 18 January 2012 - 04:41 PM

Attached Image


^-^
Was This Post Helpful? 0
  • +
  • -

#14 CuteTabitha23  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 6
  • Joined: 18-January 12

Re: Win32 API why wont the window load Error: 183

Posted 18 January 2012 - 05:00 PM

View Postdartoscoder, on 18 January 2012 - 04:23 PM, said:

yeah i just realized that i forgot DefWindowProc.

BTW no need to be mean about me being new to WIN32 jeez


Don't worry we all make mistakes. I made that mistake myself when I first started too.

Just so we are clear, I corrected your Wndproc and all I get is some diagonal lines.

Based on your code, I think you might find this useful (You'll have to set your Character Set to Unicode, as I believe you do not have that set.

#include <windows.h>

#define ID_TIMER 1


const wchar_t g_szMainWindow[]	= L"TimerProgram";

 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
 
	HWND				hWnd;
	WNDCLASSEX			wc;
	MSG					msg; 
 
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hbrBackground	= GetSysColorBrush(COLOR_BTNFACE);
	wc.hCursor			= LoadCursor(NULL,IDC_ARROW);
	wc.hIcon			= LoadIcon(NULL,IDI_APPLICATION);
	wc.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hInstance		= hInstance;
	wc.lpfnWndProc		= WndProc;
	wc.lpszClassName	= g_szMainWindow;
	wc.lpszMenuName		= NULL;
	wc.style			= 0;
 
	if(RegisterClassEx(&wc) == NULL)
	{
		MessageBox(NULL,L"The program was unable to initalize the window. This program will now close.", 
                           L"Could not initialze window",MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}
  
	hWnd = CreateWindowEx(WS_EX_WINDOWEDGE,
			      g_szMainWindow,
			      L"Default Window",
			      WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MAXIMIZEBOX,
			      CW_USEDEFAULT,
			      CW_USEDEFAULT,
			      400,
			      300,
			      NULL,
			      NULL,
			      hInstance,
			      NULL);
 
	if(hWnd == NULL)
	{
		MessageBox(NULL,L"The program was unable to create the window. This program will now close.", 
                           L"Could not create window",MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}
  
	ShowWindow(hWnd,nShowCmd);
	UpdateWindow(hWnd);
 
	// Do the Message Loop
 
	while(GetMessage(&msg,NULL,0,0) > 0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
 
	return msg.wParam;
 
}
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 
	static bool bFlip;

	switch(msg)
	{
		case WM_CREATE: // Window Creation

			SetTimer(hWnd, ID_TIMER, 500, NULL);

			bFlip = false;
			break;

		case WM_TIMER:
			if(bFlip == false)
			{
				SetWindowText(hWnd,L"Hello");
				bFlip = true;
			}
			else
			{
				SetWindowText(hWnd,L"World!");
				bFlip = false;
			}
			break;

		case WM_CLOSE:
 
			DestroyWindow(hWnd);
			KillTimer(hWnd,ID_TIMER);
 
			break;
 
		case WM_DESTROY:
 
			PostQuitMessage(0);
 
			break;
 
		default:
 
			return DefWindowProc(hWnd, msg, wParam, lParam); 
 
			break;
	}
 
	return 0;
}



This should get you started on the right track now. I hope this helps you! :)
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 977
  • View blog
  • Posts: 3,393
  • Joined: 19-February 09

Re: Win32 API why wont the window load Error: 183

Posted 18 January 2012 - 05:14 PM

View Postdartoscoder, on 19 January 2012 - 01:35 AM, said:

I did not give you a minus one. I don't know who did.


Well, I suppose it could be an accident.
I thought you could change it but it doesn't appear so.
I will neutralize it with a plus vote.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2