API函数中同一类型参数类型声明 在不同函数中转化成VB函数声明时对应不同类型的疑惑
1、int rf_load_key(unsigned char *_NKey);这个函数在VB中的声明是:
Declare Function rf_load_key Lib "mwrf32.dll" (ByRef nkey As Byte) As Integer
unsigned char *_NKey对应转化为ByRef nkey As Byte
2、int rf_read(unsigned char *_Data);这个函数在VB中声明是:
Declare Function rf_read Lib "mwrf32.dll" (ByVal sdata as string ) As Integer
unsigned char *_NKey对应转化为ByVal sdata as string
问题有以下两个:
1、为什么同一个unsigned char *_NKey对应VB中的两个参数类型?
2、ByVal sdata as string 这种类型 怎么作为指针来使用?
[解决办法]
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
ByRef Destination As Any,_
ByRef Source As Any, _ByVal Length As Long)
可以看出,同一个API函数可以声明为两种数据类型。
如同你所说的例子,你可以试试声明为不同的类型,如:
Declare Function rf_load_key Lib "mwrf32.dll" (ByVal nkey As string) As Integer
Declare Function rf_read Lib "mwrf32.dll" (ByRef sdata as Byte) As Integer
应该同样可以得到正确的结果,不同的是你在VB中调用这样的函数时,传递参数的形式需要改变。
第二、由于C++具备函数重载的能力,因此一些API同样可以实现重载,记得以前写过一个代码,对于同一个API声明了不同的参数类型,也可以将其声明为Any,以减化声明和调用过程。
[解决办法]
Declare Function rf_load_key Lib "mwrf32.dll" (ByRef nkey As Byte) As Integer
Dim aBytes() As Byte
Redim aBytes(99)
Debug.Print rf_load_key(aBytes(0))
Declare Function rf_read Lib "mwrf32.dll" (ByVal sdata as string ) As Integer
Dim s As String
s = Space(100)
Debug.Print rf_load_key(s)