Solved

Help Fix Problem With This Code

Posted on 1998-02-08
10
159 Views
Last Modified: 2010-04-10
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
Comment
Question by:larockd
  • 5
  • 3
  • 2
10 Comments
 

Author Comment

by:larockd
ID: 1181278
Edited text of question
0
 

Author Comment

by:larockd
ID: 1181279
Edited text of question
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1181280
How did you invoke your CLORDSEDDlg dialog box?
Is it modal or modaless?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 3

Expert Comment

by:q2guo
ID: 1181281
Sorry, above meant to be comments
       
0
 

Author Comment

by:larockd
ID: 1181282
It is a modeless dialog box.  It is a dialog based application and the CLORDSEDDlg dialog is the applications main dialog.  
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1181283
The prompt to enter a valid integer between (0-65535) dialog
box should be model.
0
 

Author Comment

by:larockd
ID: 1181284
I am not sure what you are getting at.  The (DDV) Dialog Data Validation code is all built in to MFC.
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1181285
You haven't checked the return value from UpdateData .. check it before further processing.  If it returns false, then don't do anything.
0
 

Author Comment

by:larockd
ID: 1181286
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1181287
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question