How To Transfer Files from a PocketPC to an FTP Server

Exonerare de răspundere pentru conținutul retras din baza de cunoștințe

Acest articol a fost scris despre produsele pentru care Microsoft nu mai oferă asistență. Prin urmare, acest articol este oferit „ca atare” și nu mai va fi actualizat.

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://<myservername>) 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

  1. Create a new PocketPC application in eVB. Form1 is created by default.
  2. 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.
  3. 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 "FTP initialized"
    If ConnectToFTPServer(Text1.Text, "", "") Then
    MsgBox "Connected to server"
    PutFileOnFTPServer Text2.Text, _
    Text3.Text, blnBin
    MsgBox "File transferred"
    End If
    CloseFTP
    MsgBox "Connection closed"
    End If

    If Err.Number <> 0 Then MsgBox "Error in Command1_Click: " & _
    Err.Number & " - " & 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 = ""
    Text2.Text = ""
    Text3.Text = ""
    Label1.Caption = "FTP Server"
    Label2.Caption = "File on Device"
    Label3.Caption = "File on FTP Server"
    Check1.Caption = "Binary Transfer"
    Command1.Caption = "Transfer To Server"
    End Sub
  4. 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 "wininet" Alias "FtpPutFileW" ( _
    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 "wininet" ( _
    ByVal hInet As Long) As Long

    Declare Function InternetConnect Lib "wininet" Alias "InternetConnectW" ( _
    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 "wininet" Alias "InternetOpenW" ( _
    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 "coredll" () 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("eVB OpenUrl", _
    INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    If Err.Number <> 0 Or lngInternetHandle = 0 Then
    MsgBox "Failed to initialize communications to transfer data" & _
    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 "Failed to connect to server to transfer data. Error: " & _
    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 "Transfer of file " & strSourceFilename & _
    " to server failed. Error: " & CStr(GetLastError())
    Else
    MsgBox "Transfer of file" & strSourceFilename & " succeeded!"
    End If
    PutFileOnFTPServer = blnTransferredOK

    End Function

    Public Sub CloseFTP()

    On Error Resume Next

    InternetCloseHandle lngFtpHandle
    InternetCloseHandle lngInternetHandle

    End Sub
  5. Run the project on the PocketPC device.
  6. 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).
  7. 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.
  8. In the File on FTP Server text box, type the name of the destination file on the server.
  9. Click the command button. Notice that several message boxes appear, which state that the file has been transferred successfully.
  10. Go to the FTP directory of the IIS FTP server, and verify that the file exists there.

References

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

312039 BUG: Error Message When You Call FtpGetFile and FtpPutFile Functions: 0x00000057 - ERROR_INVALID_PARAMETER
Proprietăți

ID articol: 305598 - Ultima examinare: 13 iul. 2004 - Revizie: 1

Feedback