Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why am I not able to get handle of file?

Why am I not able to get handle of file?

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpquestion
7 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SherTeks
    wrote on last edited by
    #1

    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

    S D K S 4 Replies Last reply
    0
    • S SherTeks

      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

      S Offline
      S Offline
      SandipG
      wrote on last edited by
      #2

      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.

      K 1 Reply Last reply
      0
      • S SherTeks

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • S SandipG

          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.

          K Offline
          K Offline
          ky_rerun
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • K ky_rerun

            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

            S Offline
            S Offline
            SandipG
            wrote on last edited by
            #5

            I think you wanted to reply to OP :)

            Regards, Sandip.

            1 Reply Last reply
            0
            • S SherTeks

              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

              K Offline
              K Offline
              ky_rerun
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • S SherTeks

                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

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups