Visual Basic 和 C++ 的 DLL 之间传递一个字符串的方法
.cpp
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);
}
{
// 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);
}
.def(不可或缺的,之前一直少了这个,报很多奇怪的问题)
LIBRARY StrSamp
DESCRIPTION 'Microsoft KB Sample DLL'
EXPORTS
DisplayStringByVal
DisplayStringByRef
FillString
InStrRev
DESCRIPTION 'Microsoft KB Sample DLL'
EXPORTS
DisplayStringByVal
DisplayStringByRef
FillString
InStrRev
VB
Private Declare Sub FillString Lib "StrSamp.dll" _
(ByVal sMyString As String, ByVal cBufferSize As Long)
(ByVal sMyString As String, ByVal cBufferSize As Long)
Private Sub Command2_Click()
Dim sFillTest As String
sFillTest = Space$(260)
FillString sFillTest, 260
MsgBox Trim$(sFillTest), vbInformation, "Fill String"
End Sub
Dim sFillTest As String
sFillTest = Space$(260)
FillString sFillTest, 260
MsgBox Trim$(sFillTest), vbInformation, "Fill String"
End Sub
from:
http://support.microsoft.com/kb/187912