Board index » cppbuilder » Copy Shell

Copy Shell


2005-08-07 05:45:28 AM
cppbuilder105
Hey guys,
Why does the following code to copy files through the shell not work
correctly? Sometimes he says he cannot read the source file, sometimes
the "overwrite" dialog doesn't show up a filename etc..
void ShellCopyFiles(TStrings* sourceFiles, AnsiString targetDir)
{
AnsiString tmpTarget = targetDir;
tmpTarget += "\0";
try {
AnsiString aFrom = "C:\\test.txt\0";
AnsiString aTo = tmpTarget;
char* pFrom = new char[aFrom.Length()+1];
char* pTo = new char[aTo.Length()+1];
// Set the contents of string to zeros
memset(pFrom, 0, aFrom.Length()+1);
memset(pTo, 0, aTo.Length()+1);
// Copy strings
strcpy(pFrom, aFrom.c_str());
strcpy(pTo, aTo.c_str());
TSHFileOpStruct* opStruct = new TSHFileOpStruct();
try {
// Properties
opStruct->hwnd = Application->Handle;
opStruct->wFunc = FO_COPY;
opStruct->pFrom = pFrom;
opStruct->pTo = pTo;
opStruct->fFlags = 0;
opStruct->hNameMappings = 0;
SHFileOperation(opStruct);
opStruct->fAnyOperationsAborted;
}
__finally {
delete opStruct;
}
delete [] pFrom;
delete [] pTo;
}
catch (char* excpMsg) {
// ...
}
}
 
 

Re:Copy Shell

Alexander Adam < XXXX@XXXXX.COM >writes:
[Just a side note]
Quote
TSHFileOpStruct* opStruct = new TSHFileOpStruct();
try {
This try suggests that ...
Quote
// Properties
opStruct->hwnd = Application->Handle;
opStruct->wFunc = FO_COPY;
opStruct->pFrom = pFrom;
opStruct->pTo = pTo;
opStruct->fFlags = 0;
opStruct->hNameMappings = 0;
SHFileOperation(opStruct);
opStruct->fAnyOperationsAborted;
some of these lines may throw.
Quote
}
__finally {
delete opStruct;
}

delete [] pFrom;
delete [] pTo;
If that happens, these two lines will not be executed, and the memory
refered to by pFrom and pTo will be leaked.
It's a good idea to never use a "raw" pointer to express ownership of
dynamically allocated resources (such as memory). Better use automatic
objects or, if that's not possible, smart pointers (like std::auto_ptr
or boost::shared_ptr) or container classes (such as
std::vector<char>).
E.g. is there anything wrong with
void ShellCopyFiles(AnsiString targetDir)
{
AnsiString aFrom("C:\\test.txt\0");
AnsiString aTo(targetDir+"\0");
TSHFileOpStruct opStruct;
opStruct.hwnd = Application->Handle;
opStruct.wFunc = FO_COPY;
opStruct.pFrom = aFrom.c_str();
opStruct.pTo = tmpTarget.c_str();
opStruct.fFlags = 0;
opStruct.hNameMappings = 0;
if (SHFileOperation(&opStruct)==0)
; // success
else if (opStruct.fAnyOperationsAborted)
; // handle abort by user
else
; // handle error
}
?
 

Re:Copy Shell

Thomas Maeder [TeamB] wrote:
Quote
AnsiString aFrom("C:\\test.txt\0");
Not being C++ savvy, that looks like aFrom would be single-null
terminated instead of the expected double null?
Might be better to use an older method:
char aFrom[] = "C:\\test.txt\0";
Quote
opStruct.pFrom = aFrom.c_str();
opStruct.pFrom = aFrom;
 

{smallsort}

Re:Copy Shell

Bob Gonder < XXXX@XXXXX.COM >writes:
Quote
>AnsiString aFrom("C:\\test.txt\0");

Not being C++ savvy, that looks like aFrom would be single-null
terminated instead of the expected double null?

Might be better to use an older method:

char aFrom[] = "C:\\test.txt\0";
This makes sense.