Why am I not able to get handle of file?
-
Hello, I've a code snippet. Please follow the comments.
//Create a directory if it doesn't exist if ( !PathFileExists("C:\\\\TestDir") ) { CreateDirectory("C:\\\\TestDir", NULL); } //Create a batch file in the directory CFile cfBatchFile( "C:\\\\TestDir\\\\TestBatch.bat", CFile::modeCreate| CFile::modeRead | CFile::modeWrite ); //Write 'C:\\\\windows\\\\system32\\cmd.exe" ipconfig>C:\\\\TestDir\\\\Test.txt in the batch file cfBatchFile.Write( "\\"C:\\\\windows\\\\system32\\cmd.exe\\" ipconfig>C:\\\\TestDir\\\\Test.txt", MAX\_PATH ); cfBatchFile.Close(); //ShellExecute runs "C:\\\\TestDir\\\\TestBatch.bat" and generates output for 'ipconfig' and redirects it to text file "C:\\\\TestDir\\\\Test.txt" if ( ShellExecute(NULL, "open", "C:\\\\TestDir\\\\TestBatch.bat", NULL, NULL, SW\_HIDE) > (HINSTANCE)32) { CFile\* pcfTxt = NULL; //Trying to open 'C:\\\\TestDir\\\\Test.txt' for reading BUT CRASH HAPPENS HERE (unhandled Exception) pcfTxt = new CFile(\_T("C:\\\\TestDir\\\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); INT nLogTxtLen = pcfTxt->GetLength(); }
In Debug mode (without a Debug point), the crash is shown at pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); But if I place a debug point at : pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); and then continue, no crash arises. I guess the issue is something to do with the handle of 'C:\\TestDir\\Test.txt' file. How could I rectify this. Is some Sleep() required to wait for the handle to be available. Replies would be appreciated. Thanks Edit/Delete Message
-
Hello, I've a code snippet. Please follow the comments.
//Create a directory if it doesn't exist if ( !PathFileExists("C:\\\\TestDir") ) { CreateDirectory("C:\\\\TestDir", NULL); } //Create a batch file in the directory CFile cfBatchFile( "C:\\\\TestDir\\\\TestBatch.bat", CFile::modeCreate| CFile::modeRead | CFile::modeWrite ); //Write 'C:\\\\windows\\\\system32\\cmd.exe" ipconfig>C:\\\\TestDir\\\\Test.txt in the batch file cfBatchFile.Write( "\\"C:\\\\windows\\\\system32\\cmd.exe\\" ipconfig>C:\\\\TestDir\\\\Test.txt", MAX\_PATH ); cfBatchFile.Close(); //ShellExecute runs "C:\\\\TestDir\\\\TestBatch.bat" and generates output for 'ipconfig' and redirects it to text file "C:\\\\TestDir\\\\Test.txt" if ( ShellExecute(NULL, "open", "C:\\\\TestDir\\\\TestBatch.bat", NULL, NULL, SW\_HIDE) > (HINSTANCE)32) { CFile\* pcfTxt = NULL; //Trying to open 'C:\\\\TestDir\\\\Test.txt' for reading BUT CRASH HAPPENS HERE (unhandled Exception) pcfTxt = new CFile(\_T("C:\\\\TestDir\\\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); INT nLogTxtLen = pcfTxt->GetLength(); }
In Debug mode (without a Debug point), the crash is shown at pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); But if I place a debug point at : pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); and then continue, no crash arises. I guess the issue is something to do with the handle of 'C:\\TestDir\\Test.txt' file. How could I rectify this. Is some Sleep() required to wait for the handle to be available. Replies would be appreciated. Thanks Edit/Delete Message
You can try using CreateProcess[^]instead of ShellExecute and then Call WaitForSingleObject[^]on the handle for process obtained from CreateProcess. Introducing Sleep might work but it will not be a good solution.
Regards, Sandip.
-
Hello, I've a code snippet. Please follow the comments.
//Create a directory if it doesn't exist if ( !PathFileExists("C:\\\\TestDir") ) { CreateDirectory("C:\\\\TestDir", NULL); } //Create a batch file in the directory CFile cfBatchFile( "C:\\\\TestDir\\\\TestBatch.bat", CFile::modeCreate| CFile::modeRead | CFile::modeWrite ); //Write 'C:\\\\windows\\\\system32\\cmd.exe" ipconfig>C:\\\\TestDir\\\\Test.txt in the batch file cfBatchFile.Write( "\\"C:\\\\windows\\\\system32\\cmd.exe\\" ipconfig>C:\\\\TestDir\\\\Test.txt", MAX\_PATH ); cfBatchFile.Close(); //ShellExecute runs "C:\\\\TestDir\\\\TestBatch.bat" and generates output for 'ipconfig' and redirects it to text file "C:\\\\TestDir\\\\Test.txt" if ( ShellExecute(NULL, "open", "C:\\\\TestDir\\\\TestBatch.bat", NULL, NULL, SW\_HIDE) > (HINSTANCE)32) { CFile\* pcfTxt = NULL; //Trying to open 'C:\\\\TestDir\\\\Test.txt' for reading BUT CRASH HAPPENS HERE (unhandled Exception) pcfTxt = new CFile(\_T("C:\\\\TestDir\\\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); INT nLogTxtLen = pcfTxt->GetLength(); }
In Debug mode (without a Debug point), the crash is shown at pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); But if I place a debug point at : pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); and then continue, no crash arises. I guess the issue is something to do with the handle of 'C:\\TestDir\\Test.txt' file. How could I rectify this. Is some Sleep() required to wait for the handle to be available. Replies would be appreciated. Thanks Edit/Delete Message
SherTeks wrote:
In Debug mode (without a Debug point), the crash is shown at...
Please explain this "crash" in more detail. Is an assertion firing? Is an exception being thrown?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
You can try using CreateProcess[^]instead of ShellExecute and then Call WaitForSingleObject[^]on the handle for process obtained from CreateProcess. Introducing Sleep might work but it will not be a good solution.
Regards, Sandip.
looks like this is related to delayed write. Just because you close a file does not mean the OS will write it the folowing is from the FlushFileBuffers documentation Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media, the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information, see CreateFile. to me it looks like you need to open it in non buffering mode.
a programmer traped in a thugs body
-
looks like this is related to delayed write. Just because you close a file does not mean the OS will write it the folowing is from the FlushFileBuffers documentation Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media, the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information, see CreateFile. to me it looks like you need to open it in non buffering mode.
a programmer traped in a thugs body
-
Hello, I've a code snippet. Please follow the comments.
//Create a directory if it doesn't exist if ( !PathFileExists("C:\\\\TestDir") ) { CreateDirectory("C:\\\\TestDir", NULL); } //Create a batch file in the directory CFile cfBatchFile( "C:\\\\TestDir\\\\TestBatch.bat", CFile::modeCreate| CFile::modeRead | CFile::modeWrite ); //Write 'C:\\\\windows\\\\system32\\cmd.exe" ipconfig>C:\\\\TestDir\\\\Test.txt in the batch file cfBatchFile.Write( "\\"C:\\\\windows\\\\system32\\cmd.exe\\" ipconfig>C:\\\\TestDir\\\\Test.txt", MAX\_PATH ); cfBatchFile.Close(); //ShellExecute runs "C:\\\\TestDir\\\\TestBatch.bat" and generates output for 'ipconfig' and redirects it to text file "C:\\\\TestDir\\\\Test.txt" if ( ShellExecute(NULL, "open", "C:\\\\TestDir\\\\TestBatch.bat", NULL, NULL, SW\_HIDE) > (HINSTANCE)32) { CFile\* pcfTxt = NULL; //Trying to open 'C:\\\\TestDir\\\\Test.txt' for reading BUT CRASH HAPPENS HERE (unhandled Exception) pcfTxt = new CFile(\_T("C:\\\\TestDir\\\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); INT nLogTxtLen = pcfTxt->GetLength(); }
In Debug mode (without a Debug point), the crash is shown at pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); But if I place a debug point at : pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); and then continue, no crash arises. I guess the issue is something to do with the handle of 'C:\\TestDir\\Test.txt' file. How could I rectify this. Is some Sleep() required to wait for the handle to be available. Replies would be appreciated. Thanks Edit/Delete Message
ooks like this is related to delayed write. Just because you close a file does not mean the OS will write it the folowing is from the FlushFileBuffers documentation Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media, the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information, see CreateFile. to me it looks like you need to open it in non buffering mode.
a programmer traped in a thugs body
-
Hello, I've a code snippet. Please follow the comments.
//Create a directory if it doesn't exist if ( !PathFileExists("C:\\\\TestDir") ) { CreateDirectory("C:\\\\TestDir", NULL); } //Create a batch file in the directory CFile cfBatchFile( "C:\\\\TestDir\\\\TestBatch.bat", CFile::modeCreate| CFile::modeRead | CFile::modeWrite ); //Write 'C:\\\\windows\\\\system32\\cmd.exe" ipconfig>C:\\\\TestDir\\\\Test.txt in the batch file cfBatchFile.Write( "\\"C:\\\\windows\\\\system32\\cmd.exe\\" ipconfig>C:\\\\TestDir\\\\Test.txt", MAX\_PATH ); cfBatchFile.Close(); //ShellExecute runs "C:\\\\TestDir\\\\TestBatch.bat" and generates output for 'ipconfig' and redirects it to text file "C:\\\\TestDir\\\\Test.txt" if ( ShellExecute(NULL, "open", "C:\\\\TestDir\\\\TestBatch.bat", NULL, NULL, SW\_HIDE) > (HINSTANCE)32) { CFile\* pcfTxt = NULL; //Trying to open 'C:\\\\TestDir\\\\Test.txt' for reading BUT CRASH HAPPENS HERE (unhandled Exception) pcfTxt = new CFile(\_T("C:\\\\TestDir\\\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); INT nLogTxtLen = pcfTxt->GetLength(); }
In Debug mode (without a Debug point), the crash is shown at pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); But if I place a debug point at : pcfTxt = new CFile(_T("C:\\TestDir\\Test.txt"), CFile::modeRead | CFile::shareDenyNone); and then continue, no crash arises. I guess the issue is something to do with the handle of 'C:\\TestDir\\Test.txt' file. How could I rectify this. Is some Sleep() required to wait for the handle to be available. Replies would be appreciated. Thanks Edit/Delete Message
I would suggest you a) use CreateProcess to run ipconfig (on its own, not in a batch file), so that you can b) redirect the standard output and standard error handles[^] of the created process to a string within your parent process. That way, your program is using the exact same mechanism as CMD to redirect ipconfig's output to where you want to make use of it. And you don't need to mess with files and stuff.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p