[C #] Use wininet.dll to realize ftp function-part: function list

xiaoxiao2021-03-06  56

For a few days, FTP is studied. When FTPClient is transferred to the FTP file transmission, if the FTP site does not support PASV mode, the internal network is also limited to the port mode, and the multi-test is not successful, converts the idea, and consider using the Win API. Successfully implemented the platform call to Wininet.dll, the list of functions used below is as follows:

// The following is two data structures used in the implementation of the FTP process.

[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Auto)] public class WIN32_FIND_DATA {public UInt32 dwFileAttributes = 0; public FILETIME ftCreationTme; public FILETIME ftLastAccessTime; public FILETIME ftLastWriteTime; public UInt32 nFileSizeHigh = 0; public UInt32 nFileSizeLow = 0; public UInt32 dwReserved0 = 0; public UInt32 dwReserved1 = 0; [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 256)] public string cFileName = null; [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 14)] public string cAlternateFileName = null;};

[StructLayout (layoutkind.sequential, charset = charset.auto)] public class filetime {public int dwhighttime = 0; public int dWhighttime = 0;

// The following is a hosted conversion to important functions in WinInet.dll

[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 Strapname);

[DLLIMPORT ("Wininet.dll", Charset = Charset.Auto)]

Public Static Extern Bool InternetFindNextFile (INTPTR HFIND, [IN, OUT] WIN32_FIND_DATA DIRDATA);

[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 ulnderved);

[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] WIN32_FIND_DATA DIRDATA, ULONG ULFLAGS, ULONG ULCONTEXT);

[DLLIMPORT ("Wininet.dll", Charset = Charset.Auto)]

Public Static Extern Bool FTPGETFILE (INTPTR ULSESSION, STRING STRING STRING STRING STRINGEXIST, ULONG ULFLAGS, ULONG ULINETFALS, ULONG ULCONTEXT);

[DLLIMPORT ("Wininet.dll", Charset = Charset.Auto)]

Public Static Extern Bool FtpputFile (INTPTR ULSESSION, STRING STRINGS, ULONG ULFLAGS, ULONG ULCONTEXT);

[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);

There are also many other functions in Wininet.dll, please see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/wininet_reference.asp

转载请注明原文地址:https://www.9cbs.com/read-114101.html

New Post(0)