Microsoft KB Archive/305598

= How To Transfer Files from a PocketPC to an FTP Server =

Article ID: 305598

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft eMbedded Visual Basic 3.0, when used with:
 * Microsoft Windows CE Platform Software Development Kit for Handheld PC 2000

-



This article was previously published under Q305598



SUMMARY
This article illustrates how to use eMbedded Visual Basic 3.0 to programmatically transfer files from a PocketPC device to a Microsoft Internet Information Server (IIS) File Transfer Protocol (FTP) server on the Internet.



MORE INFORMATION
To perform this transfer, you can use application programming interface (API) calls to Wininet.dll. However, before you can call these APIs, you must ensure that the following conditions are met:
 * The PocketPC must be connected to the network.
 * The IIS FTP server on the desktop must be running. To verify this, go to the FTP site (for example, ftp://) in Pocket Internet Explorer on the device.
 * The IIS FTP server must allow Anonymous connections.
 * The IIS FTP server must allow both Read and Write access on the FTP directory.

To configure Windows 2000 so that it allows for Anonymous connections and Read and Write access to the FTP directory, follow these steps:
 * 1) From the Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager to open the Internet Services Manager.
 * 2) Click to expand the server name. Right-click the default FTP site, and then click Properties.
 * 3) In the Properties dialog box, on the Security Accounts tab, make sure that the Allow Anonymous Connections check box is selected.
 * 4) On the Home Directory tab, make sure that both the Read and Write check boxes are selected for FTP Site Directory.

After you configure the IIS FTP server as above, follow the steps below to create the eMbedded Visual Basic (eVB) application.

Step-by-Step Example
 Create a new PocketPC application in eVB. Form1 is created by default. Add three labels, three text boxes, one check box, and one command button to Form1. Do not be concerned with the placement of the controls.  Paste the following code into Form1: Option Explicit

Private Sub Command1_click On Error Resume Next Dim blnBin As Boolean blnBin = False If Check1.Value = 1 Then blnBin = True If InitializeFTP Then MsgBox &quot;FTP initialized&quot; If ConnectToFTPServer(Text1.Text, &quot;&quot;, &quot;&quot;) Then MsgBox &quot;Connected to server&quot; PutFileOnFTPServer Text2.Text, _ Text3.Text, blnBin MsgBox &quot;File transferred&quot; End If       CloseFTP MsgBox &quot;Connection closed&quot; End If   If Err.Number <> 0 Then MsgBox &quot;Error in Command1_Click: &quot; & _ Err.Number & &quot; - &quot; & Err.Description

End Sub

Private Sub Form_OKClick App.End End Sub

Private Sub Form_Load Label1.Move 120, 120, 1575, 255 Label2.Move 120, 720, 1575, 255 Label3.Move 120, 1320, 1575, 255 Text1.Move 240, 360, 2895, 255 Text2.Move 240, 960, 2895, 255 Text3.Move 240, 1560, 2895, 255 Command1.Move 120, 1960, 3135, 495 Check1.Move 1560, 3120, 2775, 255 Text1.Text = &quot;&quot; Text2.Text = &quot;&quot; Text3.Text = &quot;&quot; Label1.Caption = &quot;FTP Server&quot; Label2.Caption = &quot;File on Device&quot; Label3.Caption = &quot;File on FTP Server&quot; Check1.Caption = &quot;Binary Transfer&quot; Command1.Caption = &quot;Transfer To Server&quot; End Sub   Add a module to the project, and paste the following code to the new module: Option Explicit

Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0 Public Const INTERNET_OPEN_TYPE_DIRECT = 1 Public Const INTERNET_OPEN_TYPE_PROXY = 3 Public Const INTERNET_FLAG_RELOAD = &H80000000

Public Const FTP_TRANSFER_TYPE_UNKNOWN As Long = 0 Public Const FTP_TRANSFER_TYPE_ASCII As Long = 1 Public Const FTP_TRANSFER_TYPE_BINARY As Long = 2

Public Const INTERNET_DEFAULT_FTP_PORT As Long = 21 Public Const INTERNET_FLAG_PASSIVE As Long = &H8000000 Public Const INTERNET_SERVICE_FTP As Long = 1

