CopyFile( ) Returns "Access Denied" in Windows 7/8

This is a discussion on CopyFile( ) Returns "Access Denied" in Windows 7/8 within the Windows Programming forums, part of the Platform Specific Boards category; I've created an MFC dialog program to copy mp3 files from the HD to a USB Digital Audio Player. The ...

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    8

    CopyFile( ) Returns "Access Denied" in Windows 7/8

    I've created an MFC dialog program to copy mp3 files from the HD to a USB Digital Audio Player. The program runs OK on Windows XP (32-bit) but returns error code 5 (ERROR_ACCESS_DENIED) in Windows 7 and 8. This is not limited to CopyFile. I also used SetAttributes() and DeleteFile() which also return code 5. All help is appreciated. Here sample code:

    Code:
    hFind = ::FindFirstFile((LPCSTR)csSourceFileSpec, &FindFileData);
    while(hFind != INVALID_HANDLE_VALUE)
    { 
       if(!threadData->bRun) // if user canceled
       {
          FindClose(hFind);
          threadData->pWndProgress->SetPos(0);
          return 0; 
       }
       csSourceFileSpec.Format(_T("%s%s"), threadData->csSourceFolder, FindFileData.cFileName);
       csTargetFileSpec.Format(_T("%s%s"), threadData->csTargetFolder, FindFileData.cFileName);
       if(::CopyFile((LPCSTR)csSourceFileSpec, (LPCSTR)csTargetFileSpec, FALSE) == 0)
       {
          csPrompt.Format(_T("Error %d Copying %s to %s"), GetLastError(), csSourceFileSpec, csTargetFileSpec);
          AfxMessageBox(csPrompt, MB_OK|MB_ICONINFORMATION);
       }
       csPrompt.Format(_T("Copying %s"),(LPCTSTR)   FindFileData.cFileName);
       threadData->pWndStatusLabel->SetWindowText((LPCTSTR)csPrompt);
       threadData->pWndProgress->StepIt();
       if(!FindNextFile(hFind, &FindFileData))
       { 
          FindClose(hFind);
          hFind = INVALID_HANDLE_VALUE; // end while loop
       }
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    It would help if you specified what the various inputs are (csSourceFileSpec, etc), the files available here to be copied, and info about the copy destination (e.g. are you attempting to copy files to an existing destination, and overwrite what's there?).

    CopyFile() does fail with ERROR_ACCESS_DENIED if the destination exists (e,g, copying A\x.dat to B\x.dat, where B\x.dat already exists).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    8
    This dialog-based MFC program compiled in Visual C++ 6.0 in 32-bit WXP works fine in WXP but fails in W7/8. Here's the program dialog:

    Name:  MP3TODAP.JPG
Views: 148
Size:  21.2 KB

    User selects target drive from list and clicks Copy. The files from source folder (top CStatic, defaults to exe module folder) to target folder (bottom CStatic, defaults to root of selected drive).

    Copy button handler calls the following thread:

    Code:
    UINT ThreadMain(void * data)
    {
     CString csSourceFileSpec;
     CString csTargetDriveSpec;
     CString csTargetFileSpec;
     HANDLE hFind;
     WIN32_FIND_DATA FindFileData;
     CString csPrompt;
     // create drive and folder specs
     
     ThreadData * threadData = static_cast<ThreadData*>(data);   // get thread struct
     csSourceFileSpec.Format(_T("%s*.mp3"), threadData->csSourceFolder); // "SourceFolder\*.mp3"
     csTargetDriveSpec = threadData->csTargetFolder.Left(2) + _T("\\"); // "X:\"
     csTargetFileSpec.Format(_T("%s*.mp3"), threadData->csTargetFolder); // "SourceFolder\*.mp3"
     
     // count # of mp3 files to delete from Target Folder
     int iNumFilesToProcess = 0;
     if(threadData->bDeleteOldMP3Files)
     {
      hFind = ::FindFirstFile((LPCSTR)csTargetFileSpec, &FindFileData);
      while(hFind != INVALID_HANDLE_VALUE)
      {  
       if(!threadData->bRun) // user canceled?
       {
        FindClose(hFind);
        return 0;
       }
       iNumFilesToProcess++;
       if(!FindNextFile(hFind, &FindFileData))
       {   
        FindClose(hFind);
        hFind = INVALID_HANDLE_VALUE;
       } 
      }
     }
     
     // count # of mp3 files to copy from source folder
     hFind = ::FindFirstFile((LPCSTR)csSourceFileSpec, &FindFileData);
     if(hFind == INVALID_HANDLE_VALUE)
     {
      FindClose(hFind);
      threadData->pMainWind->ShowWindow(SW_SHOW);
      threadData->pWndStatusLabel->SetWindowText(_T(""));
      threadData->pWndCopyButton->SetWindowText(_T("Copy"));
      threadData->pWndListBoxDrives->EnableWindow(TRUE);
      AfxMessageBox(_T("No mp3 file in source folder!\t"), MB_OK|MB_ICONEXCLAMATION);
      return 0;
     }
     while(hFind != INVALID_HANDLE_VALUE)
     {  
      if(!threadData->bRun)
      {
       FindClose(hFind);
       return 0;
      }
      iNumFilesToProcess++;
      if(!FindNextFile(hFind, &FindFileData))
      {   
       FindClose(hFind);
       hFind = INVALID_HANDLE_VALUE;
      } 
     }
     // initialize progress control
     threadData->pWndProgress->SetRange(0, iNumFilesToProcess);
     threadData->pWndProgress->SetStep(1);
     threadData->pWndProgress->SetPos(0);
     // delete all mp3 files in Target Folder
     if(threadData->bDeleteOldMP3Files)
     {
      hFind = ::FindFirstFile((LPCSTR)csTargetFileSpec, &FindFileData);
      while(hFind != INVALID_HANDLE_VALUE)
      { 
       if(!threadData->bRun)
       {
        FindClose(hFind);
        threadData->pWndProgress->SetPos(0);
        return 0;
       }
       csTargetFileSpec.Format(_T("%s%s"), (LPCSTR)csTargetDriveSpec, FindFileData.cFileName);
       if(::SetFileAttributes((LPCSTR)csTargetFileSpec, FILE_ATTRIBUTE_NORMAL) == 0)
       {
        csPrompt.Format(_T("Error: %d setting attributes for %s"), GetLastError(), csTargetFileSpec);
        AfxMessageBox(csPrompt, MB_OK|MB_ICONINFORMATION);
       }
       if(::DeleteFile((LPCSTR)csTargetFileSpec) == 0)
       {
        csPrompt.Format(_T("Error %d Deleting %s"), GetLastError(), csTargetFileSpec);
        AfxMessageBox(csPrompt, MB_OK|MB_ICONINFORMATION);
       }
       csPrompt.Format(_T("Deleting %s"),(LPCTSTR)FindFileData.cFileName);
       threadData->pWndStatusLabel->SetWindowText((LPCTSTR)csPrompt);
       threadData->pWndProgress->StepIt();
       if(!FindNextFile(hFind, &FindFileData))
       {   
        FindClose(hFind);
        hFind = INVALID_HANDLE_VALUE;
       } 
      }
     }
     // copy mp3 files from Source Folder to Target Folder
     hFind = ::FindFirstFile((LPCSTR)csSourceFileSpec, &FindFileData);
     while(hFind != INVALID_HANDLE_VALUE)
     { 
      if(!threadData->bRun)
      {
       FindClose(hFind);
       threadData->pWndProgress->SetPos(0);
       return 0;
      }
      csSourceFileSpec.Format(_T("%s%s"), threadData->csSourceFolder, FindFileData.cFileName);
      csTargetFileSpec.Format(_T("%s%s"), threadData->csTargetFolder, FindFileData.cFileName);
      if(::CopyFile((LPCSTR)csSourceFileSpec, (LPCSTR)csTargetFileSpec, FALSE) == 0)
      {
       csPrompt.Format(_T("Error %d Copying %s to %s"), GetLastError(), csSourceFileSpec, csTargetFileSpec);
       AfxMessageBox(csPrompt, MB_OK|MB_ICONINFORMATION);
      }
      csPrompt.Format(_T("Copying %s"),(LPCTSTR)FindFileData.cFileName);
      threadData->pWndStatusLabel->SetWindowText((LPCTSTR)csPrompt);
      threadData->pWndProgress->StepIt();
      if(!FindNextFile(hFind, &FindFileData))
      {   
       FindClose(hFind);
       hFind = INVALID_HANDLE_VALUE;
      } 
     }
     // Housekeeping
     //ShellExecute(NULL, NULL, threadData->csTargetFolder, NULL, NULL, SW_SHOWNOACTIVATE);
     threadData->pWndStatusLabel->SetWindowText(_T(""));
     threadData->pWndProgress->SetPos(0);
     threadData->pWndCopyButton->SetWindowText(_T("Copy"));
     threadData->pWndListBoxDrives->EnableWindow(TRUE);
     csPrompt = _T("Successfully copied MP3 files to DAP!\t\n\n");
     csPrompt += _T("1. The DAP will be ejected automatically after you click OK here.\t\n");
     csPrompt += _T("2. Wait until DAP is ejected (disappears from the drive list).\t\n");
     csPrompt += _T("3. Physically disconnect DAP from USB port.\t\n");
     csPrompt += _T("4. Connect next DAP to USB port and wait for it to show up in drive list.\t\n");
     csPrompt += _T("5. Select new DAP from the list and click Copy.\t\n\n");
     csPrompt += _T("These instructions were placed in the ClipBoard\t");
     PutInClipBoard(csPrompt);
     AfxMessageBox(csPrompt, MB_OK|MB_ICONINFORMATION);
     CString csRemoveDriveParamters;
     csRemoveDriveParamters.Format(_T("%c: -h -a -b -W:1000"), (LPCSTR)csTargetDriveSpec[0]);
     HINSTANCE hiReturnValue = ShellExecute(NULL, "open", "removedrive.exe", (LPCSTR)csRemoveDriveParamters, NULL, SW_HIDE);
     if(hiReturnValue <= (HINSTANCE)32)
     {
      csPrompt.Format(_T("Could not Eject Drive %s\t"),csTargetDriveSpec);
      AfxMessageBox(csPrompt, MB_OK|MB_ICONINFORMATION);
     }
     threadData->bRun = false;
     return 0;
    }
    The thread deletes all existing mp3 files from Target Folder (if user checked the Checkbox) first. Then it copies all mp3 files from Source Folder to Target Folder. It then called RemoveDrive to eject target drive (Digital Audio Player). All functions work fine on Win7/8 except for SetFileAttributes(), DeleteFile() and CopyFile(). Thank you for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 7 "access denied" for gcc
    By brightmatter in forum Tech Board
    Replies: 4
    Last Post: 03-25-2010, 04:12 PM
  2. "Permission Denied" error reading file
    By Katman in forum C++ Programming
    Replies: 7
    Last Post: 05-08-2009, 08:00 PM
  3. Replies: 1
    Last Post: 02-10-2009, 11:02 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21