Page 1
Page 2

This
first function first calls CeFindFirstFile to check if the file which it
is to write to on the Pocket PC exists. If so, the routine will exit. It would
be simple enough to modify this to prompt for overwrite etc. The next action performed
is to call CeCreateFile. This function has a multitude of uses and is used
both to read files from the device, and write to them. The best way I can describe
it really is not CreateFile but 'Create me a File Handle for this file with these
settings'. We are using GENERIC_WRITE which means writing to the file, FILE_SHARE_READ
which indicates no-one else can open this file for writing whilst we are accessing
it, CREATE_ALWAYS which means create the file no-matter what, and FILE_ATTRIBUTES_NORMAL
which means we don't wish to set any system or read-only type attributes on the
file. Next we use the ReadFileAsBinary function (which is a little further
on) to read the client file from the PC into a Byte array. This buffer of Bytes
is then passed into the CeWriteFile routine, along with the file handle
given to us by the CeCreateFile function, the number of bytes in the buffer,
and a variable to receive how many bytes have been written by the call.
If
we receive a non-zero value from the call, it has been a success.
'==================================================
Public
Function CopyFileFromPocketPC() As Boolean
Dim bytBuffer(16384)
As Byte
Dim lngFileHandle As Long
Dim lngBytesRead As
Long
Dim typFindFileData As CE_FIND_DATA
Dim intFreeFileID
As Integer
Dim intWriteLoop As Integer
'locate
the file, and see if it already exists on the device
lngFileHandle = CeFindFirstFile(Form1.txtPPCFile.Text,
typFindFileData)
If lngFileHandle = INVALID_HANDLE Then
MsgBox
"File " & Form1.txtPPCFile.Text & " Not Found. Operation
Aborted.", vbOKOnly
CopyFileFromPocketPC = False
Exit Function
End
If
'we dont need this handle now that we know the file
is there
CeFindClose lngFileHandle
'i know it seems
odd to have to call a function called CreateFile to read a file
'but what it
really refers to is create me a handle to a file of this type.
lngFileHandle
= CeCreateFile(Form1.txtPPCFile.Text, GENERIC_READ, FILE_SHARE_READ, vbNullString,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
If lngFileHandle
= INVALID_HANDLE Then
MsgBox "Failed to open file " & Form1.txtPPCFile.Text
CopyFileFromPocketPC
= False
Exit Function
End If
intRetVal = CeReadFile(lngFileHandle,
bytBuffer(0), 16384, lngBytesRead, 0)
'if we got a 0 return
value from readfile then there is an error
'so pass it to the error handler
If
intRetVal = READ_ERROR Then
GoTo ErrHandler
End If
intFreeFileID
= FreeFile
Open Form1.txtDesktopFile.Text For Binary As
intFreeFileID
For intWriteLoop = 0 To lngBytesRead
Put #intFreeFileID, intWriteLoop
+ 1, bytBuffer(intWriteLoop)
Next intWriteLoop
Close
intFreeFileID
CopyFileFromPocketPC = True
Exit
Function
ErrHandler:
MsgBox "Error
" & CeGetLastError & " Occurred When Copying File " &
_
Form1.txtPPCFile.Text & " From The Device as " & Form1.txtDesktopFile.Text,
vbCritical & vbOKOnly
CopyFileFromPocketPC = False
End
Function
'=============================================
The
second function starts off in a very similar fashion to the first (I did think
about making the call to CeFindFirstFile a separate function call to tidy
the code up, but I have to leave you guys some things to play with), with a call
to CeFindFirstFile. After all, if the file that we are trying to read doesnt
exist, there really isnt any point trying to read it. Then again we call CeCreateFile.
This time we are using GENERIC_READ as we are reading, FILE_SHARE_READ which indicates
no-one else can open this file for writing whilst we are accessing it, OPEN_EXISTING
to retrieve a file handle to open an existing file, and FILE_ATTRIBUTES_NORMAL
which means we don't wish to set any system or read-only type attributes on the
file. Next we use the CeReadFile function to retrieve a maximum of 16384
bytes, from the file handle provided by CeCreateFile, into another array
of bytes. This byte buffer is filled with bytes from the file on the CE device,
and the number of Bytes read and placed in the buffer is written into lngBytesRead.
If
we receive a non-zero value from the call, it has been a success, so we can carry
on and write the file to the pc.
We now use a simple
piece of Visual basic to create a new file on the local pc, opened in Binary mode,
and loop through the byte buffer, writing out the contents, byte by byte, into
the PC file.
'=============================================
Private
Function ReadFileAsBinary(strSrcFilename As String, lngFileSize As Long, bytBuffer()
As Byte) As Boolean
Dim intFileHandle As Integer
Dim
intSeekPos As Integer
'On Error GoTo ErrHandler
lngFileSize
= FileLen(strSrcFilename)
intFileHandle = FreeFile
Open strSrcFilename For Binary As intFileHandle
For intSeekPos
= 1 To lngFileSize
Get #intFileHandle, intSeekPos, bytBuffer(intSeekPos -
1)
Next intSeekPos
Close intFileHandle
ReadFileAsBinary
= True
Exit Function
ErrHandler:
MsgBox "Error reading file " & strSrcFilename, vbCritical &
vbOKOnly
ReadFileAsBinary = False
End Function
'==================================================
And
that ladies and Gentlemen, is how we can delve into the murky depths of the Remote
API, and, with a little VB know how, read and write files between the devices.
Previous Page