Declare Function FtpPutFile Lib &quot;wininet&quot; Alias &quot;FtpPutFileW&quot; ( _   ByVal hFtp As Long, _    ByVal lpszLocalFile As String, _    ByVal lpszNewRemoteFile As String, _    ByVal dwFlags As Long, _    ByVal dwContext As Long) As Long

Declare Function InternetCloseHandle Lib &quot;wininet&quot; ( _   ByVal hInet As Long) As Long

Declare Function InternetConnect Lib &quot;wininet&quot; Alias &quot;InternetConnectW&quot; ( _   ByVal hInet As Long, _    ByVal lpszServerName As String, _    ByVal nServerPort As Long, _    ByVal lpszUsername As String, _    ByVal lpszPassword As String, _    ByVal dwService As Long, _    ByVal dwFlags As Long, _    ByVal dwContext As Long) As Long

Declare Function InternetOpen Lib &quot;wininet&quot; Alias &quot;InternetOpenW&quot; ( _   ByVal lpszAgent As String, _    ByVal dwAccessType As Long, _    ByVal lpszProxyName As String, _    ByVal lpszProxyBypass As String, _    ByVal dwFlags As Long) As Long

Declare Function GetLastError Lib &quot;coredll&quot; As Long

Public lngInternetHandle As Long Public lngFtpHandle As Long Public hOpenUrl As Long

Public Function InitializeFTP As Boolean On Error Resume Next Dim sUrl As String lngInternetHandle = InternetOpen(&quot;eVB OpenUrl&quot;, _       INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0) If Err.Number <> 0 Or lngInternetHandle = 0 Then MsgBox &quot;Failed to initialize communications to transfer data&quot; & _ vbCrLf & Err.Description InitializeFTP = False Else InitializeFTP = True End If

End Function

Public Function ConnectToFTPServer(ByVal strFTPServerName As String, _   ByVal strUsername As String, ByVal strPassword As String) As Boolean On Error Resume Next

lngFtpHandle = InternetConnect(lngInternetHandle, strFTPServerName, _       INTERNET_DEFAULT_FTP_PORT, strUsername, strPassword, _        INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0) If lngFtpHandle = 0 Or Err.Number <> 0 Then MsgBox &quot;Failed to connect to server to transfer data. Error: &quot; & _ CStr(GetLastError) ConnectToFTPServer = False Else ConnectToFTPServer = True End If

End Function

Public Function PutFileOnFTPServer(ByVal strSourceFilename As String, _   ByVal strDestFilename As String, _    ByVal blnBinaryFile As String) As Boolean On Error Resume Next

Dim blnTransferredOK As Boolean Dim lngTransferType As Long If blnBinaryFile Then lngTransferType = FTP_TRANSFER_TYPE_BINARY Else lngTransferType = FTP_TRANSFER_TYPE_ASCII End If   blnTransferredOK = FtpPutFile(lngFtpHandle, strSourceFilename, _        strDestFilename, lngTransferType, 0) If blnTransferredOK = 0 Then MsgBox &quot;Transfer of file &quot; & strSourceFilename & _ &quot; to server failed. Error: &quot; & CStr(GetLastError) Else MsgBox &quot;Transfer of file&quot; & strSourceFilename & &quot; succeeded!&quot; End If   PutFileOnFTPServer = blnTransferredOK

End Function

Public Sub CloseFTP On Error Resume Next InternetCloseHandle lngFtpHandle InternetCloseHandle lngInternetHandle

End Sub  Run the project on the PocketPC device. In the FTP Server text box, type the server name.

NOTE: Do not type a URL such as ftp://<myservername>. You will receive error 12007 because the server name cannot be resolved. Instead, you must only type the server name (for example, myftpserver ).</li> In the File on Device text box, type the name of the file on the device that needs to be transferred to the IIS FTP server. For example, type \My Documents\test.txt .</li> In the File on FTP Server text box, type the name of the destination file on the server.</li> Click the command button. Notice that several message boxes appear, which state that the file has been transferred successfully.</li> Go to the FTP directory of the IIS FTP server, and verify that the file exists there.</li></ol>

<div class="references_section">