Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

share violation

Status
Not open for further replies.

shetlandbob

Programmer
Mar 9, 2004
528
GB
I have a CFileDialog with I select a file with to load into an Spreadsheet application (Tidestone Formula One spreadsheet to be precise ), but I cannot find out how to determine if I get a share violation on the file I am trying to load. I thought that if I tested it with fopen ( "file", "rw" ) then I could capture it, but this doesn't work. fopen operates fine.

Any ideas on how I can identify a share violation??? (currently it doesn't load the file and brings up a message box saying "invalid file", i require at least be to able to handle this error better and inform the user that the file may be open somewhere else?). I cannot capture a return from the Read command as te code exits the routine immediately on the Read failure.

I've included the code I use below:

Code:
void CTPP9Dlg::OnFileLoadExcel()
{
  CString fileName;
  CString pathName;
  
  char fileType[]= "Excel SpreadSheet (*.xls)|*.xls|Excel 5.0/95 Workbook (*.xls)|*.xls|";

  CFileDialog loadFile( true,(LPCTSTR)"xls", (LPCTSTR)fileName, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, fileType, this );
  if ( loadFile.DoModal() == IDOK )
  {
    pathName = loadFile.GetPathName();   // path and file

    FILE* file;
    file = fopen ( pathName, "rw" );
    if ( !file ) 
    {
      MessageBox ( "Is File Already Open?", "Warning", MB_ICONWARNING );
      return;
    }
    else 
    {
      fclose ( file );
    }
    
    short int type;
    SpreadSheet.Read ( pathName, &type );  // Excel Workbook
    if ( type != 11 )
    {
      if ( type != 4 ) MessageBox ( "Invalid File Format", "Warning", MB_ICONWARNING );
    }
  }
}
 
Use CreateFile() instead of fopen(). With CreateFile() you can specify exactly how you want to open the file, including whether or not to allow sharing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top