Page 1
Page 2
Read
Part One
In the first article in this two part series,
I discussed how we can copy Access Databases to Pocket Access Databases, and back
again, with the help of a few ActiveSync API calls and a little bit of VB6. This
article is designed to show how we can copy any sort of file between the PC and
the device.
The first thing you will need before we start,
is to download the attached sample code. Contained within the sample code file
is a file called RemoteCE.tlb
This file is an invaluable
freeware Type Library, which I found at http://www.conduits.com/ce/dev/vbrapi.htm
that contains pretty much every Remote API Call (RAPI) definition within the Windows
CE Remote API. This saved me days of pulling my hair out trying to work out the
correct definitions for Function Calls and their custom data types.
This
file is purely to assist developers, it does not need to be distributed with your
projects.
Create a new Visual Basic 6 project. Go to
the Projects, References option, and click browse. Locate the RemoteCE.tlb file
and add it. You now have a whole series of extra functions and user defined types
available to you. for those of you familiar with VB, press F2 to open the object
browser, select RemoteCETypeLibrary from the top left drop down, and you will
be presented with all the definitions. most of the function calls are held within
the CERAPI class.
Close the object browser once you are
done giving yourself a headache at what you would have otherwise had to figure
out on your own, and create a new form in the VB project.
Create
two new text fields on the form, called txtDesktopFile and txtPPCFile,
and place labels next to them reading 'Desktop File' and 'Pocket PC file' respectively.
Place
two command buttons on the form labelling them 'Download' and 'Upload'.
Within
the Click event for the Download button, place the following code:
'==================================================
On Error Goto ErrHandler
If CopyFileToPocketPC = False
Then
msgbox "An error occurred transferring the file"
Else
msgbox "File copied successfully."
End If
Exit Sub
ErrHandler:
MsgBox Err.Number & "
" & Err.Description, _
vbOKOnly + vbCritical
'==================================================
And
within the Click Event for the Upload button, place the following code:
'==================================================
On Error Goto ErrHandler
If CopyFileFromPocketPC = False
Then
msgbox "An error occurred transferring the file"
Else
msgbox "File copied successfully."
End If
Exit Sub
ErrHandler:
MsgBox Err.Number & "
" & Err.Description, _
vbOKOnly + vbCritical
'==================================================
Within
the Form_Load event place the following code:
'==================================================
dim
intRetVal as integer
intRetVal = CeRapiInit()
If intRetVal <> INIT_SUCCESS Then
MsgBox "Failed to initialise
RAPI with device with error " & _
CeGetLastError
CeRapiUninit
End
End If
'==================================================
and
within the Form_Unload event place the following code:
'==================================================
CeRapiUninit
'==================================================
These
two events ensure that when you start the program, an initialisation call is made
to the device, and when you end the program the api is shutdown properly.
These
calls could just as easily be placed inside the following functions (and probably
should be, to make for a more stable application!)
Now,
create a new module in your project called modGeneralFunctions
Within
this module, create the following Constants:
Public Const
INVALID_HANDLE = -1
Public Const GENERIC_READ = &H80000000
Public Const
GENERIC_WRITE = &H40000000 '(1073741824)
Public Const FILE_SHARE_READ =
1
Public Const FILE_SHARE_WRITE = 2
Public Const CREATE_ALWAYS = 2
Public
Const OPEN_EXISTING = 3
Public Const INIT_SUCCESS = 0
Public Const WRITE_ERROR
= 0
Public Const READ_ERROR = 0
and add these three
functions to your form's code window as follows:
'==================================================
Public
Function CopyFileToPocketPC() As Boolean
Dim bytBuffer(16384)
As Byte
Dim lngFileHandle As Long
Dim lngDestinationHandle As Long
Dim
lngNumBytesWritten As Long
Dim filename As String
Dim typFindFileData As
CE_FIND_DATA
Dim lngFileSize As Long
'On Error GoTo
ErrHandler
'locate the file, and see if it already exists
on the device
lngFileHandle = CeFindFirstFile(Form1.txtPPCFile.Text, typFindFileData)
'if
we get -1 then the file wasn't found so we can go ahead and create it
'otherwise
we dont want to overwrite in this demo, so we will cancel the operation.
If
lngFileHandle <> INVALID_HANDLE Then
MsgBox txtPPCFile & " already
exists. Operation Cancelled."
CeFindClose lngFileHandle
CopyFileToPocketPC
= False
End If
'create the file on the device, and return
the handle to the file
filename = Form1.txtPPCFile.Text
lngDestinationHandle
= CeCreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, vbNullString, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0)
'if something went wrong in CeCreateFile
we will get -1, and therefore need to abort
If lngDestinationHandle = INVALID_HANDLE
Then
MsgBox "CeCreateFile Error " & CeGetLastError & "
occurred.", vbCritical & vbOKOnly
CopyFileToPocketPC = False
Exit
Function
End If
'Call function to read file on the desktop
into the byte buffer
If ReadFileAsBinary(Form1.txtDesktopFile.Text, lngFileSize,
bytBuffer()) = True Then
'Write contents of Binary buffer
to CE file.
'Return value of 0 = failure, non zero = success
intRetVal =
CeWriteFile(lngDestinationHandle, bytBuffer(0), lngFileSize, lngNumBytesWritten,
0)
If intRetVal = WRITE_ERROR Then
GoTo ErrHandler
End
If
End If
CeCloseHandle lngDestinationHandle
CopyFileToPocketPC
= True
Exit Function
ErrHandler:
MsgBox
"Error " & CeGetLastError & " Occurred When Copying File
" & _
Form1.txtDesktopFile.Text & " To The Device as "
& Form1.txtPPCFile.Text, vbCritical & vbOKOnly
If
lngDestinationHandle Or lngDestinationHandle <> INVALID_HANDLE Then
CeCloseHandle
lngDestinationHandle
End If
CopyFileToPocketPC = False
End
Function
'==================================================
Next Page