• 12
• 12
• 9
• 10
• 13

# MessageBox puzzler

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

## Recommended Posts

I have some temp messageboxes here and there to catch bad filenames and such... later to be replaced by something more robust. It worked fine before. Today however the box doesnt popup, just the usual windows popup beep, and the program exits. There is an exit() call so its supposed to quit... that is.. its *supposed* to quit after OK is clicked on the box that has mysteriously vanished ;o) Any thoughts on what would prevent the box from appearing? std::string one = "Could not load: \n"; std::string two = std::string(filename); std::string three = one + two; MessageBox(NULL,three.c_str(),"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); exit(0);

##### Share on other sites
This will happen if you are running the program as a service, under users other than the 'Interactive User' iirc.

If that's not it, try making a complete sample program from scratch that's just big enough to demonstrate the problem, and no bigger.

##### Share on other sites
I should have also added: It does still work in other areas. Simple cut/paste of the same code... such as in the beginning of main. Not sure how this would happen but perhaps an 'enter' keypress is stored as input someplace and applied to the box? Never heard of that before but wouldnt surprise me. After this function I also have one to load a MilkShape model, at the moment commented out. If I comment out the loadtexture function instead, the same missing box. Correct filenames do not cause errors so the code with the boxes are bypassed, thus the program does not exit.

void LoadTexture(char filename[24], int ArrayNumber){  char filepath[8] = "data/";  strcat(filepath, filename);  ILuint texid;  ilGenImages(1, &texid);  ilBindImage(texid);  if (ilLoadImage(filepath))  {    //ArrayNumber changed to 0 for test. Same result using either    texture[0] = ilutGLBindTexImage(); // IL image to OpenGL  }  else  {    //Error    std::string one = "Could not find: \n";    std::string two = std::string(filename);    std::string three = one + two;    MessageBox(NULL,three.c_str(),"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);    ilDeleteImages(1, &texid);    exit(0);  };  ilDeleteImages(1, &texid);};

##### Share on other sites
Here is one obvious buffer overrun bug:
  char filepath[8] = "data/";  strcat(filepath, filename);
I'd suggest making filepath a lot bigger. The strnxxx functions are not a silver bullet either. What you want are the MS's new strxxx_s functions.[cool]I doubt that this is an isolated incident. You may wish to search for other possible buffer overruns.

##### Share on other sites
It's kind of funny that you made that mistake considering your code to print out errors clearly demonstrates that you know one way to avoid the problem (using std::string to keep track of the buffer for you).

##### Share on other sites
Change the filename/path to strings... and convert where needed as in the error code? I only just recently learned how to use them, and havent gone through everything to see if they could be used elsewhere. Sounds like a good opportunity to do so.

note: 'filepath' made no difference whether I used [7] (the minimum allowed), 8, 256, or anything in between.

At the moment I am looking to track down out some 'ambiguous symbol' errors after changing fstream.h to simply fstream. Got some of them, but there are more somewhere. But I might just change them back for now and deal with it after this one is fixed fixed. [edit: missed one ] Fun ;o)

##### Share on other sites
Have you tried running this code through your debugger? That should tell you exactly where it's going pear shaped.

edit - where's my "where?"

[Edited by - Dave Hunt on September 6, 2005 3:18:38 PM]

##### Share on other sites
Well... first off using strings works just fine. A new thing learned, thanks ;o)

The box problem remains however. Doing more cutting/pasting tests there is the SDL call for setting the video mode where one simple box before it works, but the one after it does not even when the filename is correct. Not sure what I did to cause this. Curiouser and curiouser.

##### Share on other sites
MessageBox can fail due to a variety of conditions. Add some code to check for MessageBox failing and run it under a debugger as Dave Hunt already suggested.

##### Share on other sites
Thanks for the link. Running the debugger was one of the first things I did after I didnt see anything obvious. Normally, once a box is opened it is not possible to step over into the next line until it is closed, or so it seems. However this does not happen for the second box. Next, see what the return value is.
-----------------
edit: The return code for the boxes say (before and after):
Error code: (null)
Error code: Fatal signal: Segmentation Fault (SDL Parachute Deployed)

//the code
int temp = MessageBox(NULL,"after","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
fprintf(stderr, "Error code: %s\n", temp);

if(!(Screen = SDL_SetVideoMode(ScreenWidth, ScreenHeight, 0, Flags)))
return 0;

temp = MessageBox(NULL,"after","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
fprintf(stderr, "Error code: %s\n", temp);

So... I guess now I know where to look. ;o)

[Edited by - Sammy0037 on September 6, 2005 5:05:28 PM]