Re: FTP

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Jim Edgar (djedgarNOSPAM_at_cox.net)
Date: 12/15/04


Date: Tue, 14 Dec 2004 16:25:41 -0800

Thanks Tom --

I've never used .net before so translating some of the code is hit and miss. Here's
my current configuration of WIN32_FIND_DATA

Public Structure WIN32_FIND_DATA
 Dim dwFileAttributes As Integer
 Dim ftCreationTime As FILETIME
 Dim ftLastAccessTime As FILETIME
 Dim ftLastWriteTime As FILETIME
 Dim nFileSizeHigh As Integer
 Dim nFileSizeLow As Integer
 Dim dwReserved0 As Integer
 Dim dwReserved1 As Integer
 <VBFixedString(260)> Public cFileName As String
 <VBFixedString(14)> Public cAlternate As String
End Structure

I've also tried

Public Structure WIN32_FIND_DATA
 Dim dwFileAttributes As Integer
 Dim ftCreationTime As FILETIME
 Dim ftLastAccessTime As FILETIME
 Dim ftLastWriteTime As FILETIME
 Dim nFileSizeHigh As Integer
 Dim nFileSizeLow As Integer
 Dim dwReserved0 As Integer
 Dim dwReserved1 As Integer
 <VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByVa
lTStr, SizeConst:=260)> Public cFileName As String
 <VBFixedString(14),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByVa
lTStr, SizeConst:=14)> Public cAlternate As String
End Structure

Which one should I use with WinInet or is there a better way
to declare fixed length strings in structures?

Thanks,
Jim Edgar

"Tom Shelton" <tom@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:uCBOohj4EHA.3756@TK2MSFTNGP14.phx.gbl...
> In article <uQaUg2i4EHA.1404@TK2MSFTNGP11.phx.gbl>, Jim Edgar wrote:
> > Is is possible to use WinInet in vb.net 2003 to download files from an FTP server?
I've
> > been asked to rewrite some VB6 code as a service that downloads files then parses and
> > reformats them. The parsing and formatting was easy but the FTP stuff has me stuck.
> > FtpFindFirstFile() doesn't return the name of the file in the WIN32_FIND_DATA
structure so
> > I can't get a list of the files on the server. Has anyone used WinInet before for
FTP?
> > Is this doable in .net or do I need to use System.Net and System.Net.Sockets?
> >
> > Thanks,
> >
> > Jim Edgar
>
> Yes it's possible... I haven't really done it from VB.NET - but I have
> a whole class written in C# that does it. The code should convert to
> VB.NET fairly easily, and can be used from a separate library as is...
>
> Anyway, here is my declarations from that libary, with a little main
> function that does what you're asking - it seems to work here any way (I
> tested this against an SCO Unix server and a FreeBSD server)...
>
> using System;
> using System.Text;
> using System.Runtime.InteropServices;
>
> namespace Testing
> {
> /// <summary>
> /// Summary description for WinInet.
> /// </summary>
> public sealed class WinInet
> {
> public const int INTERNET_OPEN_TYPE_PRECONFIG = 0;
> public const int INTERNET_OPEN_TYPE_DIRECT = 1;
> public const int INTERNET_OPEN_TYPE_PROXY = 3;
> public const short INTERNET_DEFAULT_FTP_PORT = 21;
> public const int INTERNET_SERVICE_FTP = 1;
> public const int FTP_TRANSFER_TYPE_ASCII = 0x01;
> public const int FTP_TRANSFER_TYPE_BINARY = 0x02;
> public const int GENERIC_WRITE = 0x40000000;
> public const int GENERIC_READ = unchecked((int)0x80000000);
> public const int MAX_PATH = 260;
> public const int INTERNET_FLAG_NEED_FILE = 0x00000010;
>
> [StructLayout (LayoutKind.Sequential)]
> public struct FILETIME
> {
> public int dwLowDateTime;
> public int dwHighDateTime;
> }
>
> [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Auto)]
> public struct WIN32_FIND_DATA
> {
> public int dwFileAttributes;
> public FILETIME ftCreationTime;
> public FILETIME ftLastAccessTime;
> public FILETIME ftLastWriteTime;
> public int nFileSizeHigh;
> public int nFileSizeLow;
> public int dwReserved0;
> public int dwReserved1;
>
> [MarshalAs (UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
> public string cFileName;
>
> [MarshalAs (UnmanagedType.ByValTStr, SizeConst=14)]
> public string cAlternateFileName;
> }
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern IntPtr InternetOpen(
> string lpszAgent,
> int dwAcessType,
> string lpszProxyName,
> string lpszProxyBypass,
> int dwFlags);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern IntPtr InternetConnect(
> IntPtr hInternet,
> string lpszServerName,
> short nServerPort,
> string lpszUserName,
> string lpszPassword,
> int dwService,
> int dwFlags,
> ref int dwContext);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern bool FtpGetCurrentDirectory(
> IntPtr hConnect,
> StringBuilder lpszCurrentDirectory,
> ref int lpdwCurrentDirectory);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern bool FtpSetCurrentDirectory(
> IntPtr hConnect,
> string lpszCurrentDirectory);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern IntPtr FtpOpenFile(
> IntPtr hConnect,
> string lpszFileName,
> int dwAccess,
> int dwFlags,
> out int dwContext);
>
> [DllImport("wininet.dll", SetLastError=true)]
> public static extern bool InternetWriteFile(
> IntPtr hFile,
> [MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
> int dwNumberOfBytesToWrite,
> out int lpdwNumberOfBytesWritten);
>
> [DllImport("wininet.dll", SetLastError=true)]
> public static extern bool InternetReadFile(
> IntPtr hFile,
> [MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
> int dwNumberOfBytesToRead,
> out int lpdwNumberOfBytesRead
> );
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern bool InternetCloseHandle(IntPtr hInternet);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern bool FtpPutFile (
> IntPtr hConnect,
> string lpszLocalFile,
> string lpszNewRemoteFile,
> int dwFlags,
> out int dwContext);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern bool FtpGetFile (
> IntPtr hConnect,
> string lpszRemoteFile,
> string lpszLocalFile,
> bool failIfExists,
> int dwFlagsAttributes,
> int dwFlags,
> out int dwContext);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern bool FtpCreateDirectory(
> IntPtr hConnect,
> string lpszDirectory);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern IntPtr FtpFindFirstFile(
> IntPtr hConnect,
> string lpszSearchFile,
> out WIN32_FIND_DATA lpFindFileData,
> int dwFlags,
> out int dwContext);
>
> [DllImport("wininet.dll", CharSet=CharSet.Auto,
> SetLastError=true)]
> public static extern bool InternetFindNextFile(
> IntPtr hFind,
> out WIN32_FIND_DATA lpvFindData
> );
>
> [STAThread ()]
> public static void Main ()
> {
> int context = 0;
> IntPtr hInternet = InternetOpen (null,
> INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
> IntPtr hConnection = InternetConnect (
> hInternet,
> "zorkmid.dakcs.com",
> INTERNET_DEFAULT_FTP_PORT,
> "tom",
> "vws4ever",
> INTERNET_SERVICE_FTP,
> 0,
> ref context);
>
> WIN32_FIND_DATA findData;
> IntPtr hFind = FtpFindFirstFile (
> hConnection,
> "*",
> out findData,
> INTERNET_FLAG_NEED_FILE,
> out context
> );
> Console.WriteLine (findData.cFileName);
> while (InternetFindNextFile (hFind, out findData))
> {
> Console.WriteLine (findData.cFileName);
> }
> InternetCloseHandle (hFind);
> InternetCloseHandle (hConnection);
> InternetCloseHandle (hInternet);
>
>
> }
> }
> }
>
> HTH
> --
> Tom Shelton [MVP]
> OS Name: Microsoft Windows XP Professional
> OS Version: 5.1.2600 Service Pack 2 Build 2600
> System Up Time: 0 Days, 4 Hours, 36 Minutes, 58 Seconds



Relevant Pages

  • VirtualAllocEx returns a bad pointer in some processes
    ... (ByVal hWnd As IntPtr, _ ... (ByVal hWndParent As IntPtr, _ ... ByVal lParam As String) As Integer ... Dim aihWnds As ArrayList ...
    (microsoft.public.vc.mfc)
  • Virtual memory C++ problem
    ... (ByVal hWnd As IntPtr, _ ... (ByVal hWndParent As IntPtr, _ ... ByVal lParam As String) As Integer ... Dim aihWnds As ArrayList ...
    (microsoft.public.win32.programmer.kernel)
  • SetPrinter for network printers
    ... Public dmDeviceName As String ... Public pSecurityDescriptor As IntPtr ... Dim pPrinterInfo As IntPtr ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Detecting a service that is running
    ... "OpenSCManagerA" (ByVal lpMachineName As String, ... "QueryServiceStatus" (ByVal hService As Long, ... public int dwControlsAccepted = 0; ... private static extern IntPtr OpenSCManager(string lpMachineName, ...
    (microsoft.public.vb.winapi)
  • Re: MapiSendMail and web browser
    ... Public Shared Function SendMail(ByVal strAttachmentFileName As String, ... Dim winhandle As IntPtr = New IntPtr ... Public Class MapiMessage ...
    (microsoft.public.win32.programmer.messaging)