CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: SHFileOperation

  1. #1
    Join Date
    Jul 2004
    Posts
    7

    SHFileOperation

    Here is the code . Everything works fine as long as I use
    the char *, but if I use CStrings instead ,in passing the
    file names it throws an error. I tried to assign the
    allocated CStrings to char pointers but wouldn’t work. It’s
    bizarre. Any clues how to convert the CSring to char * and
    still make it work.

    Thanks,


    bool bFailIfExists = false;

    char *strTS = "c:\\temp\\msv.txt";
    char *strTD = "\\\\MyMachine\\ docs\\msv.txt";

    CString strSource("c:\\temp\\msv.txt"), strDest
    ("\\\\MyMachine\\docs\\msv.txt");

    SHFILEOPSTRUCT shop;
    ZeroMemory(&shop, sizeof(shop));
    shop.hwnd = AfxGetMainWnd()->m_hWnd;
    shop.wFunc = FO_COPY;
    shop.pFrom = strTS;
    shop.pTo = strTD;
    shop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;


    if (0 == SHFileOperation(&shop))
    {
    bool bRet = true;
    }

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: SHFileOperation

    What error ? Compilation error or runtime error ?

    Note that you can use CString objects wherever LPCTSTR/LPSTR are used since CString provides an operator. So you don't have to do anything there..

  3. #3
    Join Date
    Jul 2004
    Posts
    7

    Re: SHFileOperation

    I know that CString automatically does this job and we don't need to do anything. I never had any problem doing that before, except for with this function, understandbly so , since it's a Shell programming API and caters to other supported languages.

    Unfortunately the error(Run-time) is not specific: Is says "Copy failed:Cannot read the source file".

    The code that I sent is the working code, you can copy into a simple Dialog based exe and run to see , by first sending the char pointers for the file names and then use CStrings.

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: SHFileOperation

    The filenames pFrom and pTo should be double NULL terminated.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Jul 2004
    Posts
    7

    Re: SHFileOperation

    I think, that's only when you pass multiple files. If you look at the code, I didnot set multiple NULLs in both char* and CString, however, it works with the char * and fails with CStrings, even though both were initialized by the same set of string literals.

  6. #6
    Join Date
    Jul 2004
    Posts
    7

    Re: SHFileOperation

    Yes, I did .....ended up with the same error message.

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: SHFileOperation

    U need to double terminate. This code snippet from Paul DiLascia's Q&A

    // Copy pathname to double-NULL-terminated string.
    //
    TCHAR buf[_MAX_PATH + 1]; // allow one more character
    _tcscpy(buf, pszPath); // copy caller's pathname
    buf[_tcslen(buf)+1]=0; // need two NULLs at end

    // Set SHFILEOPSTRUCT params for delete operation
    //
    wFunc = FO_DELETE; // REQUIRED: delete operation
    pFrom = buf; // REQUIRED: which file(s)
    pTo = NULL; // MUST be NULL
    if (bDelete) { // if delete requested..
    fFlags &= ~FOF_ALLOWUNDO; // ..don't use Recycle Bin
    } else { // otherwise..
    fFlags |= FOF_ALLOWUNDO; // ..send to Recycle Bin
    }
    return SHFileOperation(this); // do it!

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: SHFileOperation

    Quote Originally Posted by svmatcha
    I think, that's only when you pass multiple files. If you look at the code, I didnot set multiple NULLs in both char* and CString, however, it works with the char * and fails with CStrings, even though both were initialized by the same set of string literals.
    No, it should be double-null terminated.
    Probably, you were just lucky to have double nulls in your char* test.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  9. #9
    Join Date
    Jul 2004
    Posts
    7

    Re: SHFileOperation

    I tried double termniate both char * & CString.



    1) Irrespective of double terminate or not char * parameter works.

    2) Double terminate or single terminate, same error message with CStrings.

    Pretty much exhausted all options. Including copying the CStrings into char[] and terminate the string manually etc wasting a whole day.

    The bottom line is , any involvement of CString in any stage of construction of the string lateral used as a pFrom or pTo parameter, causes a failure.

  10. #10
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: SHFileOperation

    I use something like:
    Code:
      // pszFrom should be DOUBLE NULL terminated!
      TCHAR* pszFrom = new TCHAR[m_strFrom.GetLength() + 2];
      _tcsncpy(pszFrom, m_strFrom, m_strFrom.GetLength());
      pszFrom[m_strFrom.GetLength()] = 0;
      pszFrom[m_strFrom.GetLength() + 1] = 0;
      fileop.pFrom = pszFrom;
    m_strFrom is a CString.
    NOTE: just putting \0\0 inside your string isn't guaranteed to work IIRC.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  11. #11
    Join Date
    Jul 2004
    Posts
    7

    Re: SHFileOperation

    Awesome. It worked.

    Thaks much.

  12. #12
    Join Date
    May 2002
    Posts
    107

    Question Re: SHFileOperation

    Quote Originally Posted by svmatcha
    Awesome. It worked.

    Thaks much.
    I had the problem of copying the security attributes using SHFileOperation. Were you able to copy the security attribute of the source to destination when the copy is performed?

    Thanks.

    Ken

  13. #13
    Join Date
    May 2002
    Posts
    107

    Re: SHFileOperation

    Quote Originally Posted by svmatcha
    Awesome. It worked.

    Thaks much.
    I have tried the method which everyone has suggested, the file is copied OK, but the problem is the security attributes are not copied. It changed my attribute from (Administrators, SYSTEM, myself) to everyone on the "TO" file.

    Can anyone tell me why? Thanks.

  14. #14
    Join Date
    Jan 2005
    Posts
    4

    Smile Re: SHFileOperation

    Thanks every Buddy,
    This Api is bugging me for quite some time.

    Thanks
    Cheers,
    Alok Gupta
    visit me at http://www.thatsalok.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured