如何将文本存储在WCHAR_T指针参数中
我想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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sizeof(输出)
将返回数组的字节数。在Windows上,这将是2000而不是1000,因为每个WCHAR_T长2个字节。但是传递给WCSCPY_S的值是您的数组可以保留的宽字符数。您的数组可以容纳1000个WCHAR_T。但是您实际上是在告诉WCSCPY_S,它可以容纳更多。
更改此信息:
对此:
声明
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:
To this:
Declare
outSize
to be of typesize_t
as well instead ofint