如何 return 未知大小的字符串从 DLL 到 Visual Basic
How to return a string of unknown size from DLL to Visual Basic
我有一个 visual basic 脚本,它调用一个 DLL,它执行网络请求并且 returns 一个请求的结果作为一个字符串。在调用 DLL 之前,结果的长度是未知的。 DLL是我自己用C/C++写的。
据我所知,从 DLL return 字符串最常用的方法是将预分配字符串对象的引用作为参数传递给 DLL。然后 DLL 函数只用 returning 字符串填充此内存。
问题是分配的缓冲区必须足够大,以确保结果适合它。
是否可以直接return DLL 中的结果字符串?或者是否有任何其他方法可以根据结果的长度动态分配字符串对象并将其 return 分配给 VB 调用者?
我尝试了不同的方法,例如像这样(丑陋,只是例子):
__declspec(dllexport) const char* sendCommand(const char* cmd, const char* ipAddress)
{
// do request stuff...
long lengthOfResult = ...
const char* result = new char[lengthOfResult];
return result;
}
或者喜欢..
__declspec(dllexport) BSTR sendCommand(const char* cmd, const char* ipAddress)
{
_bstr_t result("Test string.");
BSTR bstrResult = result.copy();
return bstrResult;
}
视觉基础方面:
Declare Function sendCommand Lib "magicdll.dll" (cmd as String, ip as String) As String
result = sendCommand("any command", "192.168.1.1")
两者都没有成功 - VB 中的结果字符串充满了垃圾。
大多数 DLL 不 return 字符串。他们接受一个字符串作为参数并将一个字符数组复制到该缓冲区中。尝试这样的事情:
_declspec(dllexport) int sendCommand(const char* cmd,
const char* ipAddress,
char* pszBuffer,
int nBufferSize)
然后将您的字符串复制到该缓冲区和 return 字符数:
int nSize = nBufferSize;
if (lstrlen(szMyResult) < nBufferSize)
nSize = lstrlen(szMyResult);
lstrcpyn(pszBuffer, szMyResult, nSize);
return nSize;
从VB调用时,分配一个字符串并指定其大小:
Dim s As String, intChars As Long
s = Space$(128)
intChars = sendCommand("...", "...", s, Len(s))
s = Left$(s, intChars)
编辑:
如果您必须 return 一个字符串作为函数调用的结果,您可以尝试创建一个 BSTR(VB 风格的字符串)并 return 处理它。您需要将字符串转换为 Unicode,然后使用 SysAllocString()
创建 BSTR。例如:
BSTR ReturnVBString()
{
return SysAllocString(L"This string is from a C DLL.");
}
加入对话晚了,但是...
最初的问题询问如何处理可变缓冲区大小。我相信做到这一点的唯一方法是通过两个函数调用。一个获取缓冲区大小,然后另一个获取字符串。
我正在使用类似的技术通过 C++ 函数在访问表单中获取加密字符串。 VBA 看起来类似于此(省略了细节):
Private Declare Function DllAes_ComputeCipher _
Lib "c:\RTScada\bin\ObjProc.dll" _
Alias "Aes_ComputeCipher" (ByRef sKey As String, ByRef sStr As String) As Integer
Private Declare Function DllAes_GetCipher _
Lib "c:\RTScada\bin\ObjProc.dll" _
Alias "Aes_GetCipher" (ByVal sEncode As Long) As Boolean
Private Sub CipherString_LostFocus()
.
.
.
iSize = DllAes_ComputeCipher(sKey, sString)
sEncoded = Space(iSize)
bSuccess = DllAes_GetCipher(StrPtr(sEncoded))
End Sub
还有一些 C++ 函数被剥离了(实际函数做的比这更重 - 但你应该明白这个想法)
// Define a global string - in reality this is computed by the ComputeCipher function
char* gpStr = "we are the dreamers of dreams and we are the music makers";
#define CLASS_DECLSPEC extern "C" __declspec(dllexport)
//========================================================================
CLASS_DECLSPEC int __stdcall Aes_ComputeCipher(const char* pKey, const char* pStr)
{
return strlen(gpStr);
}
//========================================================================
CLASS_DECLSPEC bool __stdcall Aes_GetCipher(LPSTR pReturn)
{
char pStr = gpStr;
int iLen = strlen(pStr);
int idx;
for (idx = 0; idx < iLen; idx++) {
*pReturn = *pStr;
pReturn += 2;
pStr++;
}
return true;
}
您的里程可能会有所不同...
我有一个 visual basic 脚本,它调用一个 DLL,它执行网络请求并且 returns 一个请求的结果作为一个字符串。在调用 DLL 之前,结果的长度是未知的。 DLL是我自己用C/C++写的。
据我所知,从 DLL return 字符串最常用的方法是将预分配字符串对象的引用作为参数传递给 DLL。然后 DLL 函数只用 returning 字符串填充此内存。 问题是分配的缓冲区必须足够大,以确保结果适合它。
是否可以直接return DLL 中的结果字符串?或者是否有任何其他方法可以根据结果的长度动态分配字符串对象并将其 return 分配给 VB 调用者?
我尝试了不同的方法,例如像这样(丑陋,只是例子):
__declspec(dllexport) const char* sendCommand(const char* cmd, const char* ipAddress)
{
// do request stuff...
long lengthOfResult = ...
const char* result = new char[lengthOfResult];
return result;
}
或者喜欢..
__declspec(dllexport) BSTR sendCommand(const char* cmd, const char* ipAddress)
{
_bstr_t result("Test string.");
BSTR bstrResult = result.copy();
return bstrResult;
}
视觉基础方面:
Declare Function sendCommand Lib "magicdll.dll" (cmd as String, ip as String) As String
result = sendCommand("any command", "192.168.1.1")
两者都没有成功 - VB 中的结果字符串充满了垃圾。
大多数 DLL 不 return 字符串。他们接受一个字符串作为参数并将一个字符数组复制到该缓冲区中。尝试这样的事情:
_declspec(dllexport) int sendCommand(const char* cmd,
const char* ipAddress,
char* pszBuffer,
int nBufferSize)
然后将您的字符串复制到该缓冲区和 return 字符数:
int nSize = nBufferSize;
if (lstrlen(szMyResult) < nBufferSize)
nSize = lstrlen(szMyResult);
lstrcpyn(pszBuffer, szMyResult, nSize);
return nSize;
从VB调用时,分配一个字符串并指定其大小:
Dim s As String, intChars As Long
s = Space$(128)
intChars = sendCommand("...", "...", s, Len(s))
s = Left$(s, intChars)
编辑:
如果您必须 return 一个字符串作为函数调用的结果,您可以尝试创建一个 BSTR(VB 风格的字符串)并 return 处理它。您需要将字符串转换为 Unicode,然后使用 SysAllocString()
创建 BSTR。例如:
BSTR ReturnVBString()
{
return SysAllocString(L"This string is from a C DLL.");
}
加入对话晚了,但是...
最初的问题询问如何处理可变缓冲区大小。我相信做到这一点的唯一方法是通过两个函数调用。一个获取缓冲区大小,然后另一个获取字符串。
我正在使用类似的技术通过 C++ 函数在访问表单中获取加密字符串。 VBA 看起来类似于此(省略了细节):
Private Declare Function DllAes_ComputeCipher _
Lib "c:\RTScada\bin\ObjProc.dll" _
Alias "Aes_ComputeCipher" (ByRef sKey As String, ByRef sStr As String) As Integer
Private Declare Function DllAes_GetCipher _
Lib "c:\RTScada\bin\ObjProc.dll" _
Alias "Aes_GetCipher" (ByVal sEncode As Long) As Boolean
Private Sub CipherString_LostFocus()
.
.
.
iSize = DllAes_ComputeCipher(sKey, sString)
sEncoded = Space(iSize)
bSuccess = DllAes_GetCipher(StrPtr(sEncoded))
End Sub
还有一些 C++ 函数被剥离了(实际函数做的比这更重 - 但你应该明白这个想法)
// Define a global string - in reality this is computed by the ComputeCipher function
char* gpStr = "we are the dreamers of dreams and we are the music makers";
#define CLASS_DECLSPEC extern "C" __declspec(dllexport)
//========================================================================
CLASS_DECLSPEC int __stdcall Aes_ComputeCipher(const char* pKey, const char* pStr)
{
return strlen(gpStr);
}
//========================================================================
CLASS_DECLSPEC bool __stdcall Aes_GetCipher(LPSTR pReturn)
{
char pStr = gpStr;
int iLen = strlen(pStr);
int idx;
for (idx = 0; idx < iLen; idx++) {
*pReturn = *pStr;
pReturn += 2;
pStr++;
}
return true;
}
您的里程可能会有所不同...