如何传递C ++ short *到C ++ / CLI中的托管C#程序集 [英] How to pass a C++ short* to managed C# assembly in C++/CLI

查看:184
本文介绍了如何传递C ++ short *到C ++ / CLI中的托管C#程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将参数从C ++ / CLI代码传递到.NET C#函数。

I'm having trouble passing an argument from C++/CLI code into a .NET C# function.

在C ++中,我有类似以下的东西:

In C++ I have something resembling the following:

void SomeFunction(short *id)
{
   CSharpClass::StaticClassInstance->SetValue(id);
}

在C#方面,函数声明为ref参数: / p>

On the C# side, the function is declared with a ref argument as:

public void SetValue(ref short id)
{
  id = this.internalIdField;
}

调用SetValue(id)时遇到的编译器错误是can not将参数1从short *转换为short%。

The compiler error I'm getting when calling SetValue(id) is "cannot convert parameter 1 from 'short *' to 'short %'".

我发现跟踪引用(%)等效于C#ref,但我不知道如何使用short *参数我试图

I found out that a tracking reference (%) is equivalent to C# ref but I don't know how to use it with the short* parameter I'm trying to pass.

提前感谢。

推荐答案

逻辑C ++ / CLI的签名 CSharpClass :: SetValue

The logical C++/CLI signature of CSharpClass::SetValue is

void SetValue(short% id);

如果你知道C ++(而不是C ++ / CLI),这里的答案是完全相同的这将是如果你有C + +签名

If you know C++ (as opposed to C++/CLI), the answer here is the exact same as it would be if you had the C++ signature

void SetValue(short& id);

即,只需取消引用指针:

I.e., simply dereference the pointer:

void SomeFunction(short *id)
{
    CSharpClass::StaticClassInstance->SetValue(*id);
}

这篇关于如何传递C ++ short *到C ++ / CLI中的托管C#程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