Program window wont show up when I add a function.

For some reason when I add the code for this function, without even calling it, my main window just refuses to show up.

I was having problems making dialog boxes show with it in a rather large program, so I decided to start a test case from scratch and slowly add code until I found out what was wrong, but in the other program at least my main window shows... Anyway I'd like to know what is causing it. If I disable the function the problem disappears.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void HandlePacket(RakClientInterface * rakClientInterface, Packet * p)
{
    unsigned char packetID;
    
    BitStream dataStream((const char*)p->data, p->length, false);
    
    dataStream.Read(packetID);
    
    switch(packetID) {
    case PACKET_ID_LOGINRESPONSE:
        dataStream.Read(loggedin);
        if(!loggedin)
           MessageBox(hwnd, "Incorrect Username/Password combination.", "Login Error", 0);
    break;
    }
}


Here is the full contents of main.cpp for my test case... the only cpp file in this project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

#include <RakNetworkFactory.h>
#include <RakClientInterface.h>
#include <bitstream.h>
#include <windows.h>
#include <commctrl.h>

#include "resource.h" 

RakClientInterface *rakClientInterface;
Packet * packet = NULL;
const unsigned char PACKET_ID_LOGIN = 100;
const unsigned char PACKET_ID_LOGINRESPONSE = 101;
const unsigned char PACKET_ID_LOGOUT = 102;
HWND hwnd;
bool loggedin = false;
bool connected = false;
bool lastconnected = false;
DWORD lasttime = 0;
void HandlePacket(RakClientInterface*, Packet*);



BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_INITDIALOG:

		return TRUE;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDOK:
					EndDialog(hwnd, IDOK);
				break;
				case IDCANCEL:
					EndDialog(hwnd, IDCANCEL);
				break;
			}
		break;
		default:
			return FALSE;
	}
	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FILE_EXIT:
					PostMessage(hwnd, WM_CLOSE, 0, 0);
				break;
				case ID_HELP_ABOUT:
				{
					int ret = DialogBox(GetModuleHandle(NULL), 
						MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
					if(ret == IDOK){
						MessageBox(hwnd, "Dialog exited with IDOK.", "Notice",
							MB_OK | MB_ICONINFORMATION);
					}
					else if(ret == IDCANCEL){
						MessageBox(hwnd, "Dialog exited with IDCANCEL.", "Notice",
							MB_OK | MB_ICONINFORMATION);
					}
					else if(ret == -1){
						MessageBox(hwnd, "Dialog failed!", "Error",
							MB_OK | MB_ICONINFORMATION);
					}
				}
				break;
			}
		break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
    const char g_szClassName[] = "myWindowClass";
	WNDCLASSEX wc;
	MSG Msg;

	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		g_szClassName,
		"The title of my window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}

void HandlePacket(RakClientInterface * rakClientInterface, Packet * p)
{
    unsigned char packetID;
    
    BitStream dataStream((const char*)p->data, p->length, false);
    
    dataStream.Read(packetID);
    
    switch(packetID) {
    case PACKET_ID_LOGINRESPONSE:
        dataStream.Read(loggedin);
        if(!loggedin)
           MessageBox(hwnd, "Incorrect Username/Password combination.", "Login Error", 0);
    break;
    }
}
Last edited on
Well...you are never actually calling it anywhere...
I don't see an int main() anywhere...
For Win API programs the entry point is WinMain
"Well...you are never actually calling it anywhere..."
Thats whats got me so confused, firedraco.
It doesn't matter if I call it or not, just having the code there makes my window never show up. Commenting it out makes my window show up again.

The function prototype has nothing to do with it either.

I am asking because I don't understand how adding code and not calling it can have such a strange effect on a program...

I'm not very good with things like arrays, global variables, etc... though I do know globals are bad practice. I'm wondering if maybe the way I'm creating something is causing a memory leak?
If merely having the code compiled in is causing strange behaviour. I'd lay a guess on your experiencing the effects of a buffer overflow somewhere.

Having the extra code would change the offset to alot of function calls. Somewhere something could be overwritten and preventing the execution path from being correct and thus causing undefined behaviour.
Line 10: rakClientInterface is same name as the parameter to the function. Best to avoid this so you don't end up with confusion later :)
Okay this is absolutely pathetic but I was missing a dll file in the folder, and my computer didn't show an error message.

When you said that I started commenting the function in line by line and actually found an error message this time.

Really sorry to waste your time on something so stupid guys, I can't believe I missed that. I'll try to make up for it by helping other posters once in a while...

Thanks for the help Zaita.
No worries man, glad we could help ya find the problem. Even if it wasn't code related :)
Topic archived. No new replies allowed.