如何获得FTP命令执行后的结果 。急!

eaglecoody 2003-02-18 03:53:21
PB中如何获得FTP命令执行后的结果或FTP服务器中的目录名
...全文
284 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ice2water 2003-02-23
  • 打赏
  • 举报
回复
我用wininet.dll做了一个ftp,还可以,总结了一下,供你参考:

使用Wininet.dll建立ftp应用

一、需定义的API函数:
FUNCTION uint FindWindowA (long classname, string windowname) LIBRARY "user32.dll"
function ulong CreateMutexA (ulong lpMutexAttributes, int bInitialOwner, ref string lpName) library "kernel32.dll"
function ulong GetLastError () library "kernel32.dll"

Function Long InternetOpenA(string sAgent ,long lAccessType,string sProxyName,string sProxyBypass,long lFlags) Library "wininet.dll"
Function uLong InternetConnectA (long hInternetSession , string sServerName,integer nServerPort , &
string sUsername , string sPassword , long lService, long lFlags ,long lContext ) Library "wininet.dll"

FUNCTION ulong SleepEx(ulong dwMilliseconds,ulong bAlertable) LIBRARY "kernel32.dll"

Function Boolean FtpGetFileA (long hFtpSession , string lpszRemoteFile , &
string lpszNewFile ,boolean fFailIfExists,long dwFlagsAndAttributes, long dwFlags ,long dwContext ) Library "wininet.dll"
Function boolean FtpSetCurrentDirectoryA (long hFtpSession,string lpszDirectory ) Library "wininet.dll"

FUNCTION ulong CopyFile(ref string lpExistingFileName,ref string lpNewFileName,ulong bFailIfExists) LIBRARY "kernel32.dll" ALIAS FOR "CopyFileA"
FUNCTION ulong MoveFile(ref string lpExistingFileName,ref string lpNewFileName) LIBRARY "kernel32.dll" ALIAS FOR "MoveFileA"

二、具体实施步骤
1、调用InternetOpen函数设置环境。
API函数:
Function Long InternetOpenA(string sAgent, long lAccessType, string sProxyName, string sProxyBypass, long lFlags) Library "wininet.dll"

参数sAgent 用来指定调用WinINet 函数的应用程序或实体。为了达到目的,可以设置FTP控制。

参数lAccessType 指定我们是直接与某一主机相连还是使用代理服务器相连。如果传递值1,就直接与主机连接。如果传递3,就通过代理服务器。如果传递0,连接时就要基于HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings下的注册表数值ProxyEnable、ProxyServer和 ProxyOverride 。

  我们可以使用参数sProxyName和 sProxyBypass,而不是注册设置来提供代理服务器和不使用代理服务器的IP地址和名字。列出代理的基本格式是protocol = protocol://proxy_name:access_port?。例如,要指定Proxy1 上的端口21为代理服务器,用Ftp=ftp://Proxy1:21?作为sProxyName。要饶过以ov? 开始的任何主机,sProxyBypass 字符串应为ov*? 。

  最后,lFlags 用来显示影响函数结果的不同选择。在我们的例子中,我们传递0。
integer in_lg_handle
/*如果函数调用失败,为0。不然,就保存在下一步中将要传递给InternetConnect */
in_lg_handle = InternetOpenA("PB FTP Updater", 1, '', '', 0)
IF in_lg_handle = 0 THEN
RETURN -1
END IF

2、调用InternetConnect 函数连接主机。
API函数:
Function uLong InternetConnectA &
(long hInternetSession , string sServerName,integer nServerPort , &
string sUsername , string sPassword , long lService, long lFlags ,&
long lContext ) Library "wininet.dll"

第一个参数hInternetSession 是InternetOpen 调用返回的句柄值。

  sServerName 是我们即将连接的FTP服务器的IP地址或主机名。

  nServerPort 指示与哪一个端口连接。如值为0,它指示的是默认的端口21。

  sUsername 和 sPassword 分别传递用户名和口令。

  lService 用来指示使用的服务类型,如HTTP, FTP等。如值为1,表示FTP服务。

  如果将x8000000传递到 lFlags 参数,连接将使用被动FTP语义。或者,传递0来使用非被动语义。

  最后,当使用回叫信号时,lContext 用来识别应用程序的前后关系。如不使用回叫信号,值为0。

