EndDialog crash

Hello everybody! I have been searching the answer to this problem for a few days but i couldn't find nothing. The problem it's that when i run the program it crashes when i call to EndDialog function, and i think it has not nothing strange.
Here is the code of the dialog:

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
 BOOL CALLBACK Titulo (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam){
		 HWND handler;
		 int error;
		 int valor;
		 char user[20];
		 char titulo[80];
		 FILE *archivo;
		 struct inicio conf;
		 struct test nuevo;
		 char buff[12];
		 FILE *file;
		 time_t hora;


		 switch(iMsg){

			 case WM_INITDIALOG:


			 return TRUE;

			 case WM_COMMAND:

				 switch(LOWORD (wParam)){


					case IDOK:

							GetDlgItemText(hDlg,1001,titulo,80);
							if( 0 == strcmp(user,"")){
								error = 1;
							}

							hora = time(NULL);
							itoa(hora,buff,10);

							 archivo = fopen(buff,"wb");

							 if(archivo == NULL){
								 return 0;
							 }

							 file = fopen("config.cnf","rb");
							 if(file == NULL){
								 return 0;
							 }

							 fread(&conf,sizeof(struct inicio),1,file);
						 
							 strcpy(nuevo.titulo,titulo);
							 strcpy(nuevo.creador,conf.nombre);
							 nuevo.activado = 0;

							 fclose(file);

							 fwrite(&nuevo,sizeof(struct test),1,archivo);
						 
							 fclose(archivo);

							 EndDialog(hDlg,0); //here crashes
						 
					return TRUE;

					case IDCANCEL:

							 EndDialog(hDlg,0); //here crashes

					return TRUE;

				 }

				 break;

		 }

		 return FALSE;

	 }


Thank you very much, im becoming totally crazy!
Instead of BOOL CALLBACK you should make it LRESULT CALLBACK and instead of return FALSE on line 76 you should make it return DefWindowProc(hDlg,iMsg,wParam,lParam);
Last edited on
voodark

How are you creating your dialog? With DialogBox??

Andy

P.S. DefWindowProc is only used when a dialog is a main window! If you are creating a dialog using either CreateDialog or DialogBox then, according to MSDN:

Although the dialog box procedure is similar to a window procedure, it must not call the DefWindowProc function to process unwanted messages.

Returning BOOL is what Petzold says, though the new form -- also according to MSDN -- of the prototype for DialogProg is:

1
2
3
4
5
INT_PTR CALLBACK DialogProc( HWND hwndDlg,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
);


For WIN32 it shouldn't make any difference whether you return BOOL, INT_PTR, or int
Last edited on
I am calling it from DialogBox and no one of the answers solved the problem...
The extrange thing it's that other dialogs works fine, and they have the same structure! I don't know what's happening, because for some dialogs works, and for other don't.
Thank you again.
I created an empty WIN32 application project, added a menu item to display your dialog using DialogBox, and it just worked for me. No crashes.

(I had to define a couple of structs to get it to compile, and define a simple dialog with an OK and Cancel button, and one EDIT control.)

Maybe the error is elsewhere in you code?

Andy
Yes, I was assuming this was his main window. If it's not, then that's a different story altogether.

Have you tried to return TRUE in EndDialog() instead of FALSE? That is, have you tried --

EndDialog(hDlg,TRUE); instead of FALSE???
Last edited on
Changing the second parameter (probably) won't make a difference; it's just the value you want to be returned by DialogBox. But is it usual practice to return the control id -- e.g. EndDialog(hDlg, IDOK);

(I say probably as I see that DialogBox returns 0 under some error conditions.)
I have tried all the answers that you say and it continues crashing, visual studio says:
Uncontrolled exception in 0x75e48e63 in app.exe: 0xC000041D: An uncontrolled exception was found in a return from a user call.

If we assume that the problem is not from the dialog what kind of things do you think that can affect to this problem?
Thank you andywestken & Lamblion.
I take it that you've tried commenting out the file handling code in your IDOK handler?

While you code ran for me, I wasn't using your real structs or data files. So I suppose there could be some kind of stack corruption going on.

