问题描述
我使用 SHFileOperation
时遇到问题:。我得到了工作,但我现在试图把它放入一个函数,因为它将在我的代码中使用几次。函数是:
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
}
}
当我有代码之外的函数,它工作正常。但现在,它是如上我得到一个错误返回opOkay。错误值为124,表示ERROR_INVALID_LEVEL - 系统调用级别不正确。我不知道这是什么意思。 Google也没有太大的帮助。
也应该使用 SHFileOperation
,否则我应该使用 IFileOperation
?
干杯。
p>问题是文件
将在开关块结束时超出作用域, sf.pFrom
悬停指针。将文件的声明
移动到开关
之外。
注意,您在开关
块中没有 break
>
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 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 break
s in either of the switch
blocks.
这篇关于系统错误124 - ERROR_INVALID_LEVEL与SHFileOperation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!