VB与Windows API 间的呼叫技巧[5]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 firetoucher 的 blog

const key_read = ((standard_rights_read or _
      key_query_value or key_enumerate_sub_keys or _
      key_notify) and (not synchronize))

dim key5 as string, valuename as string, strbuff as string, resultstr as string
dim leng1 as long, resul as long, hkey as long
dim tp as long, i as long

key5 = " software\microsoft\windows\currentverson "
resul = regopenkeyex(hkey_classes_root, key5, 0, key_read, hkey)
   ’hkey便是subkey (key5)的keyhandle,先取得它才能存取subkey内的valuename
valuename= "productid "
tp = reg_sz
strbuff = string(255, 0)
leng1 = len(strbuff) + 1
resul = regquerystring(hkey, valuename, 0, tp, strbuff, leng1)
   ’注意,第三个参数传0,leng1传回copy 到strbuff的字元个数(anci)
leng1 = instr(1, strbuff, chr(0), vbbinarycompare) ’重新算个数(unicode)
resultstr = left(strbuff,leng1-1)   ’这便是productid的值
*****************************************************************************
    在这里有另外一件事要特别说明,范例三程式中有一行leng1=len(strbuffer)+1,
这行可省不得,很奇怪吧,为什麽明明是一个传回值,却一定要设定给它一个strbuff
的大小呢?这是因为许多win api 不会聪明到找strbuff的null char在哪里,所以需要
程式传进去,而後它再依这个栏位传回填入strbuff 的数目。

五、array参数的传递

    我们知道win api 的阵列传递是传阵列的起始位址,所以了,在vb中唯一要注意的
是起始位置的写法。以另一个取得window目录所在路径的api为
例:
-----------------------------------------------------------------------------
uint getwindowsdirectory(
    lptstr     lpbuffer,       // address of buffer for windows directory
    uint       usize           // size of directory buffer
   );                          // 若成功,则传回目录的字元数
vb的宣告(api检视员)
declare function getwindowsdirectory lib "kernel32" alias  _
   "getwindowsdirectorya" (byval lpbuffer as string, byval nsize as long) _
   as long
我们将之更改为
declare function getwindowsdirectory lib "kernel32" alias  _
  "getwindowsdirectorya" ( lpbuffer as byte, byval nsize as long) as long

本文关键:Visual Basic
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top