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.
The problem is that
fileswill go out of scope when the switch block ends andsf.pFromwill be a dangling pointer. Move declaration offilesto outside of theswitch.Note you have no
breaks in either of theswitchblocks.