Figure 1 YELLER.DLL
YELLER.DEF
; module-definition file for generic -- used by LINK.EXE
LIBRARY Yeller
DESCRIPTION 'Old Yeller 16-bit Sample DLL'
EXETYPE WINDOWS
CODE PRELOAD FIXED
DATA PRELOAD SINGLE
HEAPSIZE 2048
EXPORTS
WEP @1 RESIDENTNAME
GetString @5
GetStringA @10
Powers @15
YELLER.C
#include <windows.h>
#include <windowsx.h>
#include <math.h>
//========================================================
// Takes an integer and creates "String number n" from it.
LPSTR _export WINAPI GetString(int i)
{
LPSTR rval;
rval = GlobalAllocPtr(GHND, 32);
wsprintf(rval, "String number %d", i);
return rval;
}
//========================================================
// Prints text into a passed-in string, and returns the
// total number of bytes used.
int _export WINAPI GetStringA(LPSTR strg, int i)
{
return wsprintf(strg, "StringA number %d", i);
}
//========================================================
// Takes two integers and returns the value of the first
// raised to the second.
double _export WINAPI Powers(int x, int y)
{
static double dbl;
dbl = pow(x, y);
return dbl;
}
Figure 2 VISTHUNK
VISTHUNK.BAS
Attribute VB_Name = "basVisThunk"
Declare Function OGetString Lib "yeller.dll" Alias "GetString" (ByVal x As _ Integer) As Long ' For LPSTR
Declare Function OGetStringA Lib "yeller.dll" Alias "GetStringA" (ByVal stg As _ String, ByVal x As Integer) As Integer
Declare Function OPowers Lib "yeller.dll" Alias "Powers" (ByVal x As Integer, _ ByVal n As Integer) As Double
Declare Function lstrcpy Lib "kernel" (ByVal lpString1 As String, ByVal _
lpString2 As Any) As Long
Sub Main()
End Sub
THUNK.CLS
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Thunk"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Function GetString(i As Integer) As String
Dim l As Long
Dim y As String * 32
l = OGetString(i)
n = lstrcpy(y, l)
GetString = y
End Function
Function GetStringA(str As String, x As Integer) As Integer
Dim irc As Integer
irc = OGetStringA(str, x)
GetStringA = irc
End Function
Function Powers(x As Integer, n As Integer) As Double
Dim dbl As Double
dbl = OPowers(x, n)
Powers = dbl
End Function
Figure 3 Converting Strings
Dim stg As Long
Dim y As String * 32 ' Or some other value - demonstration purposes
' Call the DLL GetString function. It should return "String number 5"
stg = OGetString(5)
' stg is an LPSTR. Copy it into the pre-allocated VB string.
n = lstrcpy(y, stg)
' Use the pre-allocated string to set lblGetString's Caption.
lblGetString.Caption = y
If the string is in a parameter, and not a return value, you don't have to deal with this conversion at all.
FunctionGetStringA(strAsString,xAsInteger)AsInteger
Dim irc As Integer
irc = OGetStringA(str, x)
GetStringA = irc
End Function
Figure 4 Updating Code
Old
Sub btnGetString_Click ()
Dim stg As Long
Dim y As String * 32
stg = GetString(5)
n = lstrcpy(y, stg)
lblGetString.Caption = y
End Sub
New
Private Sub btnGetString_Click()
Dim y As String * 32
y = Obj.GetString(5)
lblGetString.Caption = y
End Sub
Old
Sub btnGetStringA_Click ()
Dim y As String * 32
i = GetStringA(y, 14)
lblGetStringA.Caption = y
End Sub
New
Private Sub btnGetStringA_Click()
Dim y As String * 32
i = Obj.GetStringA(y, 14)
lblGetStringA.Caption = y
End Sub
Old
Sub btnPowers_Click ()
Dim z As Double
z = Powers(2, 13)
lblPowers.Caption = z
End Sub
New
Private Sub btnPowers_Click()
Dim z As Double
z = Obj.Powers(2, 13)
lblPowers.Caption = z
End Sub