为了保证你使用正确的调用规范,要通知编译器使用stdcall规范和/或使用在windows.h(及相关文件)中定义的常量,如WINAPI等。通常DLL的代码如下:
/////////////////////////////////////////////////////////////////////
// Shifts bits right for integers.
WORD WINAPI vbShiftRight(WORD nValue, WORD nBits)
{
return (nValue >> nBits);
}
下一步是在VB中调用这个函数,使用以下声明:
Declare Function vbShiftRight Lib "MYDLL.DLL" (ByVal nValue As Integer,
ByVal nBits As Integer)
As Integer
Sub Test()
Dim i As Integer
i = vbShiftRight(4, 2)
Debug.Assert i = 1
End Sub
如果你还想要更容易的方法从VB中调用,可以创建一个类型库。为此你需要创建和编译ODL(对象描述语言)文件。这个文件应该包含如下内容:
module MyModule {
[
helpstring("Shifts the bits of an integer to the right."),
entry("vbShiftRight")
]
short _stdcall vbShiftRight([in] short nValue, [in] short nBits);
};