none
Accessing FTP using wininet.dll - getting error 87 RRS feed

  • Question

  • Hi

    I hope this is the right place.

    i have written some code to access an FTP and download files.

    i get the following error "Message: IOpen failed, error value: 87 - Unknown error code: 87"

    My calling code looks like this
                string strLogSource = "startGJdownload(): ";

                // DEBUG LOGGER
                _log.Debug("{0}", strLogSource);

                int        iFiles            = 0;
                bool    downloaderror    = false;

                FtpDll myFtpDll = new FtpDll();

                // DEBUG LOGGER
                _log.Debug("{0}myFtpDll.IOpen();", strLogSource);

                myFtpDll.IOpen();

                // DEBUG LOGGER
                _log.Debug("{0}myFtpDll.IConnect({1}, {2}, {3});",
                    strLogSource,
                    m_strFtpAddress,
                    m_strFtpUsername,
                    m_strFtpPassword);

                myFtpDll.IConnect(m_strFtpAddress, m_strFtpUsername, m_strFtpPassword);
               
                if (m_strFtpDirectory.Trim() != "")
                    myFtpDll.FTPChangeDir(m_strFtpDirectory);
               
                string rixmlFileName    = "";
                FtpFileData fdata        = myFtpDll.FTPFirstFile("*.xml");
                rixmlFileName            = fdata.FileName;

                // DEBUG LOGGER
                _log.Debug("{0}First file: {1}",
                    strLogSource,
                    rixmlFileName);

    and the top part of the class that implements the wininet.dll API
            public class FtpDll : LoggingUserBase
            {
                //DECLARATIONS
                [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
                public static extern uint SetLastError(uint uiErrorCode);

                [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
                public static extern uint GetLastError();

                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern bool InternetGetLastResponseInfo(ref uint ulError, [MarshalAs(UnmanagedType.LPTStr)] string strBuffer, ref uint ulBufferLength);

                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern IntPtr InternetOpen(string strAppName, ulong ulAccessType, string strProxy, string strProxyBypass, ulong ulFlags);

                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern IntPtr InternetConnect(IntPtr ulSession, string strServer, uint ulPort, string strUser, string strPassword, uint ulService, uint ulFlags, uint ulContext);

                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern bool InternetGetConnectedState(ref uint ulFlags, uint ulReserved);

                //        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                //        public static extern bool FtpGetCurrentDirectory(IntPtr ulSession, string strPath, ulong ulBuffLength);
                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern bool FtpSetCurrentDirectory(IntPtr ulSession, string strPath);

                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern IntPtr FtpFindFirstFile(IntPtr ulSession, string strPath, [In, Out] FileData dirData, ulong ulFlags, ulong ulContext); //returns handle for InternetFindNextFile

                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern IntPtr InternetFindNextFile(IntPtr ulSession, [In, Out] FileData dirData); //returns handle for InternetFindNextFile

                //        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                //        public static extern bool InternetFindNextFile(ulong ulFindFirst, [In, Out] FileData dirData);
                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern bool FtpGetFile(IntPtr ulSession, string strRemoteFile, string strLocalFile, bool bolFailIfExist, ulong ulFlags, ulong ulInetFals, ulong ulContext);
                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern bool FtpPutFile(IntPtr ulSession, string strLocalFile, string strRemoteFile, ulong ulFlags, ulong ulContext);
                //        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                //        public static extern bool FtpRenameFile(IntPtr ulSession, string strExisting, string strNew);
                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern bool FtpDeleteFile(IntPtr ulSession, string strFileName);
                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern bool InternetCloseHandle(IntPtr ulSession);
       
                //CONSTANTS - there are plenty more of these, only the important ones are here!
                //InternetOpen:aul_AccessType flags
                public const uint OPEN_TYPE_PRECONFIG = 0;
                public const uint OPEN_TYPE_DIRECT = 1;
                public const uint OPEN_TYPE_GATEWAY = 2;
                public const uint OPEN_TYPE_PROXY = 3;
                //Ports
                public const uint INVALID_PORT_NUMBER = 0;
                public const uint DEFAULT_FTP_PORT = 21;
                public const uint DEFAULT_GOPHER_PORT = 70;
                public const uint DEFAULT_HTTP_PORT = 80;
                public const uint DEFAULT_HTTPS_PORT = 443;
                public const uint DEFAULT_SOCKS_PORT = 1080;
                //Service/Command types
                public const uint SERVICE_FTP = 1;
                public const uint SERVICE_GOPHER = 2;
                public const uint SERVICE_HTTP = 3;
                //Internet connection flags
                public const uint CONNECTION_MODEM = 1;
                public const uint CONNECTION_LAN = 2;
                public const uint CONNECTION_PROXY = 4;
                public const uint CONNECTION_MODEM_BUSY = 8;
                //FTP transfer flags
                public const uint FTP_TRANSFER_TYPE_ASCII = 1;
                public const uint FTP_TRANSFER_TYPE_BINARY = 2;

                private Peresys.Logging.ILog _log = Peresys.Logging.LogManager.GetLogger(typeof(FtpDll));

                //VARIABLES
                IntPtr hInternetSession = new IntPtr(0); //handle to the Internet session
                IntPtr hInternetConnection = new IntPtr(0); //handle to the Internet connection
                uint intErrorValue = 0; //error value
                string strErrorText = "";
                FileData myFileData;
                //bool bolDebug = true;
                bool LogSpecialCaseErrors = false;

                public FtpDll()
                { //constructor
                    myFileData = new FileData(); //instance the FileData class to hold the dir info
                }
           
                public void IOpen()
                {
                    string strProxy = "";
                    string strProxyBypass  = "";
                    try
                    {
                        ClearError(); //clear the 'last error' value
                        hInternetSession = FtpDll.InternetOpen("FTPTest", FtpDll.OPEN_TYPE_PRECONFIG, strProxy, strProxyBypass, 0);
                        if (GetError())
                        {
                            if (intErrorValue != 127) //hum... seem to always get 127 back!?!
                                throw new Exception("IOpen failed, error value: " + intErrorValue + " - " + strErrorText);
                        }
                    }
                    catch (Exception e)
                    {
                        //if (bolDebug) Logger.Error(e.Message);
                        _log.Error(e);
                        throw e; //pass this exception back to the caller
                    }
                }

    any ideas why i am getting the error?

    Thanks
    Jason
    Wednesday, April 25, 2007 8:02 AM

Answers

  • Thanks for the reply

    error code 87 actually means invalid parameter, my GetError() function is returning the text "Unknown error code: "

    my dll import declaration was incorrect, seems that IE 7 (or at least the updated wininet.dll that comes with IE 7) is more strict wrt parameters???

    i had to change declaration to
                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern IntPtr InternetOpen(string strAppName, uint ulAccessType, string strProxy, string strProxyBypass, uint ulFlags);

    instead of ulong, for some of the parameters, they had to be uint

    i am now getting the following error on my InternetConnect function:
    Message: IConnect failed, error value: 1008 - Unknown error code: 1008

    IConnect looks like this
                public void IConnect(string strHostURL, string strUser, string strPW)
                {
                    if (hInternetSession == IntPtr.Zero)
                        IOpen(); //we always need a session first
                    try
                    {
                        ClearError(); //make sure we are clean before starting to connect
                        hInternetConnection = FtpDll.InternetConnect(hInternetSession, strHostURL, FtpDll.DEFAULT_FTP_PORT, strUser, strPW, FtpDll.SERVICE_FTP, 0, 0);
                        if (GetError())
                            throw new Exception("IConnect failed, error value: " + intErrorValue + " - " + strErrorText);
                        if (hInternetConnection == IntPtr.Zero) //if the handle is still zero....
                            throw new Exception("Failed to get a handle in IConnect - no error code given!");
                    }
                    catch (Exception e)
                    {
                        _log.Error(e);
                        //if (bolDebug) Logger.Error(e.Message);
                        throw e; //pass this exception back to the caller
                    }
                }

    anyone know what error 1008 is?

    Thanks again
    Wednesday, April 25, 2007 9:54 AM

All replies

  • I never used InternetOpen with setting for proxy, but i gues that you need to put INTERNET_OPEN_TYPE_PROXY in the accessType parameter. Also you need to check last options parameter. I used INTERNET_FLAG_NO_CACHE_WRITE.
    Wednesday, April 25, 2007 9:39 AM
  • Thanks for the reply

    error code 87 actually means invalid parameter, my GetError() function is returning the text "Unknown error code: "

    my dll import declaration was incorrect, seems that IE 7 (or at least the updated wininet.dll that comes with IE 7) is more strict wrt parameters???

    i had to change declaration to
                [DllImport("wininet.dll", CharSet = CharSet.Auto)]
                public static extern IntPtr InternetOpen(string strAppName, uint ulAccessType, string strProxy, string strProxyBypass, uint ulFlags);

    instead of ulong, for some of the parameters, they had to be uint

    i am now getting the following error on my InternetConnect function:
    Message: IConnect failed, error value: 1008 - Unknown error code: 1008

    IConnect looks like this
                public void IConnect(string strHostURL, string strUser, string strPW)
                {
                    if (hInternetSession == IntPtr.Zero)
                        IOpen(); //we always need a session first
                    try
                    {
                        ClearError(); //make sure we are clean before starting to connect
                        hInternetConnection = FtpDll.InternetConnect(hInternetSession, strHostURL, FtpDll.DEFAULT_FTP_PORT, strUser, strPW, FtpDll.SERVICE_FTP, 0, 0);
                        if (GetError())
                            throw new Exception("IConnect failed, error value: " + intErrorValue + " - " + strErrorText);
                        if (hInternetConnection == IntPtr.Zero) //if the handle is still zero....
                            throw new Exception("Failed to get a handle in IConnect - no error code given!");
                    }
                    catch (Exception e)
                    {
                        _log.Error(e);
                        //if (bolDebug) Logger.Error(e.Message);
                        throw e; //pass this exception back to the caller
                    }
                }

    anyone know what error 1008 is?

    Thanks again
    Wednesday, April 25, 2007 9:54 AM
  • I found this at http://msdn2.microsoft.com/en-us/library/ms681383.aspx

    ERROR_NO_TOKEN
    1008 An attempt was made to reference a token that does not exist.

    ... i am not sure how to fix this...
    Wednesday, April 25, 2007 10:09 AM