打印本文 打印本文 关闭窗口 关闭窗口
VB与Windows API 间的呼叫技巧
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2370  更新时间:2009/4/23 16:40:02  文章录入:mintao  责任编辑:mintao
sp;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


上一页  [1] [2] [3] 

打印本文 打印本文 关闭窗口 关闭窗口