Hi,
I'm trying to use the CopyFileEx() method, and I occassionally get a failure. When I use GetLastError, it returns "87", which unless I've looked up in the wrong place, means "Invalid Parameter". I'm having no luck determining what this error actually means in this situation. Can anyone help
I have been copying the same set of files over and over in testing, and this error recently cropped up. Most of the time, the entire set of files copies just fine. But every once in a while I get this error.
I am copying files to a 100Gig removable hard drive. Until yesterday, the drive was formatted to be a Linux drive (ext3 format), and I had drivers on my windows system to allow me to read and write to it. During this time, I never got this error 87 from CopyFileEx.
I am now doing the same testing with the drive formatted to FAT32. Because of the size limitation with Windows formatting > 32G, I used a third party tool that would allow me to format a drive of that size. Since I've reformatted the drive, I've gotten the "error 87" several times. It seems to me to be related to the change to FAT32, but it would help if I could understand what the error actually means. Oddly enough, it seems that this error happens after I've removed the drive, and reinserted it (it is a USB drive, so I click on the safely remove hardware first, which gives an error initially, then I click on it once more and it works).
Thanks,
Beth
Here's the code snippet:
[DllImport ("Kernel32.dll")]
private extern static bool CopyFileEx(String filename, String newFileName, CopyProgressDelegate callback, long param, bool cancel, uint copyFlags);if (!PerformActualCopy(sourceFilename, destFilename))
{
int lastError = RetrieveAnyError(); throw new ContentException("Unable to copy " + sourceFilename + " to "+ destFilename + " lastError = " + lastError);}
protected virtual int RetrieveAnyError(){
return GetLastError();}
protected virtual bool PerformActualCopy(string sourceFilename, string destFilename){
return CopyFileEx(sourceFilename, destFilename, new CopyProgressDelegate(cancelCopyIfCancelRequested), 0, true,COPY_FILE_FAIL_IF_EXISTS);
}

CopyFileEx and invalid parameter error
Rick Sivernell
Greazy
And the fourth parameter should be an IntPtr, not long (unless you run exclusively on Win64). Also you can't call GetLastError from managed code. Instead set the SetLastError property to true in the DllImport attribute and call Marshal.GetLastWin32Error.
DavidBB
Thank you both!
When I first got the reply from Sergey, I quickly realized that the fourth parameter should be the IntPtr. This helped, and initially I thought this completely solved the problem. But eventually I got the same error I had been getting with the CopyFileEx() method.
Then I got this post about GetLastError. I had no idea that it was not safe and reliable to use GetLastError(). I changed the code as described, and have done lots of testing throughout the day without getting any copy errors, so I believe this problem is solved.
Thank you both for your help.
Beth