Article ID: 187912
Article Last Modified on 3/3/2005
#include <windows.h>
void __stdcall DisplayStringByVal(LPCSTR pszString)
{
//pszString is a pointer to a string
MessageBox(NULL, pszString, "Display String ByVal",
MB_OK | MB_ICONINFORMATION);
}
void __stdcall DisplayStringByRef(LPCSTR* ppszString)
{
//ppszSting is a pointer to a pointer to a string
MessageBox(NULL, *ppszString, "Display String ByRef",
MB_OK | MB_ICONINFORMATION);
}
void __stdcall FillString(LPSTR pszString, LONG cSize)
{
// Create a temp buffer with our string
char buffer[] = "Hello from the C DLL!";
// Copy our temp string to pszString
// but check the size to make sure we have a buffer
// big enough to hold the entire string.
if (cSize > strlen(buffer))
strcpy(pszString, buffer);
}
int __stdcall InStrRev(LPCSTR pszString, short iChar)
{
// This function is similar to Visual Basic's InStr function
// except that it searches for the given ASCII character from
// right to left, returning the character position of the
// last occurrence (rather than the first) of the character
// in the string.
char* pszTmp;
int nRet = 0;
// Scan for iChar in pszString backwards
pszTmp = strrchr(pszString, (int)iChar);
if(pszTmp != NULL)
nRet = pszTmp - pszString + 1;
return nRet;
}
LIBRARY StrSamp
DESCRIPTION 'Microsoft KB Sample DLL'
EXPORTS
DisplayStringByVal
DisplayStringByRef
FillString
InStrRev
Option Explicit
Private Declare Sub DisplayStringByRef Lib "StrSamp.dll" _
(sMyString As String)
Private Declare Sub DisplayStringByVal Lib "StrSamp.dll" _
(ByVal sMyString As String)
Private Declare Sub FillString Lib "StrSamp.dll" _
(ByVal sMyString As String, ByVal cBufferSize As Long)
Private Declare Function InStrRev Lib "StrSamp.dll" _
(ByVal sMyString As String, ByVal iChar As Integer) _
As Long
Note that most of the strings are declared ByVal. This does not mean
that you are passing these strings by value; rather, you are passing the
value of the BSTR variable by value (remember that a BSTR variable is a
pointer to a string). So the ByVal keyword has the effect of passing a
long pointer to a string (LPSTR), just what the C functions expect.
Private Sub Command1_Click()
Dim sTestString1 As String
Dim sTestString2 As String
sTestString1 = "This is my string passed to the dll by value."
DisplayStringByVal sTestString1
sTestString2 = "This is my string passed to the dll by reference."
DisplayStringByRef sTestString2
End Sub
Private Sub Command2_Click()
Dim sFillTest As String
sFillTest = Space$(260)
FillString sFillTest, 260
MsgBox Trim$(sFillTest), vbInformation, "Fill String"
End Sub
Private Sub Command3_Click()
Dim sPathString As String
Dim sMsg As String
Dim lCharPosition As Long
sPathString = "C:\My Documents\Temp\Item.txt"
lCharPosition = InStrRev(sPathString, Asc("\"))
If CBool(lCharPosition) Then
sMsg = "The file '" & Mid$(sPathString, lCharPosition + 1)
sMsg = sMsg & "' is at this location:" & vbCrLf & vbCrLf
sMsg = sMsg & Left$(sPathString, lCharPosition - 1)
MsgBox sMsg, vbInformation, "InStrRev"
Else
MsgBox "Cannot find '/' in " & sPathString, vbCritical
End If
End Sub
"DLLs for Beginners" in the Microsoft Developer Network (MSDN) Library "VB5DLL.DOC" located on the Visual Basic 5.0 CD-ROM in the "Tools\Docs" directory.
142840 : Visual Basic Requirements for Exported DLL Functions
Keywords: kbhowto kbapi kbdll KB187912