Andy
This is really strange. I have commented everything inside IDOK except EndDialog(hDlg,IDOK); and it continues crashing....
You probably have something in your message que that is conflicting with this. Notice this statement from MSDN relative to the return value you should pass as well as how the dialog box is actually not destroyed until it reaches the message loop --

1
2
3
4
5
6
7
8
9
10
11
12
13
Type: BOOL
 
If the function succeeds, the return value is nonzero.
 
If the function fails, the return value is zero. To get extended error information, call GetLastError.
 
Remarks
 
Dialog boxes created by the DialogBox, DialogBoxParam, DialogBoxIndirect, and DialogBoxIndirectParam functions must be destroyed using the EndDialog function. An application calls EndDialog from within the dialog box procedure; the function must not be used for any other purpose.

A dialog box procedure can call EndDialog at any time, even during the processing of the WM_INITDIALOG message. If your application calls the function while WM_INITDIALOG is being processed, the dialog box is destroyed before it is shown and before the input focus is set.

EndDialog does not destroy the dialog box immediately. Instead, it sets a flag and allows the dialog box procedure to return control to the system. The system checks the flag before attempting to retrieve the next message from the application queue. If the flag is set, the system ends the message loop, destroys the dialog box, and uses the value in nResult as the return value from the function that created the dialog box.

Last edited on
Your problem intrigues me. Can you post your entire RegisterClass() routine as well as your window proc, i.e., the one that is LRESULT CALLBACK and which returns DefWindowProc()?

I suspect that the answer may be found in there.
Here is the whole code.
Thanks for the help.

main.h
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
struct preguntas{

	int correcta;
	char pregunta[2000];
	char respuesta1[2000];
	char respuesta2[2000];
	char respuesta3[2000];
	char respuesta4[2000];
};

struct test{

	int activado;
	char activadoNombre[20];
	char activadoPass[20];

	int idTest; //se añade en la creación
	char creador[20];
	char titulo[80];


	int numeroPreguntas;
	struct preguntas pregunta[100];

};

struct inicio{

	char nombre[20];
	char pass[20];

};

int verificarLogin(FILE *);

int mostrarOpciones(HDC , HWND , PAINTSTRUCT);


main.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <windows.h>
#include "main.h"

HWND hwndpadre;
HINSTANCE hin;

int estado; //1=nada 2=crear 3=hacer
static int flag;
static struct inicio buffer;
int finTest;
int cuenta;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK Login (HWND , UINT , WPARAM , LPARAM );
BOOL CALLBACK Titulo (HWND , UINT , WPARAM , LPARAM );
BOOL CALLBACK Pregunta (HWND , UINT , WPARAM , LPARAM );
BOOL CALLBACK Exito (HWND , UINT , WPARAM , LPARAM );

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
     {
     HWND        hwnd ;
     MSG         msg ;
     WNDCLASSEX  wndclass ;

	 hin = hInstance;

     wndclass.cbSize        = sizeof (wndclass) ;
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(101)) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = "Hola" ;
     wndclass.hIconSm       = LoadIcon (hInstance, MAKEINTRESOURCE(101)) ;

     RegisterClassEx (&wndclass) ;

     hwnd = CreateWindow ("Hola",   // Nombre clase de ventana
                "Patotest - Unipato.com",    // Barra del titulo
                WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,   // Estilo de ventana
                CW_USEDEFAULT,         // Posici�n x inicial
                CW_USEDEFAULT,         // Posici�n y inicial
                800,         // Tama�o x inicial
                600,         // Tama�o y inicial
                NULL,                  // Handle de ventana padre
                NULL,                  // Handle a men� de la ventana
                hInstance,             // Handle de instancia del programa
                NULL) ;                // Par�metros de creaci�n

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

	


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

     }

And more!

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     {
     HDC         hdc,MemDC ;
     PAINTSTRUCT ps ;
     RECT        rect ;
	 FILE *archivo;
	 HBRUSH hbrush;
	 HBITMAP hbm;
	 char nombre[20];
	 int x, y;
	 
	 

     switch (iMsg)
     {

		 case WM_CREATE:

			 flag = 0;
			 

			 hwndpadre = hwnd;

			 archivo = fopen("config.cnf","rb");

			 if(archivo == NULL){

				 verificarLogin(archivo);

			 }

		 return 0;



          case WM_PAINT:

               hdc = BeginPaint (hwnd, &ps) ;

               GetClientRect (hwnd, &rect) ;

			    hbrush = CreateSolidBrush(RGB(255,255,128)); //pintamos el campo válido de posicionamiento
				SelectObject(hdc,hbrush);
				Rectangle(hdc,-2,-2,(rect.right)+2,60);
				DeleteObject(hbrush);

				hbrush = CreateSolidBrush(RGB(148,207,252)); //pintamos el campo válido de posicionamiento
				SelectObject(hdc,hbrush);
				Rectangle(hdc,0,60,rect.right,rect.bottom);
				DeleteObject(hbrush);

				SetBkMode(hdc,TRANSPARENT);
                TextOut(hdc,15,20,"Bienvenido a Patotest 1.0",25);


			    MemDC = CreateCompatibleDC(hdc);
				hbm = LoadBitmap(hin, MAKEINTRESOURCE(106));
				SelectObject(MemDC, hbm);
				BitBlt(hdc, 220, 15, 340, 30, MemDC, 0, 0, SRCCOPY);
				DeleteDC(MemDC);
				DeleteObject(hbm);

				MemDC = CreateCompatibleDC(hdc);
				hbm = LoadBitmap(hin, MAKEINTRESOURCE(107));
				SelectObject(MemDC, hbm);
				BitBlt(hdc, 360, 15, 480, 30, MemDC, 0, 0, SRCCOPY);
				DeleteDC(MemDC);
				DeleteObject(hbm);

				MemDC = CreateCompatibleDC(hdc);
				hbm = LoadBitmap(hin, MAKEINTRESOURCE(108));
				SelectObject(MemDC, hbm);
				BitBlt(hdc, 500, 15, 620, 30, MemDC, 0, 0, SRCCOPY);
				DeleteDC(MemDC);
				DeleteObject(hbm);

				MemDC = CreateCompatibleDC(hdc);
				hbm = LoadBitmap(hin, MAKEINTRESOURCE(105));
				SelectObject(MemDC, hbm);
				BitBlt(hdc, 10, (rect.bottom)-58, 210, (rect.bottom)-10, MemDC, 0, 0, SRCCOPY);
				DeleteDC(MemDC);
				DeleteObject(hbm);

				if(flag == 1){ // opciones

					hbrush = CreateSolidBrush(RGB(174,174,174)); //pintamos el campo válido de posicionamiento
					SelectObject(hdc,hbrush);
					Rectangle(hdc,30,90,770,400);
					DeleteObject(hbrush);

					hbrush = CreateSolidBrush(RGB(223,223,223)); //pintamos el campo válido de posicionamiento
					SelectObject(hdc,hbrush);
					Rectangle(hdc,30,120,770,400);
					DeleteObject(hbrush);


					SetBkMode(hdc,TRANSPARENT);
					TextOut(hdc,40,100,"Opciones de configuración:",25);

					SetBkMode(hdc,TRANSPARENT);
					TextOut(hdc,40,160,"Nombre de usuario:",18);

					SetBkMode(hdc,TRANSPARENT);
					TextOut(hdc,180,160,buffer.nombre,strlen(buffer.nombre));


					SetBkMode(hdc,TRANSPARENT);
					TextOut(hdc,40,200,"Password:",9);


					SetBkMode(hdc,TRANSPARENT);
					TextOut(hdc,120,200,"*****",5);

					MemDC = CreateCompatibleDC(hdc);
					hbm = LoadBitmap(hin, MAKEINTRESOURCE(109));
					SelectObject(MemDC, hbm);
					BitBlt(hdc, 40, 350, 160, 380, MemDC, 0, 0, SRCCOPY);
					DeleteDC(MemDC);
					DeleteObject(hbm);

				}

				if(flag == 2){ // crear


					hbrush = CreateSolidBrush(RGB(174,174,174)); //pintamos el campo válido de posicionamiento
					SelectObject(hdc,hbrush);
					Rectangle(hdc,30,90,770,400);
					DeleteObject(hbrush);

					hbrush = CreateSolidBrush(RGB(223,223,223)); //pintamos el campo válido de posicionamiento
					SelectObject(hdc,hbrush);
					Rectangle(hdc,30,120,770,400);
					DeleteObject(hbrush);


					SetBkMode(hdc,TRANSPARENT);
					TextOut(hdc,40,100,"Creación de test:",17);

					SetBkMode(hdc,TRANSPARENT);
					TextOut(hdc,50,150,"La creación de este test junto con su correspondiente subida a unipato.com le reportará 12 patopuntos.",102);

					MemDC = CreateCompatibleDC(hdc);
					hbm = LoadBitmap(hin, MAKEINTRESOURCE(110));
					SelectObject(MemDC, hbm);
					BitBlt(hdc, 250, 200, 550, 300, MemDC, 0, 0, SRCCOPY);
					DeleteDC(MemDC);
					DeleteObject(hbm);

					TextOut(hdc,50,370,"La cantidad de patopuntos puede variar desde el lanzamiento de esta versión del programa.",89);

				}

				if(flag == 3){ // hacer


					hbrush = CreateSolidBrush(RGB(174,174,174)); //pintamos el campo válido de posicionamiento
					SelectObject(hdc,hbrush);
					Rectangle(hdc,30,90,770,400);
					DeleteObject(hbrush);

					hbrush = CreateSolidBrush(RGB(223,223,223)); //pintamos el campo válido de posicionamiento
					SelectObject(hdc,hbrush);
					Rectangle(hdc,30,120,770,400);
					DeleteObject(hbrush);


					SetBkMode(hdc,TRANSPARENT);
					TextOut(hdc,40,100,"Lista de tests disponibles:",27);


				}


               EndPaint (hwnd, &ps) ;
               return 0 ;




		  case WM_LBUTTONDOWN:

			  x = LOWORD (lParam);
			  y = HIWORD (lParam);

			   if( x>220 && x<340 && y>15 && y< 40){ // crear test

				   flag = 2;
			   }

			   if( x>360 && x<480 && y>15 && y< 40){ // hacer test

				   flag = 3;
			   }

			   if( x>500 && x<620 && y>15 && y< 40){ // opciones

				   archivo = fopen("config.cnf","rb");

				   fread(&buffer,sizeof(struct inicio),1,archivo);

				   if(&buffer == NULL){
					   return 0;
				   }

				   fclose(archivo);

				   flag = 1;

			   }

			   if( x>40 && x<160 && y>350 && y< 380 && flag == 1){ //editar datos

				   DialogBox(hin,MAKEINTRESOURCE(102),hwndpadre,Login);

			   }

			   if( x>250 && x<550 && y>200 && y< 300 && flag == 2){ //nuevo test
				   
				   DialogBox(hin,MAKEINTRESOURCE(111),hwndpadre,Titulo);

				   finTest = 0;
				   cuenta = 0;

				   while(finTest == 0){

					    DialogBox(hin,MAKEINTRESOURCE(112),hwndpadre,Pregunta);

						cuenta++;
				   }

				   DialogBox(hin,MAKEINTRESOURCE(113),hwndpadre,Exito);

			   }

			   InvalidateRect(hwnd,NULL,FALSE);

		  return 0;



          case WM_DESTROY:

               PostQuitMessage (0) ;

          return 0 ;
     }

     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}


int verificarLogin(FILE *archivo){

	 DialogBox(hin,MAKEINTRESOURCE(102),hwndpadre,Login);

	return 0;
}



 BOOL CALLBACK Login (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam){
		 HWND handler;
		 int error;
		 char user[20];
		 char passwd[20];
		 int usercrypt;
		 int passcrypt;
		 FILE *archivo;
		 struct inicio conf;

		 switch(iMsg){

		 case WM_INITDIALOG:


		 return TRUE;

		 case WM_COMMAND:

			 switch(LOWORD (wParam)){


				case IDOK:

						GetDlgItemText(hDlg,1001,user,20);  //Guardamos Nombre de jugador
						if( 0 == strcmp(user,"")){
							error = 1;
						}

						GetDlgItemText(hDlg,1002,passwd,20);  //Guardamos Nombre de jugador
						if( 0 == strcmp(passwd,"")){
							error = 1;
						}

						archivo = fopen("config.cnf","wb");

						 if(archivo == NULL){
							 verificarLogin(archivo);
						 }

						 strcpy(conf.nombre,user);
						 strcpy(conf.pass,passwd);

						 fwrite(&conf,sizeof(struct inicio),1,archivo);
						 
						 fclose(archivo);

						 EndDialog(hDlg, 0);

				return TRUE;

				case IDCANCEL:

						if(flag == 0){
							PostQuitMessage (0) ;
							EndDialog (hDlg, 0);
						}
						else{
							EndDialog(hDlg, 0);
						}

				return TRUE;

			 }

			 break;

		 }

		 return FALSE;

	 }



