DEVBUZZ Homepage Copy any file from the desktop to the Pocket PC and vice versa.
 
Web www.devbuzz.com
  HOME PAGE
  All Articles
  Advertise
  Consulting

 Development
  Discuss - Forums
  Still in the box?
  .Compact Framework
  Code Snippets
  SQL Server CE
  Database
  MS Resources
 Stores
  Developer Controls
  Pocket PC Hardware
  Pocket PC Software
  Pocket PC Books
  .NET CF Books
  Book Reviews
  SPB SW Discounts
  RESCO SW Discounts
 DEVBUZZ Info
  About Us
  Help
  Join our email list
  Links & Ratings
  Press & Comments
  Pocket PC version
  Software Reviews
  Hardware Reviews
 Authors
  Authors
  Article Guide
  Competitions
 Resources
  Developers
  Register
  Login

  SPB Discounts!
 Columnists
  Rick Winscot
 Past Blast
  Personal Media Ctr
  Gizmobility
  eVB Legacy
  Old news
  Hosted Software
  Wireless
  Newsletters
  Carl Davis
  Upton Au

 Pocket PC Registry
  Join the registry
  View current list
 Current Poll
Are you converting to .NET Compact Framework?
Yes, it has changed my life!
No, I'm sticking with eVB
.NET CF what's that`?

Current results
3431 votes so far
 Recent Forum Threads [goto forums]

Get Computername
read... (67 hits)


Great aid to development productivity
read... (82 hits)


ThreadingTimer sample code
read... (143 hits)


Multithreading with .NET CF
read... (194 hits)


Moving from eMbedded Visual Basic to Visual Basic .NET
read... (166 hits)


.NET Compact Framework 2.0 Service Pack 2
read... (226 hits)


Transfer Data from SQL Server 2000 to SQL Server Compact Edition
read... (298 hits)


This protocol version is not supported
read... (236 hits)


Converting Lowercase to uppercase wont work
read... (203 hits)


Direct access to MS SQL Server 2000
read... (374 hits)


Creating SDF file in Desktop
read... (513 hits)


Winsock in CF.NET
read... (316 hits)


Using Pocket Outlook to submit HTML page form with MAILTO action
read... (420 hits)


Missing file "System.Data.PocketPC.asmmeta.dll"
read... (268 hits)


HP iPAQ hw6915 Serial Port Issue
read... (309 hits)


Info on the recent forum changes
read... (341 hits)


SqlServer tools from Redgate
read... (383 hits)


Arrow keys and Hardware navigation button
read... (393 hits)


O2 XDA lls pin sync cable to comport
read... (322 hits)


Creating dynamic folders on Pocket PC OS
read... (299 hits)

Custom Windows Mobile software development.
LBS Challenge 2007
LBS Challenge Eight previous NAVTEQ Global LBS Challenge® participants have received venture capital funding and nine past LBS Challenge winners have launched commercial applications on major wireless carriers. Register your non-commercial LBS application in the 2007 NAVTEQ Global LBS Challenge in one of three regions: Americas, Europe-Middle East-Africa (EMEA) or Asia-Pacific(APAC). You could win a share of $2 million in prizes. This could be your year.
Dream. Develop. Win.

Development | Starting Out

Copy any file from the desktop to the Pocket PC and vice versa.
Written by Richard Hands  [author's bio]  [read 82109 times]
Edited by Derek

Download the code   Discuss this article   eVB Ver 3.0   

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 

Back to Starting Out | [Article Index]

 

Back to the top of the page.
Chris De Herrera's Windows CE Website Windows CE News & Information Source
Copyright ©2000-2007 by DEVBUZZ.COM, Inc., NJ. USA.MSDEVELOP