如何在调用WindowsPhoneRuntimeComponent时使用字节数组作为参数
how to use byte array as parameter when calling a WindowsPhoneRuntimeComponent?
首先,大家好!我想从我的windows手机应用程序访问2 c++函数。所以我遵循本教程的每一步,我设法调用函数作为本教程的海报。现在我想访问我自己的函数,所以我在头和。cpp文件中创建了类,只要我的函数不是公共的,项目就可以构建。意味着我不能访问它们。
public ref class Base64Encoding sealed
{
public:
char *EncodeData(char *data, int length, int *resultLength); //this doesnt compile
char *DecodeString(char *data, int *resultLength); //this compiles buts inaccessible
};
我得到一个返回异常说错误C3992:公共成员的签名包含无效类型char。我做了一些谷歌搜索,据我所知,我不能发送char类型的参数,因为它的非托管代码或类似的东西。
那么问题是什么呢?为什么我不能传递char类型的参数?
更新
我遵循了robwirving的建议,现在标题看起来像这样。
public ref class Base64Encoding sealed
{
public : Platform::String^ EncodeData(String^ StringData);
public : Platform::String^ DecodeString(String^ StringData);
};
现在为了从字符串^ StringData参数中获取char*数据,我在我的。cpp
#include <string>
#include <iostream>
#include <msclrmarshal_cppstd.h>
using namespace Platform;
using namespace std;
String^ EncodeData(String^ StringData)
{
// base64 lookup table. this is the encoding table for all 64 possible values
static char *figures = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
msclr::interop::marshal_context context;
std::string s = context.marshal_as<std::string>(StringData);
char *data = new char[s.size() + 1];
int length = s.length;
int *resultLength = s.length;
data[s.size()] = 0;
/* bla bla some functions irrelevant*/
.
.
.
return StringFromAscIIChars(result);
}
static String^ StringFromAscIIChars(char* chars)
{
size_t newsize = strlen(chars) + 1;
wchar_t * wcstring = new wchar_t[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, newsize, chars, _TRUNCATE);
String^ str = ref new Platform::String(wcstring);
delete[] wcstring;
return str;
}
但是现在我得到了2个错误的构建。
1:错误C1114: WinRT不支持使用托管程序集
2: IntelliSense:不允许使用普通指针指向c++/CX映射ref类或接口类
你问题的答案是正确的。不能在WinRT组件的公共函数中使用char。但是,您可以使用字符串,我建议将您的函数更改为:
public ref class Base64Encoding sealed
{
public:
Platform::String^ EncodeData(Platform::String^ data);
Platform::String^ DecodeString(Platform::String^ data);
};
在你的编码/解码函数的定义中,你可以将你的输入从一个Platform::String^转换成一个char数组,调用你原来的c++函数,然后将返回值转换回一个Platform::String^
我知道这看起来像是很多额外的工作,但它使使用WinRT组件的c#更容易进行互操作。
更新:
我认为您的额外错误可能来自包含mscr marshal_cppstd.h以及您将Platform::String^转换为std:: String的方式。
参考这篇文章如何从Platform::String^转换为char*:如何将Platform::String转换为char*?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 基于字节数组生成静态范围整数值
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 swig 追加到字节数组
- C++ 替换字节数组中项的顺序
- QDataStream 读取和写入的字节数比 QFile::length() 报告要多
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 如何在C++(Arduino)中将浮点数组转换为字节数组
- 将字节数组转换为多维数组
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- 如何将带有空字符的字节数组馈送到 std::iostream 中?
- C++字节数组到整数
- 将字符串转换为无符号字符数组/字节数组
- 用于 progmem 的C++和头文件压缩的 Web 文件字节数组
- mmap 中的长度是字节数还是页数?
- 如果我向一个12字节的缓冲区写入的字节数少于12,会发生什么情况
- 从 UTF-8 字节数组创建字符串?
- ReadFile() 读取的字节数少于其参数中的指定值