We help IT Professionals succeed at work.
Get Started

Connecting to a ftp server in C# CompactFramework on pocket pc 2003

gilfrog asked
Last Modified: 2007-12-19
I  am having trouble connecting to a FTP server in C# CF pocket cp 2003.  It looks like i can import InternetOpenW, InternetConnectW and other functions from the wininet.dll.  But i can't seem to get it to work.  The internetOpen returns a good value but InternetConnect always returns 0.  I am connected to the internet, i have another ftp program that will connect from my pocket pc.  Here is the class.  I've seen some other custom written ftp samples but i'm a little leary of using them i would like to use the Win32 functions.  Does anybody have any idea?

public class ftp
            public const UInt32 INTERNET_OPEN_TYPE_PRECONFIG = 0;
            public const long INTERNET_OPEN_TYPE_DIRECT = 1;
            public const long INTERNET_OPEN_TYPE_PROXY = 3;
            public const long INTERNET_FLAG_RELOAD = 0x80000000;
            public const long FTP_TRANSFER_TYPE_UNKNOWN = 0;
            public const long FTP_TRANSFER_TYPE_ASCII = 1;
            public const long FTP_TRANSFER_TYPE_BINARY = 2;
            public const UInt16 INTERNET_DEFAULT_FTP_PORT = 21;
            public const UInt32 INTERNET_FLAG_PASSIVE = 0x08000000;
            public const UInt32 INTERNET_SERVICE_FTP = 1;

            [ DllImport("wininet.dll", EntryPoint="InternetConnectW", CharSet=CharSet.Unicode) ]
            private static extern Int32 InternetConnectW(Int32 hInternet, string ServerName, UInt16 ServerPort,
                  string sUserName, string sPassword, UInt32 nService,
                  UInt32 nFlags, UInt32 nContext);

            [ DllImport("wininet.dll", EntryPoint="InternetOpenW", CharSet=CharSet.Unicode) ]
            public static extern Int32 InternetOpenW(string sAgent, UInt32 nAccessType, string sProxy,
                  string sProxyBypass, UInt32 nFlags);

            [DllImport("coredll.dll", EntryPoint="FtpPutFileW")]
            public static extern long FtpPutFile(
                                                                        long hFtp,
                                                                        string lpszLocalFile,
                                                                        string lpszNewRemoteFile,
                                                                        long dwFlags,
                                                                        long dwContext);

            [DllImport("coredll.dll", EntryPoint="InternetCloseHandle")]
            public static extern long InternetCloseHandle(long hInet);

            [DllImport("coredll.dll", EntryPoint="GetLastError")]
            public static extern long GetLastError();

            private Int32 lInternetHandle = 0;
            private long lFtpHandle = 0;

            public bool InitializeFTP()
                  lInternetHandle = InternetOpenW("FTPOpen", INTERNET_OPEN_TYPE_PRECONFIG, "", "", 0);

                  if(lInternetHandle == 0)

                  return (lInternetHandle > 0);

            public bool ConnectToFTPServer(string strFTPServerName, string strUsername, string strPassword)
                  lFtpHandle = InternetConnectW(lInternetHandle, strFTPServerName,
                                          INTERNET_DEFAULT_FTP_PORT, strUsername, strPassword,
                                          INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);

                  if(lFtpHandle == 0)
                  return (lFtpHandle > 0);

            public bool PutFileOnFtPServer(string strSourceFilename, string strDestFilename, bool blnBinaryFile)
                  long lTranferredOK;
                  long lTransferType;
                        lTransferType = FTP_TRANSFER_TYPE_BINARY;
                        lTransferType = FTP_TRANSFER_TYPE_ASCII;

                  lTranferredOK = FtpPutFile(lFtpHandle, strSourceFilename,
                                                            strDestFilename, lTransferType, 0);

                  return (lTranferredOK > 0);

            public void CloseFTP()
Watch Question
This problem has been solved!
Unlock 2 Answers and 7 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE