-
February 1st, 2005, 01:03 PM
#1
SHFileOperation
Here is the code . Everything works fine as long as I use
the char *, but if I use CStrings instead ,in passing the
file names it throws an error. I tried to assign the
allocated CStrings to char pointers but wouldn’t work. It’s
bizarre. Any clues how to convert the CSring to char * and
still make it work.
Thanks,
bool bFailIfExists = false;
char *strTS = "c:\\temp\\msv.txt";
char *strTD = "\\\\MyMachine\\ docs\\msv.txt";
CString strSource("c:\\temp\\msv.txt"), strDest
("\\\\MyMachine\\docs\\msv.txt");
SHFILEOPSTRUCT shop;
ZeroMemory(&shop, sizeof(shop));
shop.hwnd = AfxGetMainWnd()->m_hWnd;
shop.wFunc = FO_COPY;
shop.pFrom = strTS;
shop.pTo = strTD;
shop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;
if (0 == SHFileOperation(&shop))
{
bool bRet = true;
}
-
February 1st, 2005, 02:08 PM
#2
Re: SHFileOperation
What error ? Compilation error or runtime error ?
Note that you can use CString objects wherever LPCTSTR/LPSTR are used since CString provides an operator. So you don't have to do anything there..
-
February 1st, 2005, 02:27 PM
#3
Re: SHFileOperation
I know that CString automatically does this job and we don't need to do anything. I never had any problem doing that before, except for with this function, understandbly so , since it's a Shell programming API and caters to other supported languages.
Unfortunately the error(Run-time) is not specific: Is says "Copy failed:Cannot read the source file".
The code that I sent is the working code, you can copy into a simple Dialog based exe and run to see , by first sending the char pointers for the file names and then use CStrings.
-
February 1st, 2005, 02:29 PM
#4
Re: SHFileOperation
The filenames pFrom and pTo should be double NULL terminated.
-
February 1st, 2005, 02:35 PM
#5
Re: SHFileOperation
I think, that's only when you pass multiple files. If you look at the code, I didnot set multiple NULLs in both char* and CString, however, it works with the char * and fails with CStrings, even though both were initialized by the same set of string literals.
-
February 1st, 2005, 02:45 PM
#6
Re: SHFileOperation
Yes, I did .....ended up with the same error message.
-
February 1st, 2005, 02:46 PM
#7
Re: SHFileOperation
U need to double terminate. This code snippet from Paul DiLascia's Q&A
// Copy pathname to double-NULL-terminated string.
//
TCHAR buf[_MAX_PATH + 1]; // allow one more character
_tcscpy(buf, pszPath); // copy caller's pathname
buf[_tcslen(buf)+1]=0; // need two NULLs at end
// Set SHFILEOPSTRUCT params for delete operation
//
wFunc = FO_DELETE; // REQUIRED: delete operation
pFrom = buf; // REQUIRED: which file(s)
pTo = NULL; // MUST be NULL
if (bDelete) { // if delete requested..
fFlags &= ~FOF_ALLOWUNDO; // ..don't use Recycle Bin
} else { // otherwise..
fFlags |= FOF_ALLOWUNDO; // ..send to Recycle Bin
}
return SHFileOperation(this); // do it!
-
February 1st, 2005, 02:49 PM
#8
Re: SHFileOperation
Originally Posted by svmatcha
I think, that's only when you pass multiple files. If you look at the code, I didnot set multiple NULLs in both char* and CString, however, it works with the char * and fails with CStrings, even though both were initialized by the same set of string literals.
No, it should be double-null terminated.
Probably, you were just lucky to have double nulls in your char* test.
-
February 1st, 2005, 02:54 PM
#9
Re: SHFileOperation
I tried double termniate both char * & CString.
1) Irrespective of double terminate or not char * parameter works.
2) Double terminate or single terminate, same error message with CStrings.
Pretty much exhausted all options. Including copying the CStrings into char[] and terminate the string manually etc wasting a whole day.
The bottom line is , any involvement of CString in any stage of construction of the string lateral used as a pFrom or pTo parameter, causes a failure.
-
February 1st, 2005, 02:58 PM
#10
Re: SHFileOperation
I use something like:
Code:
// pszFrom should be DOUBLE NULL terminated!
TCHAR* pszFrom = new TCHAR[m_strFrom.GetLength() + 2];
_tcsncpy(pszFrom, m_strFrom, m_strFrom.GetLength());
pszFrom[m_strFrom.GetLength()] = 0;
pszFrom[m_strFrom.GetLength() + 1] = 0;
fileop.pFrom = pszFrom;
m_strFrom is a CString.
NOTE: just putting \0\0 inside your string isn't guaranteed to work IIRC.
-
February 1st, 2005, 03:09 PM
#11
Re: SHFileOperation
Awesome. It worked.
Thaks much.
-
February 2nd, 2005, 11:49 AM
#12
Re: SHFileOperation
Originally Posted by svmatcha
Awesome. It worked.
Thaks much.
I had the problem of copying the security attributes using SHFileOperation. Were you able to copy the security attribute of the source to destination when the copy is performed?
Thanks.
Ken
-
February 2nd, 2005, 01:50 PM
#13
Re: SHFileOperation
Originally Posted by svmatcha
Awesome. It worked.
Thaks much.
I have tried the method which everyone has suggested, the file is copied OK, but the problem is the security attributes are not copied. It changed my attribute from (Administrators, SYSTEM, myself) to everyone on the "TO" file.
Can anyone tell me why? Thanks.
-
March 11th, 2005, 05:51 AM
#14
Re: SHFileOperation
Thanks every Buddy,
This Api is bugging me for quite some time.
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|