title: FtpFindFirstFile error using wininet.dll
body: <p>I'm having a problem with my FtpFindFirstFile function on my C# project. Basically this function is just to search the specified directory for a file that I mention in my program, but an error appears right before the function is finish executing, here's the screenshot of the error:</p>
  
  <p>---------------<strong>START CODE</strong>------------------</p>
  
  <pre><code>[System.Runtime.InteropServices.DllImport("wininet.dll", EntryPoint = "InternetOpen", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  static extern IntPtr InternetOpen(
                string lpszAgent, int dwAccessType, string lpszProxyName,
                string lpszProxyBypass, int dwFlags);
  [System.Runtime.InteropServices.DllImport("wininet.dll", EntryPoint = "InternetConnect", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] 
  extern public static IntPtr  /*IntPtr*/ InternetConnect(
           IntPtr hInternet, string lpszServerName, int nServerPort,
           string lpszUsername, string lpszPassword, int dwService,
           int dwFlags, int dwContext);
  
      [System.Runtime.InteropServices.DllImport("wininet.dll", EntryPoint = "FtpFindFirstFile", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
      extern public static IntPtr FtpFindFirstFile(
          IntPtr hConnect, string searchFile, out WIN32_FIND_DATA findFileData,
          int flags, IntPtr context); 
  
      #region WIN32_Structure
  
      public struct WIN32_FIND_DATA
      {
          public int dwFileAttributes;
          public int nFileSizeHigh;
          public int nFileSizeLow;
          public int dwReserved0;
          public int dwReserved1;
          public string cFileName; 
          public string cAlternateFileName;
      }
      #endregion
  
      public void PerformFTP(string HostIP, string logUsrName, string LogPwd, string SendType, string DefaultDir, string fileExtension)
      {
          #region Declaration
          WIN32_FIND_DATA win32 = new WIN32_FIND_DATA();
          bool pRoceed;
          #endregion
          pRoceed = true;
  
          /*  Initialize Internet Connection */
          IntPtr hInternet = InternetOpen("browser", INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
          //IntPtr hInternet = InternetOpen("browser", 1, null, null, 0);
  
          if (hInternet == IntPtr.Zero)
          {
              MessageBox.Show(hInternet.ToString(), "");
              MessageBox.Show(System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString()); 
          }
  
          /*  Initialize FTP Connection  */
          IntPtr hFTPhandle = InternetConnect(hInternet, HostIP, INTERNET_DEFAULT_FTP_PORT, logUsrName, LogPwd, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
          //IntPtr hFTPhandle = InternetConnect(hInternet, "203.177.252.123", 21, "bomoracle", "bomoracle", 1, 0, 0);
  
          /*  To check if the FTP connection succeeded */
          if (hFTPhandle == IntPtr.Zero)
          {
              pRoceed = false;
              MessageBox.Show(hFTPhandle.ToString(), "");
              MessageBox.Show(System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString());
              return;
          }
  
          //IntPtr hFind = FtpFindFirstFile(hFTPhandle, "*.DAT" /*+ fileExtension*/ , out win32, 0, IntPtr.Zero);
  
          IntPtr hFind = FtpFindFirstFile(hFTPhandle, "*.DAT"  , out win32, 0, IntPtr.Zero); **//THIS IS WHERE THE ERROR APPEARS**
  
          if (hFind == IntPtr.Zero)
          {
              if (System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString() == "RROR_NO_MORE_FILES") 
              {
                  MessageBox.Show("NO MORE .BOM FILES","EMPTY");
              }
              MessageBox.Show("SEARCHING IN THE DIRECTORY FAILED! ", "EMPTY");
          }
  
      }
  </code></pre>
  
  <p>---------------<strong>END CODE</strong>------------------</p>
  
  <p>Here's the error message, it appears right before executing the if-else condition:</p>
  
  <p><strong>"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."</strong></p>
  
  <p>I don't know what's causing the error, I was just searching the directory, I haven't done any get or put command on that ftp process. Hope you can help! Thanks!</p>