Simple edit dialog box
I am modifying a program that runs in Windows that I would like to input a couple of values into as it starts.
At the beginning of AppMain, the following pre-existing code allows the user to enter a filename:
char our_file_name[260] = "TEST";
#ifdef WIN32
edit_dialog(NULL,"Create File", "Enter file name:", our_file_name,260);
#endif
This all seemed rather straightforward so I thought I'd just recreate this f开发者_运维知识库or my (signed) integer values, with the following code, inserted immediately following the above code:
#ifdef WIN32
edit_dialog(NULL,"Custom tolerance", "Enter tolerance:", tolerance,260);
#endif
#ifdef WIN32
edit_dialog(NULL,"Custom position", "Enter intended position:", position,260);
#endif
... And the following placed with the other variable declarations:
int tolerance = 400;
int position = 0;
The code compiles just fine, but when I run the program, the filename section works just as it should but the program crashes as soon as this new bit starts to run.
What am I doing wrong? Is there a better method of inputting a couple of values?
The signature for the edit_dialog
function probably takes a char* as it's 4th parameter. You are passing an int there when you call it with tolerance
, so your code is treating the value of the int (in this case 400) as a pointer value (0x400) and going to look at the string located at the address 0x400... Boom. Crash.
You need to write the integer value to a character buffer before you pass it in to the edit_dialog
function.
char buf[256];
sprintf( buf, "%d", tolerance);
#ifdef WIN32
edit_dialog(NULL,"Custom tolerance", "Enter tolerance:", buf, 260);
#endif
(I bet edit_dialog is a macro, because I'm pretty sure most compilers would catch this error at compile time and warn you).
Then when your edit dialog comes back, it will store the characters the user typed back into buf which you'll probably want to convert to an int.
tolerance = atoi(buf);
精彩评论