【发布时间】:2017-02-07 22:47:52
【问题描述】:
我正在开发一个 UWP 项目。我想将一组位置数据(目前我只是发送一个浮点数组作为测试)从 c# 发送到 C++(以便在 XAML 内容之上渲染 DirectX 中生成的网格)。
我试过这个:Improper marshaling: C# array to a C++ unmanaged array(接受的答案)。但它不起作用,我猜我错过了一些东西,但我不知道是什么。当我尝试他的建议时,我的编译器抱怨在 C++ 中声明的 CInput 结构,因为它是本机的,因此它不能是公共函数中的参数。 (c#调用的函数)
(我本来想对这个问题发表评论,但我还没有这个特权。)
这是我的代码:
在 C# 中:
public struct CInput
{
public IntPtr array;
}
public VideoView()
{
InitializeComponent();
Loaded += OnLoaded;
float[] test = new float[4];
CInput input = new CInput();
input.array = Marshal.AllocHGlobal(Marshal.SizeOf<float>() * test.Length);
Marshal.Copy(test, 0, input.array, test.Length);
D3DPanel.CreateMesh(out input, test.Length);
Marshal.FreeHGlobal(input.array);
}
在 C++ 中(在 D3DPanel.h 中):
struct CInput
{
float* array;
};
[Windows::Foundation::Metadata::WebHostHidden]
public ref class D3DPanel sealed : public Track3DComponent::DirectXPanelBase
{
public:
D3DPanel();
void CreateMesh(CInput points, int length);
}
谁能告诉我我做错了什么?
编辑:
我尝试了 PassArray 模式,如 here 所述,但它给出了以下错误:“错误 C4400 'const int': 不支持此类型的 const/volatile 限定符”
void CreateMesh(const Array<float>^ points, int length);
用“Array”替换“const Array^”会得到“语法错误:标识符'Array'”。
【问题讨论】:
-
我正在使用结构,因为我只是在尝试执行已接受的答案中所写的内容。我不确定我是否完全理解你在说什么(实际上,我确定我不明白)。我会查找您所说的类型,看看我是否能更好地理解它,然后再提出新问题。
-
因此,要使用 IVector,我需要创建一个从 IVector 继承的新 ref 类,并使用它来代替 C++/CX 部分中的 CIput 结构。那是对的吗?所以我需要实现IVector中定义的所有功能?
-
当我尝试使用 Platform::Array 时,编译器会抱怨它。 (错误 C2061 语法错误:标识符“数组”)
-
我尝试了 PassArray 模式,如下所述:msdn.microsoft.com/en-us/library/windows/apps/hh700131.aspx 但它给出了这个错误:“错误 C4400 'const int': 不支持这种类型的 const/volatile 限定符”
-
等等,原来的评论发到了吗? (我在这里的第一条评论中回复的那个)
标签: c# uwp marshalling c++-cx