Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help Fix Problem With This Code

Posted on 1998-02-08
10
Medium Priority
?
166 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

927 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