简体   繁体   中英

How to return a string of unknown size from DLL to Visual Basic

I have a visual basic script that calls to a DLL which does network requests and returns the result of one request as a string. The length of the result is unknown before calling the DLL. The DLL is written in C/C++ by myself.

As far as I see, the most often used way to return strings from a DLL is to pass a reference of a preallocated string object as arguement to the DLL. The DLL function then just fills this memory with the returning string. The problem is that the allocated buffer has to be large enough, to make sure the result fits into it.

Is it possible to directly return the resulting string from the DLL? Or is there any other way to dynamically allocate a string object depending on the length of the result and return it to a VB caller?

I tried different approaches, for example like this (ugly, just examples):

__declspec(dllexport) const char* sendCommand(const char* cmd, const char* ipAddress)
{
    // do request stuff... 
    long lengthOfResult = ...
    const char* result = new char[lengthOfResult];
    return result;
}

Or like..

__declspec(dllexport) BSTR sendCommand(const char* cmd, const char* ipAddress)
{
    _bstr_t result("Test string.");
    BSTR bstrResult = result.copy();
    return bstrResult;
}

The visual basic side:

Declare Function sendCommand Lib "magicdll.dll" (cmd as String, ip as String) As String
result = sendCommand("any command", "192.168.1.1")

Both without success - the resulting string in VB is filled with garbage.

Most DLLs don't return a string. They accept a string as a param and copy a char array into that buffer. Try something like this:

_declspec(dllexport) int sendCommand(const char* cmd, 
                                     const char* ipAddress, 
                                     char* pszBuffer, 
                                     int nBufferSize)

And then copy your string into that buffer and return the number of chars:

int nSize = nBufferSize;
if (lstrlen(szMyResult) < nBufferSize)
    nSize = lstrlen(szMyResult);

lstrcpyn(pszBuffer, szMyResult, nSize);
return nSize;

When calling from VB, allocate a string and specify its size:

Dim s As String, intChars As Long
s = Space$(128)

intChars = sendCommand("...", "...", s, Len(s))
s = Left$(s, intChars) 

Edit:

If you must return a string as the result of your function call, you can try creating a BSTR (a VB-style string) and returning that. You'll need to convert your string to Unicode and then use SysAllocString() to create the BSTR. For example:

BSTR ReturnVBString() 
{
    return SysAllocString(L"This string is from a C DLL.");
} 

Joining the conversation late, but...

The initial question asked how to handle a variable buffer size. I believe the only way to do this is thru two functions calls. One to get the buffer size, then another to get the string.

I'm using a similar technique to get an encryption string in an access form using a C++ function. The VBA looks similar to this (details left out):

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

And some C++ functions with the guts stripped out (actual functions do heavier lifting than this - but you should get the idea)

//    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;
}

Your mileage may vary...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM