Several useful functions in VB

Source: Internet
Author: User

'-------- (1 )------------
'Obtain all the key values truezq under a section in the specified INI file. The following API declaration is required.
'Private declare function getprivateprofilesection lib "Kernel32" alias "getprivateprofilesectiona" (byval lpappname as string, byval lpreturnedstring as string, byval nsize as long, byval lpfilename as string) as long
'Returns a String Array
'Call example:
'Dim arrclass () as string
'Arrclass = getinfosection ("class", "d:/type. ini ")
 
Public Function getinfosection (strsection as string, strinifile as string) as string ()
Dim strreturn as string * 32767
Dim strtmp as string
Dim nstart as integer, nend as integer, I as integer
Dim sarray () as string

Call getprivateprofilesection (strsection, strreturn, Len (strreturn), strinifile)

Strtmp = strreturn
I = 1
Do While strtmp <> ""
Nstart = nend + 1
Nend = instr (nstart, strreturn, vbnullchar)
Strtmp = mid $ (strreturn, nstart, nend-nstart)
If Len (strtmp)> 0 then
Redim preserve sarray (1 to I)
Sarray (I) = strtmp
I = I + 1
End if

Loop
Getinfosection = sarray
End Function

'-------- (2 )------------
'Purpose: Remove spaces at the beginning and end of the string and all invalid characters.
'Test case
'Dim strres as string
'Dim strsour as string
'
'Strsour = "" & vbnullchar & "AB cd" & vbnullchar
'Strres = zqtrim (strsour)
'Msgbox "length =" & Len (strsour) & "value = 111" & strres & "222"
Public Function zqtrim (byval strsour as string) as string
Dim strtmp as string
Dim nlen as integer
Dim I as integer, J as integer
Dim strnow as string, strvalid () as string, strnew as string
'Strnow Current Character
'Strvalid valid characters
'Strnew' the last new character

Strtmp = trim $ (strsour)
Nlen = Len (strtmp)
If nlen <1 then
Zqtrim = ""
Exit Function
End if
J = 0
For I = 1 to nlen
Strnow = mid (strtmp, I, 1) 'read one character each time
'Msgbox ASC (strnow)
If strnow <> vbnullchar and ASC (strnow) <> 9 then' if valid, it is saved to a valid array.
Redim preserve strvalid (j)
Strvalid (j) = strnow
J = J + 1
End if

Next I

Strnew = join (strvalid, "") 'connects all valid characters
Zqtrim = trim $ (strnew) 'removes the spaces at the beginning and end of the string.
End Function

'-------- (3 )------------
'Check whether the file exists. If yes, true is returned. Otherwise, false is returned.
Public Function checkfileexist (strfile as string) as Boolean
If Dir (strfile, vbdirectory) <> "then
Checkfileexist = true
Else
Checkfileexist = false
End if
End Function

'-------- (4 )------------
'Obtain the key value of a subkey under a section in the specified INI file. The following API declaration is required.
'Public declare function getprivateprofilestring lib "Kernel32" alias _
'"Getprivateprofilestringa" (byval lpapplicationname as string ,_
'Byval lpkeyname as any, byval lpdefault as string, byval lpreturnedstring _
'As string, byval nsize as long, byval lpfilename as string) as long
'Returns a string
'Call example:
'Dim strrun as string
'Strrun = getinivalue ("Windows", "run", "C:/Windows/win. ini ")

Public Function getinivalue (byval lpkeyname as string, byval strname as string, byval strinifile as string) as string
Dim strtmp as string * 255

Call getprivateprofilestring (lpkeyname, strname ,"",_
Strtmp, Len (strtmp), strinifile)
Getinivalue = left $ (strtmp, instr (strtmp, vbnullchar)-1)

End Function

'-------- (5 )------------
'The following API declaration is required to obtain the Windows directory
'Private declare function getwindowsdirectory lib "Kernel32" alias "getwindowsdirectorya" (byval lpbuffer as string, byval nsize as long) as long
'Returns a string, such as "C:/Windows" and "c:/winnt"
'Call example:
'Dim strwindir as string
'Strwindir = getwindir ()
Private function getwindir ()
Dim WINDIR as string * 100
Call getwindowsdirectory (WINDIR, 100)
Getwindir = left $ (WINDIR, instr (WINDIR, vbnullchar)-1)

End Function

'-------- (6 )------------
'Obtain the Windows System directory. The following API declaration is required.
'Private declare function getsystemdirectory lib "Kernel32" alias "getsystemdirectorya" (byval lpbuffer as string, byval nsize as long) as long
'Returns a string, such as "C:/Windows/system" and "c:/winnt/system32"
'Call example:
'Dim strsysdir as string
'Strsysdir = getsystemdir ()
Private function getsystemdir ()
Dimstrsysdir as string * 100
Call getsystemdirectory (strsysdir, 100)
Getsystemdir = left $ (strsysdir, instr (strsysdir, vbnullchar)-1)
End Function

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.