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

gilfrog
gilfrog used Ask the Experts™
on
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)
                        MessageBox.Show("BAD");
                  else
                        MessageBox.Show("Good");

                  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)
                        MessageBox.Show("BAD");
                  else
                        MessageBox.Show("Good");
                  return (lFtpHandle > 0);
            }

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

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

                  return (lTranferredOK > 0);
            }

            public void CloseFTP()
            {
                  InternetCloseHandle(lFtpHandle);
                  InternetCloseHandle(lInternetHandle);
            }
      }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
have you checked the value of GetLastError() ? it should contain some more less useful error code...

you get the cleartext error message with

  TCHAR msg[512];

  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, msg, 512, 0, NULL);

(didn't compile this but should work ... :-)

regards
foo

Author

Commented:
It retrurn 12029 wich is ERROR_INTERNET_CANNOT_CONNECT.

I know it's connected because i can browse the web, sign into messenger and connect to network shares.

any ideas

Commented:
InternetOpen and InternetConnect both return a handle of type 'HINTERNET'

in your code you have:

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


due to the fact that InternetOpen works
 
>>   lInternetHandle = InternetOpenW(...)

Int32 seems to be the right type to encapsulate the native type HINTERNET

fix:

replace
 
    private long lFtpHandle = 0;

with

    private Int32 lFtpHandle = 0;


regards
foo
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
same thing it returns 0 error code 2027
Commented:
Try to make your sample work first in C++ so you're sure that there are no interop problems. Then try to do it over interop. And be aware of the limitations of WinInet (e.g. it cannot be used in services which have a GUI and the examples on MSDN are very dirty programmed). I wrote an HTTP client in C++ which uses WinInet and I would NEVER do it again. There are other components doing the job much better.
Commented:
I agree with ptmcomp - WinInet can be wery confusing - especially while dealing with proxy servers.

Maybe a .NET Compact Framework FTP component can help?
http://www.rebex.net/ftp.net/samples-compact-framework.aspx

Commented:
Hi
I would like to copy the Files through Wireless LAN using a FTP server from WinCE device to the Desktop server by giving the IP Address of the Server and File Name in Win Ce Device. Kindly show me a path to get ride of it. If you have solutions or Examples please let me know please.
Thanks inadvance.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial