檔案的讀取VB6 方法二

2021-09-02 21:40:12 字數 3517 閱讀 7623

參考部落格 lyserver的專欄

option explicit

private declare sub copymemory lib "kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)

private declare function createfile lib "kernel32" alias "createfilea" (byval lpfilename as string, byval dwdesiredaccess as long, byval dwsharemode as long, byval lpsecurityattributes as long, byval dwcreationdisposition as long, byval dwflagsandattributes as long, byval htemplatefile as long) as long

private declare function closehandle lib "kernel32" (byval hobject as long) as long

private declare function getfilesize lib "kernel32" (byval hfile as long, lpfilesizehigh as long) as long

private const generic_read = &h80000000

private const generic_write = &h40000000

private const open_existing = 3

private const file_share_read = &h1

private const file_share_write = &h2

private const file_attribute_normal = &h80

private const file_attribute_archive = &h20

private const file_attribute_readonly = &h1

private const file_attribute_hidden = &h2

private const file_attribute_system = &h4

private declare function unmapviewoffile lib "kernel32" (lpbaseaddress as any) as long

private const page_readwrite = &h4

private const file_map_read = &h4

private declare function varptrarray lib "msvbvm60.dll" alias "varptr" (ptr() as any) as long

private type safearraybound

celements as long

llbound as long

end type

private type safearray1d

cdims as integer

ffeatures as integer

cbelements as long

clocks as long

pvdata as long

rgsabound(0) as safearraybound

end type

'使用記憶體對映方式查詢大型檔案中包含的字串

'function findtextinfile(byval strfilename as string, byval strtext as string) as string

function getfilestring(byval strfilename as string) as string

dim hfile as long, hfilemap as long

dim nfilesize as long, lpszfiletext as long, pbfiletext() as byte

dim ppsa as long, psa as long

dim tagnewsa as safearray1d, tagoldsa as safearray1d

hfile = createfile(strfilename, _

generic_read or generic_write, _

file_share_read or file_share_write, _

0, _

open_existing, _

file_attribute_normal or file_attribute_archive or file_attribute_readonly or _

file_attribute_hidden or file_attribute_system, _

0) '開啟檔案

if hfile <> 0 then

nfilesize = getfilesize(hfile, byval 0&) '獲得檔案大小

lpszfiletext = mapviewoffile(hfilemap, file_map_read, 0, 0, 0) '將對映物件對映到程序內部的位址空間

redim pbfiletext(0) '初始化陣列

ppsa = varptrarray(pbfiletext) '獲得指向safearray的指標的指標

copymemory psa, byval ppsa, 4 '獲得指向safearray的指標

copymemory tagoldsa, byval psa, len(tagoldsa) '儲存原來的safearray成員資訊

copymemory tagnewsa, tagoldsa, len(tagnewsa) '複製safearray成員資訊

tagnewsa.rgsabound(0).celements = nfilesize '修改陣列元素個數

tagnewsa.pvdata = lpszfiletext '修改陣列資料位址

copymemory byval psa, tagnewsa, len(tagnewsa) '將對映後的資料位址繫結至陣列

' findtextinfile = instr(pbfiletext, strconv(strtext, vbfromunicode)) '查詢子字串位置

getfilestring = strconv(pbfiletext, vbunicode)

copymemory byval psa, tagoldsa, len(tagoldsa) '恢復陣列的safearray結構成員資訊

erase pbfiletext '刪除陣列

unmapviewoffile lpszfiletext '取消位址對映

closehandle hfilemap '關閉檔案對映物件的控制代碼

end if

closehandle hfile '關閉檔案

end function

Windows7下VB6的安裝方法

之前寫過一篇日誌,關於vb6程式在windows7下執行的問題 那麼,如果要在windows7下進行vb6開發,那該怎麼辦呢?首先,windows7已經不象vista那樣,可以通過acmsetup的辦法安裝。裝雙系統吧,用起來麻煩。其實,windows 7已經提供了乙個叫windows xp mod...

VB6開發的程式ocx控制項的手動註冊法

vb6開發的窗體程式中,如果使用了vb6基本控制項以外的activex控制項,例如使用了通用對話方塊或者treeview之類的第三方控制項,生成的產品發到另外一台電腦上,通常打不開。例如我開發了乙個inspect.exe的窗體應用程式,發到另一台電腦雙擊它啟動不了,看到如下的提示請不要大驚小怪。對話...

vb6中字串轉控制項名的方法

vb6中字串轉控制項名的方法 比如我想用 label1 來呼叫label1控制項,我們可以用 me label1 來呼叫label1的所有屬性 me label1 caption me label1 backcolor vbred 所有屬性都可用,可檢視label1所有的屬性。對於控制項陣列的呼叫是...