long in_lg_Connection
/* 如果函数调用失败,为0。反之, 就保存在下一步中将传递给FtpGetFile 的句柄的值。 */
in_lg_Connection = InternetConnectA(in_lg_handle, "141.60.96.3", 0, "tzsw", "1234", 1, 0, 0)
IF in_lg_Connection = 0 THEN
RETURN -1
END IF

3、调用FtpGetFile或FtpPutFile 函数得到文件。
API函数:
Function boolean FtpSetCurrentDirectoryA (long hFtpSession, string lpszDirectory ) Library "wininet.dll"
第一个参数hFtpSession是InternetConnect调用返回的句柄值。
第二个参数lpszDirectory是FTP服务器上文件的目录。

Function Boolean FtpGetFileA (long hFtpSession , string lpszRemoteFile , &
string lpszNewFile ,boolean fFailIfExists,long dwFlagsAndAttributes, &
long dwFlags ,long dwContext ) Library "wininet.dll"
第一个参数hFtpSession是InternetConnect调用返回的句柄值。
  lpszRemoteFile和lpszNewFile分别是FTP服务器上的文件名和将在本地机上创建的文件名。
  fFailIfExists 标志是 0(替换本地文件)或 1 (如果本地文件已经存在则取消)。
  dwFlagsAndAttributes 用来指定本地文件的文件属性。如忽略,传递0。
  dwFlags 参数指定为1是用ASCII传输文件(A类传输方法),指定为2是用二进制传输文件(B类传输方法)。
  最后,当使用回叫信号时,lContext 用来识别应用程序前后关系。如不使用回叫信号,值为0。

Function Boolean FtpPutFileA (long hFtpSession , string lpszLocalFile, string lpszRemoteFile, long dwFlags, long dwContext ) Library "wininet.dll"
第一个参数hFtpSession是InternetConnect调用返回的句柄值。
  lpszLocalFile和lpszRemoteFile分别是本地机上的文件名和将在FTP服务器上创建的文件名。
  fFailIfExists 标志是 0(替换本地文件)或 1 (如果本地文件已经存在则取消)。
  dwFlags 参数指定为1是用ASCII传输文件(A类传输方法),指定为2是用二进制传输文件(B类传输方法)。
  最后,当使用回叫信号时,lContext 用来识别应用程序前后关系。如不使用回叫信号,值为0。

booblen bl_success
/*如果函数调用成功,为 True, 反之为False。*/
bl_success = FtpSetCurrentDirectoryA(in_lg_connection, st_directory)
IF NOT bl_success THEN RETURN -1

//文件备份
// FUNCTION ulong MoveFile(ref string lpExistingFileName,ref string lpNewFileName) LIBRARY "kernel32.dll" ALIAS FOR "MoveFileA"

IF FileExists('.\'+st_filename) THEN
st_localfile = '.\'+st_filename
st_backupfile = '.\'+st_filename + '.bak'
MoveFile(st_localfile,st_backupfile)
END IF

long INTERNET_FLAG_RELOAD = 134217728
bl_success = FtpGetFileA(in_lg_connection, st_filename, ".\" + st_filename, False, INTERNET_FLAG_RELOAD, 2, 0)
IF NOT bl_success THEN RETURN -1

4、用InternetCloseHandle 函数关闭第1、2步的句柄。
API函数:
Function Long InternetCloseHandle(long hInternetSession) Library "wininet.dll"
调用:
InternetCloseHandle(ln_lg_connection)

2001xgp 2003-02-23
  • 打赏
  • 举报
回复
通过api函数可以取得结果,用dos命令是没有办法取得结果的
eaglecoody 2003-02-19
  • 打赏
  • 举报
回复
有没有人知道呢?
如果不行,可以简单地说一下思路吗

662

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder Web 应用
社区管理员
  • Web 应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