943,678 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1526
  • C++ RSS
Jul 3rd, 2008
0

damages when executing console from MFC

Expand Post »
hi
i created MFC dialog based application and in an event of a button i want to execute a C++ console application .
i've tried severl functions such as shellexecute and create process ....but
it executes it but then return the wrong answers .....where if i run the application console by itself ....it gave me the right calculations ....
please i neeeed this so bad....my project is supposed to be delieverd in satureday MORNING(one day left)
pleeeeeease
thank u all
Reputation Points: 10
Solved Threads: 0
Light Poster
lahom is offline Offline
42 posts
since Apr 2008
Jul 3rd, 2008
0

Re: damages when executing console from MFC

What do you mean by 'it returns the wrong answers'? How are you returning data from the console application?
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jul 3rd, 2008
0

Re: damages when executing console from MFC

by database
from console to database
from database to MFC
Reputation Points: 10
Solved Threads: 0
Light Poster
lahom is offline Offline
42 posts
since Apr 2008
Jul 3rd, 2008
0

Re: damages when executing console from MFC

That still isn't anywhere near enough information. What DB are you using? What commands are you using to access it? What software library are you using to interface with it?
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jul 4th, 2008
0

Re: damages when executing console from MFC

ok ...
to be more clear ....what my program does is :
1- open acertain 2 files in 2 editboxes(through a browse buttons).
2-the content of the editboxes is saved in 2 files called file1, file2 .
3-then i press a button where it should execute a console applicatin to compare between file1 and file2 .
the problem :
if i did the last 3 steps its doesnot give the right answer all the time ...AND if i run the console application with the previous file1, file2 it doesnot give the right answer either.
BUT
if i run the console application elsewhere with the original names of the files chosen in the browse button , it gave me the right answer ..
so i guess the problem in files or what????
HELP
Reputation Points: 10
Solved Threads: 0
Light Poster
lahom is offline Offline
42 posts
since Apr 2008
Jul 4th, 2008
0

Re: damages when executing console from MFC

ok
this is the code for the browse button:
C++ Syntax (Toggle Plain Text)
  1. //**BROWSE DIALOG 1**//
  2. //******************************************************//
  3. char strFilter[] = "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||";
  4.  
  5. CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
  6.  
  7. if (m_ldFile.DoModal() == IDOK) //Start File dlg box
  8. {
  9. m_sFileName=m_ldFile.GetPathName(); //Get file name
  10.  
  11. FILE *fp=fopen(m_sFileName,"rb"); //Open file for reading
  12.  
  13. if( !fp )
  14. {
  15. DWORD dwError = GetLastError();
  16. CString sError;
  17. sError.Format("Open Error: %d, %d\n", errno, dwError);
  18. AfxMessageBox(sError);
  19. return;
  20. }
  21. // Get the file size
  22.  
  23. int nFileLong;
  24. {
  25. fseek(fp,0,SEEK_END); //Go to file end
  26. nFileLong=ftell(fp); //Get length
  27. fseek(fp,0,SEEK_SET); //Go to file start
  28. }
  29.  
  30.  
  31. char* sText = new char[nFileLong+1]; //reserve string space
  32.  
  33. int i=fread(sText,1,nFileLong,fp); //Read the characters
  34.  
  35. sText[i]=0; //Set string terminating null
  36.  
  37. fclose(fp); //Close file
  38.  
  39. // Make a copy
  40.  
  41. {
  42. // FILE *file1 = fopen("C:\\Program Files\\C++ Clone Detector\\file1.txt","w+"); //Create txt file for saving editbox content
  43.  
  44. if( !file1 )
  45. {
  46. DWORD dwError = GetLastError();
  47. CString sError;
  48. sError.Format("Copy Error: %d, %d\n", errno, dwError);
  49. AfxMessageBox(sError);
  50. }
  51. /*else
  52. {
  53. fwrite(sText,1,nFileLong,file1); //print the content of the editbox into the file
  54. fclose(file1); //Close file2
  55. }
  56. */
  57. }
  58.  
  59. m_EDIT1=sText; //Put text in Edit box's variable
  60.  
  61. fwrite(sText,1,nFileLong,file1); //print the content of the editbox into the file
  62. fclose(file1); //Close file2
  63.  
  64. delete [] sText;
  65.  
  66. UpdateData(FALSE); //Force data to go to Edit control
  67. }
the same goes for file2.
file1 and file2 are declared as:
C++ Syntax (Toggle Plain Text)
  1. file1 = fopen("C:\\Program Files\\C++ Clone Detector\\file1.txt","w+"); //Create txt file for saving editbox content
  2. file2 = fopen("C:\\Program Files\\C++ Clone Detector\\file2.txt","w+"); //Create txt file for saving editbox content

and when i press the button :
C++ Syntax (Toggle Plain Text)
  1. ShellExecute(this->m_hWnd,"open","C:\\Program Files\\C++ Clone Detector\\Debug\\proto1.exe","","", SW_HIDE );

and in the console application the file1 , file2 are declared :
C++ Syntax (Toggle Plain Text)
  1. ifstream infile ("C:\\Program Files\\C++ Clone Detector\\file1.txt");
  2. ifstream infile2 ("C:\\Program Files\\C++ Clone Detector\\file2.txt");
Reputation Points: 10
Solved Threads: 0
Light Poster
lahom is offline Offline
42 posts
since Apr 2008
Jul 4th, 2008
0

Re: damages when executing console from MFC

Ah, I think I know what is happening.

The file stream is not required to keep the actual disk file up-to-date with the fstream's state at all times. It can wait until it is convenient to write data to the file.

So it looks to me like there is a race condition occurring between the two applications to access the file data.

You can force it to synchronize the disk file and the fstream's data buffer by using the flush function/manipulator, and for FILE*s use fflush().

So, when the button is clicked, the following should happen:
  1. Make sure to fflush before calling ShellExecute().
  2. Remember that ShellExecute starts the indicated process but does not wait for it to terminate. You must use one of the wait functions to wait for the program to terminate. For example:
    C++ Syntax (Toggle Plain Text)
    1. bool ExecTheChildProc()
    2. {
    3. // Both files should be flushed or closed before calling this function
    4.  
    5. // Execute the child process...
    6. HINSTANCE hChild = ShellExecute( ... );
    7.  
    8. // ...and wait for it to terminate
    9. // (You can specify a specific number of milliseconds to wait
    10. // before returning so that you can get control back periodically
    11. // and make sure your application doesn't freeze.)
    12. DWORD result;
    13. while (true) switch (WaitForSingleObject( hChild, 500 ))
    14. {
    15. case WAIT_OBJECT_0:
    16. // Success. The child is terminated
    17. // and the files are ready to be read.
    18. return true;
    19.  
    20. case WAIT_TIMEOUT:
    21. // Half a second has passed.
    22. // Make sure the application stays responsive.
    23. ProcessMessages();
    24. break;
    25.  
    26. default:
    27. // Something has gone wrong.
    28. return false;
    29. }
    30.  
    31. return false; // keep the compiler happy
    32. }
  3. Read the result.

Call the ExecTheChildProc() when the button is pressed (or put the code in your button event method).

The ProcessMessages() function is not defined by MFC. However, you can find it here.

If you don't expect the child to take much time, you can skip the loop and everything and just say:
return WaitForSingleObject( hChild, INFINITE ) == WAIT_OBJECT_0;

That's a zero on the end of the WAIT_OBJECT_0 macro.

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: quick question: how to 'null' blocks of an unsigned char array
Next Thread in C++ Forum Timeline: Is this legal?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC