C++ 通过引用将 WCHAR** 传递给函数

C++ pass by reference WCHAR** into a function

我编写了以下代码,我将 WCHAR** 引用传递给一个函数,以便将其归档到函数中。在函数内部,我正在填充这个二维数组。我正在尝试使用按引用传递来完成此操作。

BOOL get_file_names(WCHAR** outname)
{

    WCHAR **outhandlername = NULL;


    if (get_all_the_file_handelers( &outhandlername) > 0)
    {

        (*outname)[0] = *outhandlername[0];

    }

    return ret;
}

int get_all_the_file_handelers(WCHAR** outhandleName )
{



    for (i = 0; i < 10; i++)
    {
     WCHAR *handleName = get_handle_name(handle, i);

    outhandleName = (WCHAR**)realloc(outhandleName, sizeof(WCHAR)*(10 + 1));
    (*outhandleName) = (WCHAR*)realloc(*outhandleName, sizeof(WCHAR)*(1024));
    *outhandleName = handleName;

    }
    return 0;

}

但这似乎不起作用,任何人都可以帮助我理解在这种情况下如何通过引用传递 WCHAR 数组。当我有 WHCAR** 时,将 &WCHAR 传递给第二个函数是正确的,在第二个函数中我应该如何为二维 WCHAR 数组

分配值
int _tmain(int argc, _TCHAR* argv[])
{
      WCHAR *outhandlename=NULL;
      get_file_names(&outhandlename);
      DBGLOG("handler name  %ls", outhandlename);

      return 0;
}

BOOL process_search_file_handle( WCHAR** str)
{
      *str = (WCHAR*) malloc(1024);
      *str = L"TestName";   
      return true;
}

BOOL get_file_names(WCHAR** outname)
    {

        WCHAR **outhandlername = NULL;


        if (get_all_the_file_handelers( &outhandlername) > 0)
        {

            *outname = outhandlername[0];
            DBGLOG("outhandlername value  %ls", outhandlername[0]);
            DBGLOG("str value  %ls", *outname);

        }

        return true;
    }

一旦将 outHandleName 设置为 realloc 的输出,就已经覆盖了 outHandleName,因此您将无法从调用函数更改 outHandleName 的值。

你可以说:

*outHandleName = (WCHAR**) realloc(...);

您还必须将 get_all_the_file_handelers 的方法 header 更改为:

int get_all_the_file_handelers(WCHAR *** outHandleName)

这是因为您在此方法中使用了指向双指针的指针。

此外,您不是通过引用传递 - 您是通过指针传递。

此外,您不应在此处使用 realloc,因为您正在为数组进行初始分配。您的 10 个元素的数组应该在循环之前分配,如下所示:

*outHandleName = (WCHAR **)malloc(sizeof(WCHAR *)*(10+1));
(*outHandleName)[10] = NULL;

请注意,空分配 - 您正在为 10 项数组中的 11 项分配 space,因此我假设您使用最后一项作为占位符 - 作为 NULL - 标记结束数组的。

最后,您不需要为 10 个句柄中的每一个都分配 space 字符串缓冲区,因为您正在从 get_handle_name.

获取字符串

作为奖励,你没有返回任何东西,即使你的代码的其余部分表明你是。

最终的方法是:

int get_all_the_file_handelers(WCHAR ***outHandleName) {
  *outHandleName = (WCHAR **)malloc(sizeof(WCHAR *)*11);
  (*outHandleName)[10] = NULL;
  for(int i = 0; i < 10; i++) {
    WCHAR *handleName = get_handle_name(handle, i);
    (*outHandleName)[i] = handleName;
  }
  return .... /* WHAT AM I RETURNING?? */ ;
}

下面是一些使用标准库生成宽字符串列表的示例代码。后面你应该学会如何处理指针数组,但我建议你先学习如何使用STL。

#include <cstdlib>
#include <experimental/filesystem>
#include <iostream>
#include <locale>
#include <string>
#include <vector>

#if _WIN32 || _WIN64
// Windows needs a little non-standard magic for this to work.
#include <io.h>
#include <fcntl.h>
#include <locale.h>
#endif

using std::endl;
using std::size_t;
using std::wcout;
using std::experimental::filesystem::path;

void init_locale(void)
// Does magic so that wcout can work.
{
#if _WIN32 || _WIN64
  // Windows needs a little non-standard magic.
  constexpr char cp_utf16le[] = ".1200";
  setlocale( LC_ALL, cp_utf16le );
  _setmode( _fileno(stdout), _O_U16TEXT );
#else
  // The correct locale name may vary by OS, e.g., "en_US.utf8".
  constexpr char locale_name[] = "";
  std::locale::global(std::locale(locale_name));
  std::wcout.imbue(std::locale());
#endif
}

std::vector<std::wstring> files_in_wd()
// Returns a list of filenames in the current working directory.
{
  const path cwd = std::experimental::filesystem::current_path();
  std::vector<std::wstring> filenames;

  for ( const path& file : cwd )
    filenames.emplace_back( file.filename().wstring() );

  return filenames;
}

int main(void)
{
  init_locale();

  const std::vector<std::wstring> filenames = files_in_wd();

  for ( const std::wstring& ws : filenames )
    wcout << ws << endl;

  return EXIT_SUCCESS;
}