SHFileOperation failed, why.
-
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; }
-
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; }
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)
-
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)
thanks, a good man. ..... i'm a lazy boy