Last part!

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
 BOOL CALLBACK Pregunta (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam){
		 HWND handler;
		 int error;

		 int correcta;
		 char pregunta[2000];
		 char respuesta1[2000];
		 char respuesta2[2000];
		 char respuesta3[2000];
		 char respuesta4[2000];

		 FILE *archivo;
		 struct inicio conf;
		 time_t hora;
		 struct test nuevo;
		 FILE *file;



		 switch(iMsg){

		 case WM_INITDIALOG:


		 return TRUE;

		 case WM_COMMAND:

			 switch(LOWORD (wParam)){


				case IDOK: // anadir pregunta

						GetDlgItemText(hDlg,1001,pregunta,20);  //Guardamos Nombre de jugador
						if( 0 == strcmp(pregunta,"")){
							error = 1;
						}

						GetDlgItemText(hDlg,1002,respuesta1,20);  //Guardamos Nombre de jugador
						
						GetDlgItemText(hDlg,1007,respuesta2,20);  //Guardamos Nombre de jugador
						
						GetDlgItemText(hDlg,1008,respuesta3,20);  //Guardamos Nombre de jugador
						
						GetDlgItemText(hDlg,1009,respuesta4,20);  //Guardamos Nombre de jugador
						

						

						 archivo = fopen("time.pato","r+b");
						 if(archivo == NULL){
							 return 0;
						 }


						 fread(&conf,sizeof(struct inicio),1,archivo);
						 
						 strcpy(nuevo.pregunta[cuenta].pregunta,pregunta);
						 strcpy(nuevo.pregunta[cuenta].respuesta1,respuesta1);
						 strcpy(nuevo.pregunta[cuenta].respuesta2,respuesta2);
						 strcpy(nuevo.pregunta[cuenta].respuesta3,respuesta3);
						 strcpy(nuevo.pregunta[cuenta].respuesta4,respuesta4);
						
						 fwrite(&nuevo,sizeof(struct inicio),1,archivo);
						 
						 fclose(archivo);


						 EndDialog (hDlg, 0);

				return TRUE;

				case IDCANCEL:
							
							finTest = 1;

							EndDialog (hDlg, 0);

				return TRUE;

			 }

			 break;

		 }

		 return FALSE;

	 }

 BOOL CALLBACK Titulo (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam){
		 HWND handler;
		 int error;
		 int valor;
		 char user[20];
		 char titulo[80];
		 FILE *archivo;
		 struct inicio conf;
		 struct test nuevo;
		 char buff[12];
		 FILE *file;
		 time_t hora;


		 switch(iMsg){

			 case WM_INITDIALOG:


			 return TRUE;

			 case WM_COMMAND:

				 switch(LOWORD (wParam)){


					case IDOK:

							GetDlgItemText(hDlg,1001,titulo,80);  //Guardamos Nombre de jugador
							if( 0 == strcmp(user,"")){
								error = 1;
							}

							hora = time(NULL);
							itoa(hora,buff,10);

							 archivo = fopen(buff,"wb");

							 if(archivo == NULL){
								 return 0;
							 }

							 file = fopen("config.cnf","rb");
							 if(file == NULL){
								 return 0;
							 }

							 fread(&conf,sizeof(struct inicio),1,file);
						 
							 strcpy(nuevo.titulo,titulo);
							 strcpy(nuevo.creador,conf.nombre);
							 nuevo.activado = 0;

							 fclose(file);

							 fwrite(&nuevo,sizeof(struct test),1,archivo);
						 
							 fclose(archivo);


							 error = 0;

							 EndDialog(hDlg,IDOK);
						 
					return TRUE;

					case IDCANCEL:

							 EndDialog(hDlg,IDCANCEL);

					return TRUE;

				 }

				 break;

		 }

		 return FALSE;

	 }

  BOOL CALLBACK Exito (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam){
		 HWND handler;

		 switch(iMsg){

		 case WM_INITDIALOG:


		 return TRUE;

		 case WM_COMMAND:

			 switch(LOWORD (wParam)){


				case IDOK:

						 EndDialog (hDlg, 0);

				return TRUE;

			 }

			 break;

		 }
		 return FALSE;
	 }
Well, I confess that I don't see anything that would cause your problem. Maybe one of the more experienced programmers here can come up with something.
The reason it's crashing is that you're returning TRUE. When you return TRUE you are expected to return a custom value via SetWindowLongPtr(DWL_MSGRESULT, ...). If you aren't doing so, you should return FALSE.

(lol, at least that's what I think it is)
Returning TRUE - in the general case - just means that you processed a message.

Not setting the windows long will just leave it the same value as it was before. WM_COMMAND doesn't care about it; returning TRUE in response to WM_INITDIALOG instructs the system to set the default keyboard focus (you return FALSE if you've done this yourself)
Last edited on
greetings: I am using c and Petzold's Programming Windows to create dynamically allocated dialog boxes.

I don't have visual studio so I do everything by hand.
My problem and solution seems similar and I have found useful information on this site so maybe I can give something back.

voodark I had a similar problem.
I think the problem is in the byte alignment. Somewhere in Petzold's Programming Windows, he talks about the importance of the information Windows is looking for being EXACTLY where windows expects it to be.

[I just copied and pasted the code as is from a working program]
==code start
//===========================Define a dialog box.
lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU
| DS_MODALFRAME | WS_CAPTION | CS_OWNDC ;
//how many child boxes in this Parent Box
lpdt->cdit = no_flds() * 2 + 1; // number of controls in box +1 for INS:
//upper LT corner pf box is here
lpdt->x = cxChar;
lpdt->y = cxChar;
//make wider
lpdt->cx = width;
//make taller
lpdt->cy = taller;
//this box id
lpw = (LPWORD) (lpdt + 1);
*lpw++ = 0; // no menu
*lpw++ = 0; // predefined dialog box class (by default)
lpwsz = (LPWSTR) lpw;
nchar = 1+ MultiByteToWideChar (CP_ACP, 0, tname, -1,
lpwsz, 50);
lpw += nchar;
==code end

I defined 'tname' as

char *tname

==
This function call makes the program crash...
init_screen("Dynamic BAT file", sz_els, sc, db)

This function call runs...
init_screen("re BAT file", sz_els, sc, db)

==code start
void init_screen(name, els, bfr, maskbfr)
char *name;
int *els;
char *bfr;
char *maskbfr;
{
tname = name; //name on top
sz_els = els; //the element list
bf_sc = bfr; //beginning of the raw data buffer sc
maskbf_db = maskbfr; //beginning of the screen/mask/data buffer db
}
==code end

The MultiByteToWideChar(...) is telling the dialog box to put those characters as the dialog box title in the upper left corner of the DiagBox box.
There are too many chars in the "Dynamic BAT file" title and this causes the byte alignment to be off.

The alignment problem happens with this line

lpw += nchar;
Topic archived. No new replies allowed.