Solved

Help Fix Problem With This Code

Posted on 1998-02-08
10
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 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