2

I just got tossed on a project written in C++ on VS. I have a dialog box with Edit Controls where a user enters a MAC address. I would like to save this info when an "on click event" occurs. This is so that when the application exits, this is saved and can be loaded again. I would naturally save this to file and use basic file I/O.

Question is, "What is the most efficient way to do this?". My researched ended up in "DialogResult" property, but I could not find this property in VS 2015

https://msdn.microsoft.com/en-us/library/65ad5907(v=vs.100).aspx

All help greatly appreciated,

-SDEV

  • 1
    Welcome to stackoverflow, the q&a site for programming questions -- Your question does not look like a programming question as defined in the [help section](http://stackoverflow.com/help) – Soren May 06 '16 at 19:27

1 Answers1

0

This is usually done using the application's "Profile" storage, which used to be in an .INI file, but is now in the registry, under the application's Software key.

In an MFC app's InitInstance(), there is usually an auto-generated call to SetRegistryKey. This takes an identifying string and sets up the app's profile storage. So, if your app is missing this, you need to add something like SetRegistryKey(_T("MyCompanyGroupOrAppName"));.

Then there is an API for storing and retrieving information. For Edit control text, you probably want to save it with CWinApp::WriteProfileString. So in your handler, you would get the text from the edit control into a CString, then save it with

AfxGetApp()->WriteProfileString(_T("SectionName"), _T("MacAddress"), MyMacAddressString);

To get the string back later, use something like:

CString MyMacBeforeTheCrash = pApp->GetProfileString(_T("SectionName"), _T("MacAddress"));

Here are some links to the documentation:

It seems like you might not be sure how to access text from an edit control. There are several ways to do this, but it depends on how your dialog is set up.

If your edit control has a CString variable associated with it, with DDX (Dialog Data Exchange), then you call your dialog class' member function: UpdatData(TRUE); and after that, its CString variable will contain the current text. To save a value, you assign the new text value to the variable, then call UpdateData(FALSE);.

If you have access to a class variable for the edit control (a CEdit object), you can copy the edit control's text to a CString with MyEditControl.GetWindowText(MyString);. To copy a string's text into an edit control, do: MyEditControl.SetWindowText(MyString);.

If there isn't any kind of variable for your edit control, you might want to add one by going into your dialog's Resource Editor, right clicking on the edit control, then choosing "Add Variable" from the menu. You will get to choose whether you want to add a control object (a CEdit), or something like a CString.

You can also access an edit control's text using the non-MFC Win32 API -- this involves first somehow obtaining the control's window handle, then using ::SendMessage to send WM_GETTEXT and WM_SETTEXT messages to the control.

Good luck.

Christopher Oicles
  • 3,017
  • 16
  • 11