选择显示字体大小

在vb中调用api操作注册表

有时候我们需要读取某个key下的所有名称的值,找到特定的或者全部名称的值以作它用,比如我在编写清除著名的"木马冰河" 服务器端程序时,就需要查找hkey_local_machine\software\microsoft\windows\currentversion\run下的所有可疑的加载程序项目然后删除之。

要用到的api函数的详细描述参看“注册表编程相关函数”一文。一个完整的例子如下:

'*************enumval2.bas***************

option explicit

public const

hkey_classes_root = &h80000000

public const hkey_current_user = &h80000001

public const hkey_local_machine = &h80000002

public const hkey_users = &h80000003

public const hkey_performance_data = &h80000004

public const hkey_current_config = &h80000005

public const hkey_dyn_data = &h80000006

public const reg_none = 0

public const reg_sz = 1

public const reg_expand_sz = 2

public const reg_binary = 3

public const reg_dword = 4

public const reg_dword_big_endian = 5

public const reg_multi_sz = 7

'注意以下的函数声明须在一行内写完

declare function regopenkey lib "advapi32.dll" alias "regopenkeya" (byval hkey as long, byval lpsubkey as string, phkresult as long) as long declare function regclosekey lib "advapi32.dll" (byval hkey as long) as long

declare function regqueryvalue lib "advapi32.dll" alias "regqueryvaluea" (byval hkey as long, byval lpsubkey as string, byval lpvalue as string, lpcbvalue as long) as long

declare function regqueryvalueex lib "advapi32.dll" alias "regqueryvalueexa" (byval hkey as long, byval lpvaluename as string, byval lpreserved as long, lptype as long, lpdata as any, lpcbdata as long) as long

declare function regenumvalue lib "advapi32.dll" alias "regenumvaluea" (byval hkey as long, byval dwindex as long, byval lpvaluename as string, lpcbvaluename as long, lpreserved as long, lptype as long, lpdata as byte, lpcbdata as long) as long

declare function regenumvalueasany lib "advapi32.dll" alias "regenumvaluea" (byval hkey as long, byval dwindex as long, byval lpvaluename as string, lpcbvaluename as long, lpreserved as long, lptype as long, lpdata as any, lpcbdata as long) as long

declare function regenumvalueasany2 lib "advapi32.dll" alias "regenumvaluea" (byval hkey as long, byval dwindex as long, lpvaluename as any, lpcbvaluename as long, lpreserved as long, lptype as long, lpdata as any, lpcbdata as long) as long

declare function expandenvironmentstrings lib "kernel32" alias "expandenvironmentstringsa" (byval lpsrc as string, byval lpdst as string, byval nsize as long) as long

sub multistringtostringarray(s as string, s2() as string)

's为我们读取出来的多重字符串

's2为转换后的字符串数组

dim count as integer, pos as integer, pos2 as integer, idx as integer

pos = instr(s, chr(0))

while pos > 0 count = count + 1

pos = instr(pos + 1, s, chr(0))

wend

'取得多重字符串中的字符串个数

count = count - 1

redim s2(0 to count - 1)

pos = 1

for idx = 0 to count - 1

pos2 = instr(pos, s, chr(0))

s2(idx) = mid(s, pos, pos2 - pos)

pos = pos2 + 1

next

end sub

'在form中添加command按钮和text文本框

'************enumval2.frm****************

'以下的command1_click事件中我们将列举出'hkey_local_machine\software\microsoft\windows\currentversion\run下的所有name及其value.

private sub command1_click()

dim hkey as long, ret as long, lendata as long, typedata as long dim name as string

dim lenname as long

dim idx as integer, j as integer dim bname(256) as byte

ret = regopenkey(hkey_local_machine, "software\microsoft\windows\currentversion\run", hkey)

if ret <> 0 then exit sub

ret = 0

idx = 0

while ret = 0

lenname = 256

ret=regenumvalueasany2(hkey,idx,bname(0),lenname,byval 0,typedata,byval vbnullstring, lendata)

if ret <> 0 then

regclosekey hkey

exit sub

end if

'上面的regenumvalueasany2调用得到了第一个name的长度lenname,不含chr(0)

name = string(lenname + 1, chr(0))

lenname = len(name)

select case typedata

case reg_sz, reg_expand_sz, reg_multi_sz

dim s as string

s = string(lendata, chr(0))

regenumvalueasany hkey, idx, name, lenname, byval 0, typedata, byval s, lendata

if typedata = reg_sz then

s = left(s, instr(s, chr(0)) - 1)

text1.seltext=iif(lenname=0, "(预设值)",left(name,instr(name,chr(0))-1)) & "=" & s & vbcrlf

elseif typedata = reg_expand_sz then

dim s2 as string

s2 = string(len(s) + 256, chr(0))

expandenvironmentstrings s, s2, len(s2)

s = left(s2, instr(s2, chr(0)) - 1)

text1.seltext = left(name, instr(name, chr(0)) - 1) & " = " & s & vbcrlf

elseif typedata = reg_multi_sz then

dim sarr() as string

multistringtostringarray s, sarr

for j = 0 to ubound(sarr)

text1.seltext = left(name, instr(name, chr(0)) - 1) & "(" & j & ") = " & sarr(j) & vbcrlf

next

end if

case reg_dword, reg_dword_big_endian

dim l as long

regenumvalueasany hkey, idx, name, lenname, byval 0, typedata, l, lendata

text1.seltext = left(name, instr(name, chr(0)) - 1) & " = " & l & vbcrlf

case reg_binary

redim barr(0 to lendata - 1) as byte

regenumvalueasany hkey, idx, name, lenname, byval 0, typedata, barr(0), lendata

text1.seltext = left(name, instr(name, chr(0)) - 1) & " = "

for j = 0 to ubound(barr)

text1.seltext = hex(barr(j)) & " "

next

text1.seltext = vbcrlf

end select

idx = idx + 1

wend

regclosekey hkey

end sub



  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons