nvo_ftp.sru 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. $PBExportHeader$nvo_ftp.sru
  2. forward
  3. global type nvo_ftp from nonvisualobject
  4. end type
  5. end forward
  6. type filetime from structure
  7. unsignedlong dwlowdatetime
  8. unsignedlong dwhighdatetime
  9. end type
  10. type win32_find_data from structure
  11. unsignedlong dwfileattributes
  12. filetime ftcreationtime
  13. filetime ftlastaccesstime
  14. filetime ftlastwritetime
  15. unsignedlong nfilesizehigh
  16. unsignedlong nfilesizelow
  17. unsignedlong dwreserved0
  18. unsignedlong dwreserved1
  19. character cfilename[260]
  20. character calternate[14]
  21. end type
  22. type systemtime from structure
  23. integer wYear
  24. integer wMonth
  25. integer wDayOfWeek
  26. integer wDay
  27. integer wHour
  28. integer wMinute
  29. integer wSecond
  30. integer wMilliseconds
  31. end type
  32. global type nvo_ftp from nonvisualobject
  33. end type
  34. global nvo_ftp nvo_ftp
  35. type prototypes
  36. FUNCTION ulong InternetGetConnectedState( REF ulong lpdwFlags, ulong dwReserved ) LIBRARY "wininet.dll"
  37. Function Long InternetOpen(String sAgent, Long lAccessType, String sProxyName,String sProxyBypass, Long lFlags) Library "wininet.dll" Alias For "InternetOpenA;Ansi"
  38. Function Long InternetConnect(Long hInternetSession, String sServerName, Integer nServerPort, String sUsername,String sPassword, Long lService, Long lFlags, Long lContext) Library "wininet.dll" Alias For "InternetConnectA;Ansi"
  39. Function Integer InternetCloseHandle(Long hInet) Library "wininet.dll"
  40. Function Boolean FtpGetCurrentDirectory(Long hFtpSession, ref String lpszDirectory, ref Long lpdwCurrentDirectory) Library "wininet.dll" Alias For "FtpGetCurrentDirectoryA;Ansi"
  41. Function Boolean FtpSetCurrentDirectory(Long hFtpSession, ref String lpszDirectory) Library "wininet.dll" Alias For "FtpSetCurrentDirectoryA;Ansi"
  42. Function Long FtpFindFirstFile(Long hFtpSession, String lpszSearchFile,ref win32_find_data lpFindFileData, Long dwFlags,Long dwContent) Library "wininet.dll" Alias For "FtpFindFirstFileA;Ansi"
  43. Function Long InternetFindNextFile(Long hFind, ref win32_find_data lpvFindData) Library "wininet.dll" Alias For "InternetFindNextFileA;Ansi"
  44. Function Boolean FtpGetFile(Long hFtpSession, String lpszRemoteFile, String lpszNewFile, Boolean fFailIfExists, Long dwFlagsAndAttributes, Long dwFlags, Long dwContext) Library "wininet.dll" Alias For "FtpGetFileA;Ansi"
  45. Function Boolean FtpPutFile(Long hFtpSession, String lpszLocalFile, String lpszRemoteFile, Long dwFlags, Long dwContext) Library "wininet.dll" Alias For "FtpPutFileA;Ansi"
  46. Function Boolean FtpDeleteFile(Long hFtpSession, String lpszFileName) Library "wininet.dll" Alias For "FtpDeleteFileA;Ansi"
  47. Function Long FtpOpenFile(Long hFtpSession, String lpszRemoteFile, Long dwFlagsAndAttributes, Long dwFlags, Long dwContext) Library "wininet.dll" Alias For "FtpOpenFileA;Ansi"
  48. FUNCTION ulong FileTimeToSystemTime(ref FILETIME lpFileTime,ref SYSTEMTIME lpSystemTime) LIBRARY "kernel32.dll" alias for "FileTimeToSystemTime;Ansi"
  49. FUNCTION ulong GetCurrentDirectory(ulong nBufferLength,ref string lpBuffer) LIBRARY "kernel32.dll" Alias For "GetCurrentDirectoryA;Ansi"
  50. FUNCTION ulong SetCurrentDirectory(ref string lpPathName) LIBRARY "kernel32.dll" ALIAS FOR "SetCurrentDirectoryA;Ansi"
  51. FUNCTION ulong FindFirstFile(ref string lpFileName,ref WIN32_FIND_DATA lpFindFileData) LIBRARY "kernel32.dll" ALIAS FOR "FindFirstFileA;Ansi"
  52. FUNCTION ulong FindNextFile(ulong hFindFile,ref WIN32_FIND_DATA lpFindFileData) LIBRARY "kernel32.dll" ALIAS FOR "FindNextFileA;Ansi"
  53. FUNCTION ulong GetDriveType(ref string nDrive) LIBRARY "kernel32.dll" ALIAS FOR "GetDriveTypeA;Ansi"
  54. FUNCTION ulong DeleteFile(ref string lpFileName) LIBRARY "kernel32.dll" ALIAS FOR "DeleteFileA;Ansi"
  55. Function ulong ShellExecute(ulong hwnd,ref string lpOperation,ref string lpFile,ref string lpParameters,ref string lpDirectory,ulong nShowCmd) LIBRARY "shell32.dll" ALIAS FOR "ShellExecuteA;Ansi"
  56. end prototypes
  57. type variables
  58. Private:
  59. Long il_dwflags = 1 //传输方式1为ASSIC方式,2为二进制方式
  60. Boolean ib_overfile = False //是否覆盖己存在的文件
  61. Long il_handle = 0 //是否连接上网络 0 不成功
  62. Long il_netconn = 0 //连接上FTP的句柄 0 不成功
  63. Long il_findfile = 0 //是否找到第一个文件
  64. Long il_localfindfile = 0 //是否找到本地的第一个文件
  65. String is_servername
  66. String is_username
  67. String is_password
  68. Integer ii_port
  69. end variables
  70. forward prototypes
  71. public function long of_internetopen ()
  72. public function long uf_ftpconnect (string as_servername, string as_username, string as_password, integer ai_port)
  73. public function boolean uf_ftpgetfile (string as_remotefile, string as_newfile)
  74. public function boolean of_ftpconnect ()
  75. public function boolean uf_ftpputfile (string as_localfile, string as_remotefile)
  76. public function boolean uf_ftpdeletefile (string as_remotefile)
  77. public function string uf_ftpfindfirstfile (string as_filemask)
  78. public function boolean uf_ftpsetcurrentdir (string as_currentdir)
  79. public function boolean of_internetclose ()
  80. public function boolean uf_internetclose ()
  81. public function string uf_ftpfindnextfile ()
  82. public function string uf_ftpgetcurrentdir ()
  83. public function string uf_cutstring (string as_string, string as_posstring, integer ai_pos)
  84. public function string of_markfileinfo (win32_find_data astr_data)
  85. public function datetime of_filetime2systemtime (filetime astr_filetime)
  86. public function string uf_getcurrentdir ()
  87. public function string uf_findfirstfile (string as_filemask)
  88. public function string uf_findnextfile ()
  89. public function string uf_remotefilelist (string as_filemask)
  90. public function long of_internetopen ()
  91. public function long uf_ftpconnect (string as_servername, string as_username, string as_password, integer ai_port)
  92. public function boolean uf_ftpgetfile (string as_remotefile, string as_newfile)
  93. public function boolean of_ftpconnect ()
  94. public function boolean uf_ftpputfile (string as_localfile, string as_remotefile)
  95. public function boolean uf_ftpdeletefile (string as_remotefile)
  96. public function string uf_ftpfindfirstfile (string as_filemask)
  97. public function boolean uf_ftpsetcurrentdir (string as_currentdir)
  98. public function boolean of_internetclose ()
  99. public function boolean uf_internetclose ()
  100. public function string uf_ftpfindnextfile ()
  101. public function string uf_ftpgetcurrentdir ()
  102. public function string uf_cutstring (string as_string, string as_posstring, integer ai_pos)
  103. public function string of_markfileinfo (win32_find_data astr_data)
  104. public function datetime of_filetime2systemtime (filetime astr_filetime)
  105. public function string uf_getcurrentdir ()
  106. public function string uf_findfirstfile (string as_filemask)
  107. public function string uf_findnextfile ()
  108. public function string uf_remotefilelist (string as_filemask)
  109. public function string uf_localfilelist (string as_filemask)
  110. public function boolean uf_setcurrentdir (string as_currentdir)
  111. public subroutine uf_resetremotefind ()
  112. public subroutine uf_resetlocalfind ()
  113. public function integer uf_getdrivetype (string as_diskpath)
  114. public function string uf_readini (string as_filename, string as_section, string as_key, string as_default)
  115. public subroutine uf_overfile (boolean ab_flag)
  116. public subroutine uf_sendmode (string as_mode)
  117. public function boolean uf_actionfile (unsignedlong aul_hwnd, string as_filename)
  118. public function boolean uf_deletefile (string as_filename)
  119. public function integer uf_internetconnect ()
  120. end prototypes
  121. public function long of_internetopen ();String ls_proxyname,ls_proxypass
  122. Long ll_handle
  123. SetNull(ls_proxyname)
  124. SetNull(ls_proxypass)
  125. ll_handle = InternetOpen("FTP Control",1,ls_proxyname,ls_proxypass,0)
  126. If ll_handle = 0 Then
  127. MessageBox('提示','不能打开Ftp控制。')
  128. Return 0
  129. End If
  130. Return ll_handle
  131. end function
  132. public function long uf_ftpconnect (string as_servername, string as_username, string as_password, integer ai_port);il_handle = of_internetopen()
  133. If il_handle = 0 Then Return 0
  134. il_netconn = InternetConnect(il_handle,as_servername,ai_port,as_username,as_password,1,0,0)
  135. If il_netconn = 0 Then
  136. of_internetclose()
  137. MessageBox('提示','不能连接主机 '+as_servername+' 。')
  138. Return 0
  139. End If
  140. is_servername = as_servername
  141. is_username = as_username
  142. is_password = as_password
  143. ii_port = ai_port
  144. Return il_netconn
  145. end function
  146. public function boolean uf_ftpgetfile (string as_remotefile, string as_newfile);If Not of_ftpconnect() Then Return False
  147. Boolean lb_ok,lb_notover
  148. lb_notover = Not ib_overfile
  149. lb_ok = FtpGetFile(il_netconn,as_remotefile,as_newfile,lb_notover,0,il_dwflags,0)
  150. IF lb_ok = False Then
  151. // MessageBox('提示','下载文件'+as_remotefile+'时出错。')
  152. Return False
  153. End If
  154. Return True
  155. end function
  156. public function boolean of_ftpconnect ();If il_netconn = 0 Then
  157. MessageBox('提示','请先连接FTP服务器。')
  158. Return False
  159. End If
  160. Return True
  161. end function
  162. public function boolean uf_ftpputfile (string as_localfile, string as_remotefile);If Not of_ftpconnect() Then Return False
  163. Boolean lb_ok
  164. lb_ok = FtpPutFile(il_netconn,as_localfile,as_remotefile,il_dwflags,0)
  165. IF lb_ok = False Then
  166. // MessageBox('提示','上传文件'+as_localfile+'时出错。')
  167. Return False
  168. End If
  169. Return True
  170. end function
  171. public function boolean uf_ftpdeletefile (string as_remotefile);If Not of_ftpconnect() Then Return False
  172. Boolean lb_ok
  173. lb_ok = FtpDeleteFile(il_netconn,as_remotefile)
  174. IF lb_ok = False Then
  175. // MessageBox('提示','删除远程文件'+as_remotefile+'时出错。')
  176. Return False
  177. End If
  178. Return True
  179. end function
  180. public function string uf_ftpfindfirstfile (string as_filemask);//====================================================================
  181. // Function - uf_ftpfindfirstfile for nvo_ftp
  182. //--------------------------------------------------------------------
  183. // 描述: 寻找符合的第一个文件名
  184. //--------------------------------------------------------------------
  185. // 参数:
  186. //
  187. // string as_filemask
  188. // <描述> 需寻找的文件名,可用通配符*或?
  189. //--------------------------------------------------------------------
  190. // 返回值: (STRING) 文件名
  191. //--------------------------------------------------------------------
  192. // 编制: BAOYF 日期: 2002.10.09 15:09
  193. //====================================================================
  194. If Not of_ftpconnect() Then Return '?'
  195. If il_findfile <> 0 Then Return '?'
  196. String ls_filename = "?"
  197. win32_find_data lstr_data
  198. lstr_data.cfilename = Space(260)
  199. lstr_data.calternate = Space(14)
  200. il_findfile = FtpFindFirstFile(il_netconn,as_filemask,lstr_data,0,0)
  201. If il_findfile <> 0 Then
  202. ls_filename = of_markfileinfo(lstr_data)
  203. End If
  204. Return ls_filename
  205. end function
  206. public function boolean uf_ftpsetcurrentdir (string as_currentdir);If Not of_ftpconnect() Then Return False
  207. Boolean lb_ok
  208. lb_ok = FtpSetCurrentDirectory(il_netconn,as_currentdir)
  209. If lb_ok Then Return True
  210. //MessageBox("提示","设置远程目录错误。")
  211. Return False
  212. end function
  213. public function boolean of_internetclose ();//====================================================================
  214. // Function - of_internetclose for nvo_ftp
  215. //--------------------------------------------------------------------
  216. // 描述: 关闭ftp连接与网络连接
  217. //--------------------------------------------------------------------
  218. // 参数: 无
  219. //
  220. // <数据类型> <名称>
  221. // <描述>
  222. // <数据类型> <名称>
  223. // <描述>
  224. //--------------------------------------------------------------------
  225. // 返回值: (BOOLEAN) True成功 False不成功
  226. //--------------------------------------------------------------------
  227. // 编制: BAOYF 日期: 2002.10.09 15:06
  228. //====================================================================
  229. Long ll_ok
  230. ll_ok = InternetCloseHandle(il_netconn)
  231. If ll_ok = 0 Then
  232. Return False
  233. End If
  234. il_netconn = 0
  235. il_findfile = 0
  236. il_localfindfile = 0
  237. ll_ok = InternetCloseHandle(il_handle)
  238. If ll_ok = 0 Then
  239. Return False
  240. End If
  241. il_handle = 0
  242. Return True
  243. end function
  244. public function boolean uf_internetclose ();Return of_internetclose()
  245. end function
  246. public function string uf_ftpfindnextfile ();If il_findfile = 0 Then
  247. MessageBox("提示","不能确定文件的位置。")
  248. Return "?"
  249. End If
  250. Long ll_ok
  251. String ls_filename = "?"
  252. win32_find_data lstr_data
  253. lstr_data.cfilename = Space(260)
  254. lstr_data.calternate = Space(14)
  255. ll_ok = InternetFindNextFile(il_findfile,lstr_data)
  256. If ll_ok <> 0 Then
  257. ls_filename = of_markfileinfo(lstr_data)
  258. End If
  259. Return ls_filename
  260. end function
  261. public function string uf_ftpgetcurrentdir ();If Not of_ftpconnect() Then Return "?"
  262. String ls_dir
  263. Long ll_len
  264. Boolean lb_ok
  265. ls_dir = Space(250)
  266. ll_len = 250
  267. lb_ok = FtpGetCurrentDirectory(il_netconn,ls_dir,ll_len)
  268. If lb_ok Then
  269. Return Left(ls_dir,ll_len)
  270. End If
  271. MessageBox("提示","取远程目录错误。")
  272. Return "?"
  273. end function
  274. public function string uf_cutstring (string as_string, string as_posstring, integer ai_pos);//剪取一串字符串中按as_posstring分开的第ai_pos段字符串
  275. /*例如:
  276. ls_str = "abc||bst||9usdf8||klsfj12||9886"
  277. ls_pos = "||"
  278. li_pos = 3
  279. 则uf_cutstring(ls_str,ls_pos,li_pos) = "9usdf8"
  280. */
  281. integer li_pos,li_poslen
  282. integer li_i,li_sumi
  283. string ls_return
  284. ls_return = ""
  285. li_poslen = len(as_posstring)
  286. li_sumi = ai_pos - 1
  287. for li_i = 1 to li_sumi
  288. li_pos = pos(as_string,as_posstring)
  289. if li_pos = 0 then goto l_return
  290. as_string = mid(as_string,li_pos + li_poslen)
  291. next
  292. li_pos = pos(as_string,as_posstring)
  293. if li_pos = 0 then
  294. ls_return = as_string
  295. else
  296. ls_return = mid(as_string,1,li_pos - 1)
  297. end if
  298. return ls_return
  299. l_return:
  300. return ls_return
  301. end function
  302. public function string of_markfileinfo (win32_find_data astr_data);String ls_fileinfo
  303. DateTime ldt_filetime
  304. ldt_filetime = of_filetime2systemtime(astr_data.ftlastwritetime)
  305. ls_fileinfo = Trim(astr_data.cfilename)
  306. Choose Case astr_data.dwfileattributes
  307. Case 16,17,18,20,21,22,23,48,49,50,52,53,2066
  308. ls_fileinfo = ls_fileinfo + '?' + '-1' //目录
  309. Case Else
  310. ls_fileinfo = ls_fileinfo + '?' + String(astr_data.nfilesizelow)
  311. End Choose
  312. ls_fileinfo = ls_fileinfo + '?' + String(ldt_filetime,'yyyy.mm.dd hh:mm:ss')
  313. Return ls_fileinfo
  314. end function
  315. public function datetime of_filetime2systemtime (filetime astr_filetime);String ls_date,ls_time
  316. DateTime ldt_datetime
  317. systemtime lstr_systemtime
  318. FileTimeToSystemTime(astr_filetime,lstr_systemtime)
  319. ls_date = String(lstr_systemtime.wyear)+'-'+String(lstr_systemtime.wmonth)+'-'+String(lstr_systemtime.wday)
  320. ls_time = String(lstr_systemtime.whour)+':'+String(lstr_systemtime.wminute)+':'+String(lstr_systemtime.wsecond)
  321. ldt_datetime = Datetime(Date(ls_date),Time(ls_time))
  322. Return ldt_datetime
  323. end function
  324. public function string uf_getcurrentdir ();String ls_dirstring
  325. uLong ll_len,ll_ok
  326. ls_dirstring = Space(256)
  327. ll_len = 256
  328. ll_ok = GetCurrentDirectory(ll_len,ls_dirstring)
  329. If ll_ok <> 0 Then
  330. ls_dirstring = Trim(ls_dirstring)
  331. Return ls_dirstring
  332. End If
  333. Return "?"
  334. end function
  335. public function string uf_findfirstfile (string as_filemask);String ls_filename = "?"
  336. win32_find_data lstr_data
  337. lstr_data.cfilename = Space(260)
  338. lstr_data.calternate = Space(14)
  339. il_localfindfile = FindFirstFile(as_filemask,lstr_data)
  340. If il_localfindfile <> 0 Then
  341. ls_filename = of_markfileinfo(lstr_data)
  342. End If
  343. Return ls_filename
  344. end function
  345. public function string uf_findnextfile ();If il_localfindfile = 0 Then
  346. MessageBox("提示","不能确定本地文件的位置。")
  347. Return "?"
  348. End If
  349. Long ll_ok
  350. String ls_filename = "?"
  351. win32_find_data lstr_data
  352. lstr_data.cfilename = Space(260)
  353. lstr_data.calternate = Space(14)
  354. ll_ok = FindNextFile(il_localfindfile,lstr_data)
  355. If ll_ok <> 0 Then
  356. ls_filename = of_markfileinfo(lstr_data)
  357. End If
  358. Return ls_filename
  359. end function
  360. public function string uf_remotefilelist (string as_filemask);If il_findfile = 0 Then
  361. Return uf_ftpfindfirstfile(as_filemask)
  362. End If
  363. Return uf_ftpfindnextfile()
  364. end function
  365. public function string uf_localfilelist (string as_filemask);If il_localfindfile = 0 Then
  366. Return uf_findfirstfile(as_filemask)
  367. End If
  368. Return uf_findnextfile()
  369. end function
  370. public function boolean uf_setcurrentdir (string as_currentdir);Long ll_ok
  371. ll_ok = SetCurrentDirectory(as_currentdir)
  372. If ll_ok = 0 Then Return False
  373. Return True
  374. end function
  375. public subroutine uf_resetremotefind ();il_findfile = 0
  376. end subroutine
  377. public subroutine uf_resetlocalfind ();il_localfindfile = 0
  378. end subroutine
  379. public function integer uf_getdrivetype (string as_diskpath);//Return :
  380. //0 指定设备不可用
  381. //1 指定目录不存在
  382. //2 可移动设备DRIVE_REMOVABLE
  383. //3 DRIVE_FIXED
  384. //4 DRIVE_REMOTE
  385. //5 DRIVE_CDROM
  386. //6 虚拟设备 DRIVE_RAMDISK
  387. //其它不存在
  388. Return GetDriveType(as_diskpath)
  389. end function
  390. public function string uf_readini (string as_filename, string as_section, string as_key, string as_default);//读取ini文件中的信息,若不存在此值则新增一个值
  391. //参数:as_filename INI文件名
  392. // as_section 段
  393. // as_key 键
  394. // as_default 默认值
  395. //返回:读取的值
  396. Integer li_fileno
  397. String ls_string
  398. If FileExists(as_filename) = False Then
  399. li_fileno = FileOpen(as_filename,LineMode!,Write!,LockWrite!,Append!)
  400. If li_fileno <= 0 Then
  401. MessageBox('提示','文件不能被创建,请与管理员联系。')
  402. Return ''
  403. End If
  404. FileClose(li_fileno)
  405. End If
  406. ls_string = ProfileString (as_filename,as_section,as_key,'Bsoft_Value')
  407. If ls_string = 'Bsoft_Value' Then
  408. SetProfileString(as_filename,as_section,as_key,as_default)
  409. ls_string = as_default
  410. End If
  411. Return ls_string
  412. end function
  413. public subroutine uf_overfile (boolean ab_flag);ib_overfile = ab_flag
  414. end subroutine
  415. public subroutine uf_sendmode (string as_mode);String ls_mode
  416. ls_mode = Upper(as_mode)
  417. If ls_mode = 'BIN' Then
  418. il_dwflags = 2
  419. Else
  420. il_dwflags = 1
  421. End If
  422. end subroutine
  423. public function boolean uf_actionfile (unsignedlong aul_hwnd, string as_filename);String str_null,str_filename
  424. uLong ll_ok
  425. SetNull ( str_null )
  426. ll_ok = ShellExecute(aul_hwnd, str_null , as_filename , str_null, str_null, 1 )
  427. If ll_ok = 0 Then Return False
  428. Return True
  429. end function
  430. public function boolean uf_deletefile (string as_filename);//删除本地文件
  431. Long ll_ok
  432. ll_ok = DeleteFile(as_filename)
  433. If ll_ok = 0 Then Return False
  434. Return True
  435. end function
  436. public function integer uf_internetconnect ();uLong lul_connection
  437. lul_connection = 7
  438. Return InternetGetConnectedState( lul_connection, 0 )
  439. //= 0 //不通
  440. //= 1 //拨号连通
  441. //= 2 //网络连通
  442. //= 4 //proxy连通
  443. end function
  444. on nvo_ftp.create
  445. TriggerEvent( this, "constructor" )
  446. end on
  447. on nvo_ftp.destroy
  448. TriggerEvent( this, "destructor" )
  449. end on