<bdo id='wH4OI'></bdo><ul id='wH4OI'></ul>
        <tfoot id='wH4OI'></tfoot>

        <small id='wH4OI'></small><noframes id='wH4OI'>

      1. <legend id='wH4OI'><style id='wH4OI'><dir id='wH4OI'><q id='wH4OI'></q></dir></style></legend>
      2. <i id='wH4OI'><tr id='wH4OI'><dt id='wH4OI'><q id='wH4OI'><span id='wH4OI'><b id='wH4OI'><form id='wH4OI'><ins id='wH4OI'></ins><ul id='wH4OI'></ul><sub id='wH4OI'></sub></form><legend id='wH4OI'></legend><bdo id='wH4OI'><pre id='wH4OI'><center id='wH4OI'></center></pre></bdo></b><th id='wH4OI'></th></span></q></dt></tr></i><div id='wH4OI'><tfoot id='wH4OI'></tfoot><dl id='wH4OI'><fieldset id='wH4OI'></fieldset></dl></div>

        如何将未知大小的字符串从 DLL 返回到 Visual Basic

        时间:2023-08-28
          <tbody id='ztuLS'></tbody>

      3. <tfoot id='ztuLS'></tfoot>
          <i id='ztuLS'><tr id='ztuLS'><dt id='ztuLS'><q id='ztuLS'><span id='ztuLS'><b id='ztuLS'><form id='ztuLS'><ins id='ztuLS'></ins><ul id='ztuLS'></ul><sub id='ztuLS'></sub></form><legend id='ztuLS'></legend><bdo id='ztuLS'><pre id='ztuLS'><center id='ztuLS'></center></pre></bdo></b><th id='ztuLS'></th></span></q></dt></tr></i><div id='ztuLS'><tfoot id='ztuLS'></tfoot><dl id='ztuLS'><fieldset id='ztuLS'></fieldset></dl></div>
              <bdo id='ztuLS'></bdo><ul id='ztuLS'></ul>
              • <small id='ztuLS'></small><noframes id='ztuLS'>

                <legend id='ztuLS'><style id='ztuLS'><dir id='ztuLS'><q id='ztuLS'></q></dir></style></legend>

                  本文介绍了如何将未知大小的字符串从 DLL 返回到 Visual Basic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个可视化的基本脚本,它调用一个 DLL,该 DLL 执行网络请求并将一个请求的结果作为字符串返回.在调用 DLL 之前,结果的长度是未知的.DLL 是我自己用 C/C++ 编写的.

                  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.

                  据我所知,从 DLL 返回字符串的最常用方法是将预分配的字符串对象的引用作为参数传递给 DLL.DLL 函数然后只是用返回的字符串填充这个内存.问题是分配的缓冲区必须足够大,以确保结果适合它.

                  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.

                  是否可以直接从 DLL 返回结果字符串?或者有没有其他方法可以根据结果的长度动态分配一个字符串对象并将其返回给VB调用者?

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

                  或者喜欢..

                  __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 中的结果字符串充满了垃圾.

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

                  推荐答案

                  大多数 DLL 不返回一个字符串.他们接受一个字符串作为参数并将一个字符数组复制到该缓冲区中.尝试这样的事情:

                  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;
                  

                  从VB调用时,分配一个字符串并指定其大小:

                  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) 
                  

                  如果您必须返回一个字符串作为函数调用的结果,您可以尝试创建一个 BSTR(一个 VB 样式的字符串)并返回它.您需要将字符串转换为 Unicode,然后使用 SysAllocString() 创建 BSTR.例如:

                  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.");
                  } 
                  

                  这篇关于如何将未知大小的字符串从 DLL 返回到 Visual Basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 C++ DLL 调用 Delphi 中的回调函数 下一篇:使用 Visual Studio 链接到第 3 方库

                  相关文章

                  <legend id='hMrCx'><style id='hMrCx'><dir id='hMrCx'><q id='hMrCx'></q></dir></style></legend>
                  • <bdo id='hMrCx'></bdo><ul id='hMrCx'></ul>
                1. <small id='hMrCx'></small><noframes id='hMrCx'>

                  <i id='hMrCx'><tr id='hMrCx'><dt id='hMrCx'><q id='hMrCx'><span id='hMrCx'><b id='hMrCx'><form id='hMrCx'><ins id='hMrCx'></ins><ul id='hMrCx'></ul><sub id='hMrCx'></sub></form><legend id='hMrCx'></legend><bdo id='hMrCx'><pre id='hMrCx'><center id='hMrCx'></center></pre></bdo></b><th id='hMrCx'></th></span></q></dt></tr></i><div id='hMrCx'><tfoot id='hMrCx'></tfoot><dl id='hMrCx'><fieldset id='hMrCx'></fieldset></dl></div>

                  <tfoot id='hMrCx'></tfoot>