从C#调用C ++ / CLI,带out参数 [英] calling C++/CLI from C# with out parameter

查看:744
本文介绍了从C#调用C ++ / CLI,带out参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些参考资料,以便在使用C ++ / CLI连接C#时,更好地了解 out 参数(以及使用的'%'运算符)。
使用VS2012和此msdn参考: msdn ref



使用/ clr编译的C ++ DLL代码

  #pragma once 
using namespace System;

命名空间MsdnSampleDLL {

public ref class Class1
{
public:
void TestOutString([Runtime :: InteropServices :: Out] String ^%s)
{
s =只是一个字符串;
}
void TestOutByte([Runtime :: InteropServices :: Out] Byte ^%b)
{
b =(Byte)
}
};
}

而C#代码:

 使用系统; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

使用MsdnSampleDLL;
namespace MsdnSampleApp
{
class Program
{
static void Main(string [] args)
{
Class1 cls = new Class1 );
string str
cls.TestOutString(out str);

System.Console.WriteLine(str);

Byte aByte =(Byte)3;
cls.TestOutByte(out aByte);
}
}
}

此代码的字符串部分(从msdn复制)工作正常。但是当我试图扩展这个想法,通过一个字节填充 - 我有编译C#

时出现以下错误


参数1:不能从'out byte'转换为'out System.ValueType'


所以显然我不是从msdn文档。

解决方案

问题出在你的C ++ / CLI声明中:

  void TestOutByte([Runtime :: InteropServices :: Out] Byte ^%b)
/ pre>

System :: Byte 是一个值类型,而不是引用类型,因此它不会得到 ^ 。对值类型的引用不是可以在C#中表示的,因此使用对 ValueType 的引用。



除去字节上的 ^ 它会工作正常。


Need some references to better understand the out parameter (and the '%' operator used) when interfacing C# with C++/CLI. Using VS2012 and this msdn reference:msdn ref

C++ DLL code compiled with /clr

#pragma once
using namespace System;

namespace MsdnSampleDLL {

    public ref class Class1
    {
    public:
        void TestOutString([Runtime::InteropServices::Out] String^ %s) 
        {
            s = "just a string";
        }
        void TestOutByte([Runtime::InteropServices::Out] Byte^ %b) 
        {
            b = (Byte)13;
        }
    };
}

And the C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MsdnSampleDLL;
namespace MsdnSampleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 cls = new Class1();
            string str;
            cls.TestOutString(out str);

            System.Console.WriteLine(str);

            Byte aByte = (Byte)3;
            cls.TestOutByte(out aByte);            
        }
    }
}

The string portion of this code (copied from msdn) works fine. But when I tried to expand on the idea with passing a Byte to be filled in - I got the following error from compiling the C#

Argument 1: cannot convert from 'out byte' to 'out System.ValueType'

So obviously I'm just not "getting it" from the msdn docs. I'd appreciate links to better documentation to explain this.

解决方案

The problem is in your C++/CLI declaration:

void TestOutByte([Runtime::InteropServices::Out] Byte^ %b) 

System::Byte is a value type, not a reference type, therefore it doesn't get the ^. A reference to a value type isn't something that can be represented in C#, so a reference to ValueType is used instead.

Get rid of the ^ on the Byte, and it'll work fine.

这篇关于从C#调用C ++ / CLI,带out参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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