• 13
• 18
• 19
• 27
• 9

Stuck with WIN32 API

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

Recommended Posts

Hello everyone, My teacher as an assigment gave me a task to write a Window Manager that could open multiple windows. Each Window can render a scene in either D3D or OpenGL. The problem is I am unable to get my WIN32 API calls to work well, the Windows I am trying to make from Window_Manager do not get build, because I think I made a mistake somewhere thats making the HWND not working well. I posted the code on my Developer journal, and have now rewritten a few things and still It did not work :( http://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=401209 Has anyone else ever had to do something like this, and does anyone know what I am doing wrong? :) Thjank you! Wijnand

Share on other sites
I've been poring over your code in your journal for a while now, and I couldn't see anything wrong with it. As an experiment I wrote this (THIS IS VERY BAD CODE PURELY TO ILLUSTRATE THAT WIJNAND'S PRINCIPLE SEEMS SOUND)

#include <windows.h>#include <windowsx.h>#include <list>class Window{public:	Window(HINSTANCE HIn);	HWND Hw;};Window::Window(HINSTANCE HIn){    Hw=CreateWindowEx(WS_EX_TOPMOST,"MyWin","",WS_OVERLAPPEDWINDOW,                      0,0,640,480,                      GetDesktopWindow(),NULL,HIn,NULL);    if(Hw==NULL) return;	ShowWindow(Hw,SW_SHOWDEFAULT); UpdateWindow(Hw); SetFocus(Hw);}class WindowManager{private:	std::list<Window*> Ws;	public:	WindowManager(){ }	void Add(HINSTANCE HIn);};void WindowManager::Add(HINSTANCE HIn){	Ws.push_back(new Window(HIn));}WindowManager Wm;LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);int Register(HINSTANCE HIn);int WINAPI WinMain(HINSTANCE HIn,HINSTANCE,LPSTR CmdLine,int Show){    MSG Msg;        if(!Register(HIn)) return 0;	Wm.Add(HIn);	Wm.Add(HIn);    PeekMessage(&Msg,NULL,0,0,PM_NOREMOVE);    while(Msg.message!=WM_QUIT)        {        if(PeekMessage(&Msg,NULL,0,0,PM_REMOVE))            {            TranslateMessage(&Msg);            DispatchMessage(&Msg);            }		}    return static_cast<int>(Msg.wParam);}void OnDestroy(HWND Hw){    PostQuitMessage(0);}LRESULT CALLBACK WndProc(HWND Hw,UINT Msg,WPARAM wParam,LPARAM lParam){    switch(Msg)        {        HANDLE_MSG(Hw,WM_DESTROY,OnDestroy);        default: return DefWindowProc(Hw,Msg,wParam,lParam);        }}int Register(HINSTANCE HIn){    WNDCLASSEX Wc;    Wc.cbSize=sizeof(WNDCLASSEX);    Wc.style=0;    Wc.lpfnWndProc=WndProc;    Wc.cbClsExtra=0;    Wc.cbWndExtra=0;    Wc.hInstance=HIn;    Wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);    Wc.hCursor=LoadCursor(NULL,IDC_ARROW);    Wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);    Wc.lpszMenuName=NULL;    Wc.lpszClassName="MyWin";    Wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);    return RegisterClassEx(&Wc);}

That creates two windows without a problem, so the principle is obviously sound. I'm stumped as to why yours doesn't work.

Obviously there are many other potential problems with a design like this. All your windows will share the same WndProc with no way of differentiating between them, but I've already pointed you at Oluseyi's article on this subject so I assume that is not an issue you are concerned about at the moment.

Sorry I can't be of more help but I just wanted to give you some code that did work in (sort of) the way your seems to be trying to in the hopes you might spot a difference that could be causing yours to fail. I can't see it.

Share on other sites
Could you add me on MSN or ICQ? my msn is wijnand@dalmijn.com maybe we can go through it together :) Thank you for all the attempts you have done so far its really appreciated ^^

Share on other sites
I don't actually have an IM account of any kind. You're of course quite welcome to PM me here, but I'd suggest that if we discuss things in the forums, others far more knowledgable than me (i.e. most GD posters) can also wade in and give you a far better quality of assistance.

Do you have anywhere you could put your entire code up on the internet so people can download and play around with it?

Share on other sites
put a breakpoint at the beginning of your winmain function and step through to see where things go wrong.

-me

Share on other sites
Could you be a bit more specific about your problem? Which Win32 call is failing? What is GetLastError() returning?

Share on other sites
Not sure why Palidine edited his/her post but they may well have cracked it.

In the code in your journal, you don't have a message loop in your WinMain, so your windows get created then the program just terminates immediatley.

Was this ommitted from the journal post for brevity, or have you not implemented the message loop in WinMain?

Share on other sites
that is the strange thing about all of this, even with all break points all over the thing. It still does not crash, it does not give an error (exists with code 0x0).

The problem is that Hwnd is not being properly defined apparantly, whenever I put it throught the debugger it always complains that the HWND is invalidly specified and I think thats the main reason its not opening the window. but I am a bit baffeled its giving me these errors :/

Share on other sites
Just for clarity, could you post your actual message loop code that sits in WinMain after you create the windows?

[EDIT - although I did just put a breakpoint before the message loop in my mini-app above and the window did appear. Was a bitch to close again though]

Share on other sites
http://members.gamedev.net/wijnand/programs/Vyper0.1.zip VS2005

Here is the source code of the project.

The Loop is in the Window_Manager.cpp its the LRESULT CALLBACK

I did not make the original messageloop (it was there) but I removed it after trying to figure out what was causing the errors, I didn't think the message loop itself was causing the problems :( I just had to fish a backup out becuase I completly wrecked the code I posted on my journal LOL I even went as far as to just move everything WINAPI related to Window.CPP and it still would not show itself on my screen.

And im probably shooting myself in the foot when the problem is found because it was so stupid lol