Solved

Help Fix Problem With This Code

Posted on 1998-02-08
10
157 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
Comment Utility
Edited text of question
0
 

Author Comment

by:larockd
Comment Utility
Edited text of question
0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
How did you invoke your CLORDSEDDlg dialog box?
Is it modal or modaless?
0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
Sorry, above meant to be comments
       
0
 

Author Comment

by:larockd
Comment Utility
It is a modeless dialog box.  It is a dialog based application and the CLORDSEDDlg dialog is the applications main dialog.  
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

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

Author Comment

by:larockd
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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. …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now