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. SHFileOperation failed, why.

SHFileOperation failed, why.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 Posters 0 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.
  • T Offline
    T Offline
    ttzzgg_80713
    wrote on last edited by
    #1

    the app code is below .i could find any problem, why it isn't work. BOOL AppFileOperator(LPCSTR lpszSrc, LPCSTR lpszDest, int op) { SHFILEOPSTRUCT stFileOP; string strsrc = lpszSrc; string strDesc = lpszDest; stFileOP.pFrom = strsrc.c_str(); stFileOP.pTo = strDesc.c_str(); stFileOP.wFunc = op; stFileOP.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; printf("Start to file operator. \n src file : %s\n, desc file is %s\n", strsrc.c_str(), strDesc.c_str()); return SHFileOperation(&stFileOP) == 0 ? TRUE : FALSE; }

    M 1 Reply Last reply
    0
    • T ttzzgg_80713

      the app code is below .i could find any problem, why it isn't work. BOOL AppFileOperator(LPCSTR lpszSrc, LPCSTR lpszDest, int op) { SHFILEOPSTRUCT stFileOP; string strsrc = lpszSrc; string strDesc = lpszDest; stFileOP.pFrom = strsrc.c_str(); stFileOP.pTo = strDesc.c_str(); stFileOP.wFunc = op; stFileOP.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; printf("Start to file operator. \n src file : %s\n, desc file is %s\n", strsrc.c_str(), strDesc.c_str()); return SHFileOperation(&stFileOP) == 0 ? TRUE : FALSE; }

      M Offline
      M Offline
      Mike Upton
      wrote on last edited by
      #2

      If you have a look in the documentation for SHFILEOPSTRUCT, you'll see that pFrom and pTo both need to be double null terminated strings, which the std::string::c_str() operator won't give you. A couple of minor points too - you should really initialise the entire SHFILEOPSTRUCT structure, and your code isn't UNICODE safe (but I'm assuming that's not an issue for you). So, one possible version of this code would be (N.B. code is untested):

      BOOL AppFileOperator(LPCSTR lpszSrc, LPCSTR lpszDest, int op)
      {
      SHFILEOPSTRUCT stFileOP={0};

      //allow space for double terminating null
      char* pFrom = new char[strlen(lpszSrc)+2];
      char* pTo = new char[strlen(lpszDest)+2];

      //strncpy will pad the copied string with nulls up to the length
      //specified, so no need to add the terminating nulls separately.
      strncpy(pFrom, lpszSrc, strlen(lpszSrc)+2);
      strncpy(pTo, lpszDest, strlen(lpszDest)+2);

      stFileOP.pFrom = pFrom;
      stFileOP.pTo = pTo;
      stFileOP.wFunc = op;
      stFileOP.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;

      printf("Start to file operator.\n src file : %s,\n dest file is %s\n", lpszSrc, lpszDest);

      BOOL bSuccess = (SHFileOperation(&stFileOP) == 0);

      delete[] pFrom;
      delete[] pTo;

      return bSuccess;
      }


      "We are the knights who say Ni" (The Knights Who Say Ni)

      T 1 Reply Last reply
      0
      • M Mike Upton

        If you have a look in the documentation for SHFILEOPSTRUCT, you'll see that pFrom and pTo both need to be double null terminated strings, which the std::string::c_str() operator won't give you. A couple of minor points too - you should really initialise the entire SHFILEOPSTRUCT structure, and your code isn't UNICODE safe (but I'm assuming that's not an issue for you). So, one possible version of this code would be (N.B. code is untested):

        BOOL AppFileOperator(LPCSTR lpszSrc, LPCSTR lpszDest, int op)
        {
        SHFILEOPSTRUCT stFileOP={0};

        //allow space for double terminating null
        char* pFrom = new char[strlen(lpszSrc)+2];
        char* pTo = new char[strlen(lpszDest)+2];

        //strncpy will pad the copied string with nulls up to the length
        //specified, so no need to add the terminating nulls separately.
        strncpy(pFrom, lpszSrc, strlen(lpszSrc)+2);
        strncpy(pTo, lpszDest, strlen(lpszDest)+2);

        stFileOP.pFrom = pFrom;
        stFileOP.pTo = pTo;
        stFileOP.wFunc = op;
        stFileOP.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;

        printf("Start to file operator.\n src file : %s,\n dest file is %s\n", lpszSrc, lpszDest);

        BOOL bSuccess = (SHFileOperation(&stFileOP) == 0);

        delete[] pFrom;
        delete[] pTo;

        return bSuccess;
        }


        "We are the knights who say Ni" (The Knights Who Say Ni)

        T Offline
        T Offline
        ttzzgg_80713
        wrote on last edited by
        #3

        thanks, a good man. ..... i'm a lazy boy

        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