Mr Aj
Mr Aj

Reputation: 21

Error 124- The system call level is not correct for SHFileOperation

I am tring to copy file/directory from one location to other using SHFileOperation. My code for this so far is,

    for(int index=0; index < clbSource.GetCount();index++ )
    {
        SHFILEOPSTRUCT SHFileOp;
        CString src,dest;
        std::string NULLTermination="\\0\\0";

        //Get source file/directory path from CListBox
        int n = clbSource.GetTextLen(index);
        clbSource.GetText(index, src.GetBuffer(n));
        src.ReleaseBuffer();
        CT2CA pszConvertedAnsiString (src);
        std::string strStdS (pszConvertedAnsiString);

        //replace single slash with double
        size_t found = 0, next = 0;
        while( (found = strStdS.find('\\', next)) != std::string::npos )
        {
        strStdS.insert(found, "\\");
        next = found+4;
        }

        //Add double null termination
        strStdS.append(NULLTermination);   
        std::wstring stempS = s2ws(strStdS);

        //Get source file/directory path from CListBox
        int m = clbDest.GetTextLen(index);
        clbDest.GetText(index, dest.GetBuffer(n));
        dest.ReleaseBuffer();
        CT2CA pszConvertedAnsiStringd (dest);
        std::string strStdD (pszConvertedAnsiStringd);

        //replace single slash with double
        size_t foundD = 0, nextD = 0;
        while( (foundD = strStdD.find('\\', nextD)) != std::string::npos )
        {
        strStdD.insert(foundD, "\\");
        nextD = foundD+4;
        }

        //Add double null termination
        strStdD.append(NULLTermination);   
        std::wstring stempD = s2ws(strStdD);


        //  set up File Operation structure
        ZeroMemory(&SHFileOp, sizeof(SHFILEOPSTRUCT));

        // application the owner of the progress dialog.
        SHFileOp.hwnd = GetSafeHwnd();
        SHFileOp.wFunc = FO_COPY;

        SHFileOp.pFrom = stempS.c_str();
        //SHFileOp.pFrom = _T("E:\\backup\\java softwares\0\0");

        SHFileOp.pTo = stempD.c_str();
        //SHFileOp.pTo = _T("E:\\Tmp\0\0");

        // Set the flags.
        SHFileOp.fFlags = FOF_SIMPLEPROGRESS;

        // Do it.
        DWORD result = SHFileOperation(&SHFileOp);

        //Check for success
        if(result!=0)
        {
        ::AfxMessageBox(ERR_MSG_FAILED_TO_COPY);
        }
        else
            ::AfxMessageBox(SUCCESS);

    }

Now, the result is non-zero ie 124. Also gone throgh this link System Error 124 - ERROR_INVALID_LEVEL with SHFileOperation. Can't understand where the problem is.

Note: If hardcoded values (as shown) are passed to 'SHFileOp.pFrom' and 'SHFileOp.pTo', it works with no issues.

Can anyone help??

Upvotes: 0

Views: 3291

Answers (2)

Mike Vine
Mike Vine

Reputation: 9837

Firstly, std::string NULLTermination="\\0\\0" should be std::string NULLTermination="\0\0".

Even then, std::string NULLTermination="\0\0"; doesn't do what you think it does.

It creates an empty string. This is because the constructor of a std::string from a char* treats the string you give it as a nul terminated string. It gets to the first nul and ends there as am empty string, not even looking at the second nul.

There's a few ways round this to create a double-nul terminated string, probably the easiest is:

std::string NULLTermination;
NullTermination.push_back('\0');

Upvotes: 1

peterchen
peterchen

Reputation: 41096

SHFileOperation does not return standard Windows Error codes, for "historic reasons".

MSDN:SHFileOperation, has a list of possible results under "Return Values".

124 == 0x7C == The path in the source or destination or both was invalid.

Inspect the values you are actually passing in the struct, there's probably a bug in the conversion of the paths.

Upvotes: 3

Related Questions