discodowney
discodowney

Reputation: 1507

System Error 124 - ERROR_INVALID_LEVEL with SHFileOperation

I was having a problem with using SHFileOperation: SHFileOperation/SHFILEOPSTRUCT. I got that working but I am now trying to put that into a function as it will be used several times throughout my code. The functions is:

void SHFileOperationFunc(string item1, string item2, int operation)
{
    SHFILEOPSTRUCT sf;
    memset(&sf,0,sizeof(sf));
    sf.hwnd = 0;
    sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;

    switch(operation)
    {
    case 1:

    case 2:
        sf.wFunc = FO_COPY;
        string files = item1 + "\\*.*";
        files.append(1, '\0');
        sf.pFrom = files.c_str();
        item2.append(1, '\0');
        sf.pTo = item2.c_str();
    }

    int opOkay = SHFileOperation(&sf);
    if(opOkay != 0)
    {
        //FAIL
    }

}

When I had the code outside the function it worked fine. But now that it is as above i get an error returned to opOkay. The error value is 124 which means ERROR_INVALID_LEVEL - The system call level is not correct. I dont know what this means. Google hasnt been much help either. Anyone enlighten me?

Also should i be using SHFileOperation at all or should I be using IFileOperation?

Cheers.

Upvotes: 3

Views: 4761

Answers (1)

hmjd
hmjd

Reputation: 121971

The problem is that files will go out of scope when the switch block ends and sf.pFrom will be a dangling pointer. Move declaration of files to outside of the switch.

Note you have no breaks in either of the switch blocks.

Upvotes: 2

Related Questions