0

I have been recently constructing a program. The end goal is to get what the user has written in the Fl_Input box and write it into my database. To do this I am going to need a callback on a button that allows me to at least get the string value of an Fl_Input->value()

The expected results is that the contents of the Fl_Input will be outputted in the console window (but eventually they will be written to a database). The actual results that I am getting is an error code stating that the program is trying to write to secure/corrupted memory it is not too clear so I will paste below

An unhandled exception of type 'System.AccessViolationException' occurred in Start.exe Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

As eventually I would like to have multiple of these Fl_Inputs as strings and store them in some sort of vector I will leave that part of the code in as well but it is commented.

struct Info
{
    // The widgets
    Fl_Input* instr;
    Fl_Int_Input* inint;

    // Saved values
    char sval[40];
    int  ival;
};

// Callback for the done button
void done_cb(Fl_Widget* w, void* param)
{
    Info* input = reinterpret_cast<Info*>(param);

    // Get the values from the widgets
    //const char* l = input->instr->value();
    //auto my_cstr = l;
    //std::string s(my_cstr);

    strcpy_s(input->sval, input->instr->value());
    input->ival = atoi(input->inint->value());

    // Print the values
    printf("String value is %s\n", input->sval);
    printf("Integer value is %d\n", input->ival);
}

void signupScreen (void) {
    //Fl_Input *fname, *lname, *username, *password, *dob;
    //Fl_Window *w = new Fl_Window(800, 800, "signup");
    //fname = new Fl_Input(200, 100, 500, 30, "Enter first name:");
    //lname = new Fl_Input(200, 150, 500, 30, "Enter last name:");
    //username = new Fl_Input(200, 200, 500, 30, "Enter username wanted:");
    //password = new Fl_Input(200, 250, 500, 30, "Enter password wanted:");
    //dob = new Fl_Input(200, 300, 500, 30, "Enter date of birth in format dd/mm/yyyy:");
    //std::vector<std::string> v; //= { string(fname->value()), string(lname->value()), string(username->value()), string(password->value()), string(dob->value()) };
    Info input;

    // Create the window
    Fl_Window *window = new Fl_Window(200, 150);
    int x = 50, y = 10, w = 100, h = 30;
    input.instr = new Fl_Input(x, y, w, h, "Str");
    input.instr->tooltip("String input");

    y += 35;
    input.inint = new Fl_Int_Input(x, y, w, h, "Int");
    input.inint->tooltip("Integer input");

    y += 35;
    Fl_Button* done = new Fl_Button(x, y, 100, h, "Done");
    done->callback(done_cb, &input);
    window->end();


    window->show();

}

EDIT: Remove line of code

  • There aren't any C++/CLR strings in the code you have posted. To convert as per your title, have a look at https://support.microsoft.com/en-gb/help/311259/how-to-convert-from-system-string-to-char-in-visual-c. – cup May 22 '20 at 21:20
  • Okay I will edit the tag and the title it is there because their will be as this is a sign up form that uses CLR to interact with a database. EDIT: the link posted is for converting string to char*. I want the reverse. As I have solved this part of the problem if you wish to help I refer you to https://stackoverflow.com/questions/28441540/fltk-getting-value-from-input-on-button-release?rq=1 If you have any questions as to how I solved this problem please feel free to send me a message @cup - NP00 – NewProgrammer00 May 22 '20 at 22:35

1 Answers1

1

I suspect your Info object is going out of scope (since it is allocated on the stack) before the callback. Try making it static/global instead.

Turix
  • 4,470
  • 2
  • 19
  • 27