如何将文本存储在WCHAR_T指针参数中

发布于 2025-02-10 04:59:01 字数 1132 浏览 0 评论 0原文

我想DLL从CPP到DART导出一些功能,为此,我需要使用指针参数创建一个函数,我将在其中发送文本。

但是经过多次搜索,我没有找到可行的解决方案。

我的问题是:如何使用wchar_t*参数创建函数,并用文本填充该变量?我不知道CPP指针,我通常不在CPP中编码。

这是我的代码,它给出了错误:


//extern "C" __declspec(dllexport) 
void getAudioDevices(wchar_t* output, int outSize)
{
    std::vector<DeviceProps> result = EnumAudioDevices(eRender);
    std::wstring listOutput(L"");
    if (!result.empty())
    {
        for (const auto& device : result)
        {
            std::wstring info(L"");
            info += device.name; //PCWSTR
            info += L"\n";
            info += device.iconInfo; //PCWSTR
            info += L"\n";
            info += device.id; //LPWSTR
            info += L"\n";
            info += device.isActive ? L"true" : L"false";
            info += L"\n[][][][]\n";
            listOutput += info.c_str();
        }
    }
    wcscpy_s(output, outSize, listOutput.c_str());
}
int wmain(int argc, WCHAR* argv[])
{
    WCHAR output[1000];
    getAudioDevices(output, sizeof(output));
    cout << output << endl;

}

I want to dll export some functions from cpp to dart and in order to do this I need to create a function with a pointer parameter where I will send text.

But after many searches I found no solution that works.

My question is: How to create a function with a wchar_t* parameter, and fill that variable with text? I have no knowledge of cpp pointers, I usually dont code in cpp.

Here is my code, it gives errors:


//extern "C" __declspec(dllexport) 
void getAudioDevices(wchar_t* output, int outSize)
{
    std::vector<DeviceProps> result = EnumAudioDevices(eRender);
    std::wstring listOutput(L"");
    if (!result.empty())
    {
        for (const auto& device : result)
        {
            std::wstring info(L"");
            info += device.name; //PCWSTR
            info += L"\n";
            info += device.iconInfo; //PCWSTR
            info += L"\n";
            info += device.id; //LPWSTR
            info += L"\n";
            info += device.isActive ? L"true" : L"false";
            info += L"\n[][][][]\n";
            listOutput += info.c_str();
        }
    }
    wcscpy_s(output, outSize, listOutput.c_str());
}
int wmain(int argc, WCHAR* argv[])
{
    WCHAR output[1000];
    getAudioDevices(output, sizeof(output));
    cout << output << endl;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧街凉风 2025-02-17 04:59:01

sizeof(输出)将返回数组的字节数。在Windows上,这将是2000而不是1000,因为每个WCHAR_T长2个字节。

但是传递给WCSCPY_S的值是您的数组可以保留的宽字符数。您的数组可以容纳1000个WCHAR_T。但是您实际上是在告诉WCSCPY_S,它可以容纳更多。

更改此信息:

getAudioDevices(output, sizeof(output));

对此:

getAudioDevices(output, sizeof(output)/sizeof(output[0]) );

声明oftize属于size_t而不是int

sizeof(output) will return the number of bytes the array is. On Windows this will be 2000 instead of 1000 since each wchar_t is 2 bytes long.

But the the value passed to the wcscpy_s is the number of wide characters that your array can hold. Your array can hold 1000 wchar_t's. But your are actually telling wcscpy_s that it can hold more than that.

Change this:

getAudioDevices(output, sizeof(output));

To this:

getAudioDevices(output, sizeof(output)/sizeof(output[0]) );

Declare outSize to be of type size_t as well instead of int

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文