[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

Help Fix Problem With This Code

I have a dialog based application. It contains edit boxes that hold integer variables with range checking (0-65535).  
If you load a data file and then edit one of the edit boxes to outside the range it was intended, and click the patch button, the dialog will prompt you to enter a valid integer between (0-65535), However the program does not halt on waiting for the user to enter a valid integer it continues running the code in the OnPatchGame function.  It writes the invalid data to the file.  After this code is completed it then allows the user to fix the variable.  The user then hits the PATCH button again and now I receive Invalid File Handle errors because the file handle was closed before the user could fix the variable.  What I would like it to do is make sure that all the data is within the valid ranges before attempting to write the data to the file.  How can I recitfy this situation?

Code is listed below.


void CLORDSEDDlg::OnPatchGame()
{
// TODO: Add your control notification handler code here
//Open Data File -- Save Game
   int Result;
   DWORD dwPointer;
   DWORD dwBytesWritten;

   UpdateData(TRUE); //This is where the program
                     //notifys me that a value is out
                     //of Range

///Write Wood//////

//There are atleast 10 of these seperate writes I only
//included Two to save space and they are all identical
//Except for the byte offsets and variable names.

   dwPointer=SetFilePointer(File_In,84888,NULL,FILE_BEGIN);
   if (dwPointer == 0xFFFFFFFF) {
     Display_Error_Message();
   }
   Result=WriteFile(File_In,&m_Wood,sizeof(m_Wood),
                    &dwBytesWritten,NULL);
   if (Result==0)
     Display_Error_Message();

   //The Display_Error_Function just calls GetLastError and
   //Displays The Error In A Message Box

///////////Write Swords////////
   dwPointer=SetFilePointer(File_In,84912,NULL,FILE_BEGIN);
   if (dwPointer == 0xFFFFFFFF) {
     Display_Error_Message();
                        }
   Result=WriteFile(File_In,&m_Swords,sizeof(m_Swords),
                    &dwBytesWritten,NULL);
   if (Result==0)
     Display_Error_Message();



  CloseHandle(File_In); //Closes The File Handle
  OnOK();         //Should Exit The Program And Close
                  //The Dialog.
      
}
0
larockd
Asked:
larockd
  • 5
  • 3
  • 2
1 Solution
 
larockdAuthor Commented:
Edited text of question
0
 
larockdAuthor Commented:
Edited text of question
0
 
q2guoCommented:
How did you invoke your CLORDSEDDlg dialog box?
Is it modal or modaless?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
q2guoCommented:
Sorry, above meant to be comments
       
0
 
larockdAuthor Commented:
It is a modeless dialog box.  It is a dialog based application and the CLORDSEDDlg dialog is the applications main dialog.  
0
 
q2guoCommented:
The prompt to enter a valid integer between (0-65535) dialog
box should be model.
0
 
larockdAuthor Commented:
I am not sure what you are getting at.  The (DDV) Dialog Data Validation code is all built in to MFC.
0
 
RONSLOWCommented:
You haven't checked the return value from UpdateData .. check it before further processing.  If it returns false, then don't do anything.
0
 
larockdAuthor Commented:
Ok so I checked the return value and it's false.  What I did was encapsulate all the code in an if statement...

      if(UpdateData(TRUE)!=0) {
         ........
         }

Now instead of having to place all the code in the function in an if statement is there a way to just return to the dialog?

What is the advantage of doing it this way or setting up EN_CHANGE messages for each edit box?


0
 
RONSLOWCommented:
well you could say

if (! UpdateData(TRUE)) return;

returning from the function will return you to the dialog.  The dialog stay there until the OnOK (which calls EndDialog)

The advantage of this way is that the DDX/DDV mechanism makes specifying the checks easier - there is less code for you to write.  It is not necessarily trivial to handle EN_CHANGE and check for values etc. then display a message and set your focus back to the control that had the problem.


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now