WinCE CopyFile RAPI
-
Wednesday, November 16, 2011 7:31 PM
MODS: PLEASE MOVE THIS TO A MORE APPROPRIATE FORUM. HOPEFULLY ONE WITH BETTER TRAFFIC
I think something may be wrong with this code:
bytesread = localFile.Read(buffer, filepos, buffer.Length); while(bytesread > 0) { // move remote file pointer # of bytes read filepos += bytesread; // write our buffer to the remote file if(! Convert.ToBoolean(CeWriteFile(remoteFile, buffer, bytesread, ref byteswritten, 0))) { // check for success CeCloseHandle(remoteFile); throw new RAPIException("Could not write to remote file: " + RemoteFileName); } try { // refill the local buffer bytesread = localFile.Read(buffer, 0, buffer.Length); } catch(Exception) { bytesread = 0; } }
And for VB users:
bytesread = localFile.Read(buffer, filepos, buffer.Length) While bytesread > 0 ' move remote file pointer # of bytes read filepos += bytesread ' write our buffer to the remote file If Not CBool(CeWriteFile(remoteFile, buffer, bytesread, byteswritten, 0)) Then ' check for success CeCloseHandle(remoteFile) Throw New RAPIException("Could not write to remote file: " & RemoteFileName) End If Try ' refill the local buffer bytesread = localFile.Read(buffer, 0, buffer.Length) Catch generatedExceptionName As Exception bytesread = 0 End Try End While
when refilling the buffer, shouldn't the line @bytesread = localFile.Read(buffer, 0, buffer.Length);
actually read
bytesread = localFile.Read(buffer, filepos, buffer.Length);???
the thing is... It actually works the way it is!!!
I don't see how. if I change it to
bytesread = localFile.Read(buffer, filepos, buffer.Length);
will that break it?
the real JeZteR
Check out my Blog: Titan Blog
Check my unanswered questions: JeZteR's Unanswered Questions
All Replies
-
Wednesday, November 16, 2011 7:57 PM
maybe I don't understand what that parameter is for...
it seems to me
bytesread = localFile.Read(buffer, 0, buffer.Length);
would read the same spot over and over.
Ahhhh, when I call localfile.read <- that increments the internal pos counter of localfile itself. (localFile.Position)
so that solves that.
next question,
am i doing this right in the first place? I sawe another way to do that checks for remaining bytes and changes the buffer to that amount:
do { if (Information.UBound(bytFile) - TotalCopied > lBufferLen) { // Copy the next set of bytes lResult = CeWriteFile(lCeFileHandle, bytFile(BytePos), lBufferLen, lBytesWritten, 0L); TotalCopied = TotalCopied + lBytesWritten; // Unicode compensation. BytePos = BytePos + (lBufferLen / 2); Form1.Label6.Caption = "Bytes Copied: " + TotalCopied + " Down."; Form1.Label6.Refresh(); } else { // Copy the remaining bytes if greater than 0 lBufferLen = Information.UBound(bytFile) - TotalCopied; if (lBufferLen > 0) { // Copy remaining bytes at one time. lResult = CeWriteFile(lCeFileHandle, bytFile(BytePos), lBufferLen, lBytesWritten, 0L); } TotalCopied = TotalCopied + lBytesWritten; Form1.Label6.Caption = "Bytes Copied: " + TotalCopied + " Down."; Form1.Label6.Refresh(); bDoLoop = false; } } while bDoLoop;
in VB:Do If UBound(bytFile) - TotalCopied > lBufferLen Then ' Copy the next set of bytes lResult = CeWriteFile(lCeFileHandle, bytFile(BytePos), _ lBufferLen, lBytesWritten, 0&) TotalCopied = TotalCopied + lBytesWritten ' Unicode compensation. BytePos = BytePos + (lBufferLen \ 2) Form1.Label6.Caption = "Bytes Copied: " & _ TotalCopied & " Down." Form1.Label6.Refresh Else ' Copy the remaining bytes if greater than 0 lBufferLen = UBound(bytFile) - TotalCopied If lBufferLen > 0 Then ' Copy remaining bytes at one time. lResult = CeWriteFile(lCeFileHandle, _ bytFile(BytePos), lBufferLen, lBytesWritten, 0&) End If TotalCopied = TotalCopied + lBytesWritten Form1.Label6.Caption = "Bytes Copied: " & _ TotalCopied & " Down." Form1.Label6.Refresh Exit Do End If Loop
is my way handling that automatically or should I change and do it this other way?
the real JeZteR
Check out my Blog: Titan Blog
Check my unanswered questions: JeZteR's Unanswered Questions- Edited by JeZteRicp Wednesday, November 23, 2011 8:49 PM
-
Friday, November 18, 2011 8:53 AMModerator
Hi,
I think these codes are different from how to determine the left size after we copy the file buffer by fixed size.
But I did not know the detail in localFile.Read function, so that we can not sure if your codes get the right size of left buffer.
Best regards,
Jesse
Jesse Jiang [MSFT]
MSDN Community Support | Feedback to us
-
Friday, November 18, 2011 5:26 PM
i think since buffer is an array with 4000 elements localfile.read will fill that array with bytes until the last run thru the loop. At that point there may be less than 4000 bytes left, lets say 2000 so localfile.read fills the first 2000 elements with bytes and leaves the other 2000 elements empty.
I guess my question is:
does the CeWriteFile handle the extra empty elements or is having those going to cause invalid file or corrupt file or anything that may be a problem later down the road?
the real JeZteR
Check out my Blog: Titan Blog
Check my unanswered questions: JeZteR's Unanswered Questions -
Friday, November 18, 2011 5:34 PM
just to be safe, i did modify my code to check for the remainder and changes the buffer size to that amount. However, this slowed my transfer down considerably.
maybe there is a better way to do this?
bytesread = localFile.Read(buffer, filepos, buffer.Length); while(bytesread > 0) { // move remote file pointer # of bytes read filepos += bytesread; // write our buffer to the remote file if(! Convert.ToBoolean(CeWriteFile(remoteFile, buffer, bytesread, ref byteswritten, 0))) { // check for success CeCloseHandle(remoteFile); throw new RAPIException("Could not write to remote file: " + RemoteFileName); } try { // refill the local buffer if(localfile.Length - localfile.Position > buffer.length) { long remain = localfile.Length - localfile.Position; buffer = new byte[remain]; } bytesread = localFile.Read(buffer, 0, buffer.Length); } catch(Exception) { bytesread = 0; } }
in VB:bytesread = localFile.Read(buffer, filepos, buffer.Length) While bytesread > 0 ' move remote file pointer # of bytes read filepos += bytesread ' write our buffer to the remote file If Not CBool(CeWriteFile(remoteFile, buffer, bytesread, byteswritten, 0)) Then ' check for success CeCloseHandle(remoteFile) Throw New RAPIException("Could not write to remote file: " & RemoteFileName) End If Try ' refill the local buffer If localfile.Length - localfile.Position > buffer.length Then Dim remain As Long = localfile.Length - localfile.Position buffer = New Byte(remain - 1) {} End If bytesread = localFile.Read(buffer, 0, buffer.Length) Catch generatedExceptionName As Exception bytesread = 0 End Try End While
the real JeZteR
Check out my Blog: Titan Blog
Check my unanswered questions: JeZteR's Unanswered Questions- Proposed As Answer by Jesse JiangMicrosoft Contingent Staff, Moderator Wednesday, November 23, 2011 8:02 AM
- Edited by JeZteRicp Wednesday, November 23, 2011 8:56 PM
-
Wednesday, November 23, 2011 1:57 PM
I see that my previous post has been proposed as the answer...
but my previous post was actually a question...
so does that mean the way I'm doing it IS the best way to do it?
the real JeZteR
Check out my Blog: Titan Blog
Check my unanswered questions: JeZteR's Unanswered Questions

