简体   繁体   English

托管的C ++和C#

[英]managed C++ and C#

I have a class in managed C++ that has all its member variables initialized inside the constructor. 我在托管C ++中有一个类,该类的所有成员变量都在构造函数中初始化。 The member of interest is an array. 感兴趣的成员是一个数组。 I am calling it from the .cs file of a C# project, having linked the two projects with the dll of the first one. 我从C#项目的.cs文件中调用它,将两个项目与第一个项目的dll链接在一起。 However, the function says that one or more of the parameters are incorrect and therefore, cannot be used successfully. 但是,该函数表示一个或多个参数不正确,因此无法成功使用。

The class declaration and the function declaration is as follows. 类声明和函数声明如下。 Both are in the .h file. 两者都在.h文件中。

Now, I would like to call the function in the .cs file as follows: 现在,我想按以下方式在.cs文件中调用该函数:

var Driver = new Driver();
long status = Driver.Config2("CAN0", 8, Driver.AttrIdList, Driver.AttrValueList);
Console.WriteLine(status);

If the function Config executes correctly, it should output a 0. However, I am getting a negative number and upon the lookup with the table provided by the vendor, it states that one or more of the parameters are not setup correctly. 如果函数Config正确执行,它应该输出0。但是,我得到一个负数,并且在使用供应商提供的表进行查找时,它指出一个或多个参数未正确设置。 I have no idea how to get past this point since I'm a newbie to managed C++. 我不知道如何克服这一点,因为我是托管C ++的新手。 All help would be greatly appreciated. 所有帮助将不胜感激。 Thanks. 谢谢。

The code declaration is as follows: 代码声明如下:

public ref class Driver
    {
    public:

        NCTYPE_STATUS Status;
        NCTYPE_OBJH TxHandle;
        MY_NCTYPE_CAN_FRAME Transmit;
        array<NCTYPE_UINT32>^ AttrIdList;
        array<NCTYPE_UINT32>^ AttrValueList;
        array<char>^ Data;
        NCTYPE_UINT32 Baudrate;

    public:
        Driver()
        {
            Baudrate = 1000000;
            TxHandle = 0;
            AttrIdList =    gcnew array<NCTYPE_UINT32>(8);
            AttrValueList = gcnew array<NCTYPE_UINT32>(8);
            AttrIdList[0] =         NC_ATTR_BAUD_RATE;   
            AttrValueList[0] =      Baudrate;
            AttrIdList[1] =         NC_ATTR_START_ON_OPEN;
            AttrValueList[1] =      NC_TRUE;
            AttrIdList[2] =         NC_ATTR_READ_Q_LEN;
            AttrValueList[2] =      0;
            AttrIdList[3] =         NC_ATTR_WRITE_Q_LEN;
            AttrValueList[3] =      1;
            AttrIdList[4] =         NC_ATTR_CAN_COMP_STD;
            AttrValueList[4] =      0;
            AttrIdList[5] =         NC_ATTR_CAN_MASK_STD;
            AttrValueList[5] =      NC_CAN_MASK_STD_DONTCARE;
            AttrIdList[6] =         NC_ATTR_CAN_COMP_XTD;
            AttrValueList[6] =      0;
            AttrIdList[7] =         NC_ATTR_CAN_MASK_XTD;
            AttrValueList[7] =      NC_CAN_MASK_XTD_DONTCARE;

            interior_ptr<NCTYPE_UINT32> pstart (&AttrIdList[0]);
            interior_ptr<NCTYPE_UINT32> pend (&AttrIdList[7]);


            Data = gcnew array<char>(8);
            for (int i=0; i<8;i++)
                Data[i]=i*2;

        }

I also have another method right underneath the Config function that is declared as follows: 我在Config函数的下面还有另一个方法,该方法声明如下:

NCTYPE_STATUS Config2 (String^ objName, int numAttrs, array<unsigned long>^ AttrIdList, array<unsigned long>^ AttrValueList )
    {
      msclr::interop::marshal_context^ context = gcnew msclr::interop::marshal_context();
      const char* name = context->marshal_as<const char*>(objName);


      char* name_unconst = const_cast<char*>(name);

      return ncConfig (name_unconst, 8, nullptr, nullptr);
      delete context;

    }

The program compiles and builds, this is a run-time error. 程序编译并生成,这是运行时错误。 I am guessing it has something to do with the two nullptr passed in the function Config2, but if I replace these with the parameters AttrIdList and AttrValueList, the compiler gives an error: cannot convert parameter 3 from 'cli::array^' to 'NCTYPE_ATTRID_P' 我猜想这与函数Config2中传递的两个nullptr有关,但是如果我将它们替换为参数AttrIdList和AttrValueList,则编译器会出现错误:无法将参数3从'cli :: array ^'转换为' NCTYPE_ATTRID_P'

BTW: NCTYPE_STATUS is unsigned long, while NCTYPE_ATTRID_P is unsigned long*. 顺便说一句:NCTYPE_STATUS为无符号长,而NCTYPE_ATTRID_P为无符号长*。

cannot convert parameter 3 from 'cli::array^' to 'NCTYPE_ATTRID_P' 无法将参数3从cli :: array ^转换为NCTYPE_ATTRID_P
NCTYPE_ATTRID_P is unsigned long* NCTYPE_ATTRID_P无符号长*

You can't pass a managed array to a pure native C++ function, you first need to 'convert' it to a fixed unsigned long* pointer. 您不能将托管数组传递给纯本机C ++函数,首先需要将其“转换”为固定的无符号long *指针。

Here's a way to do it: 这是一种方法:

unsigned long* ManagedArrayToFixedPtr(array<unsigned long>^input)
{
    pin_ptr<unsigned long> pinned = &input[0];
    unsigned long* bufferPtr = pinned;

    unsigned long* output = new unsigned long[input->Length];
    memcpy_s(output, input->Length, bufferPtr,  input->Length);

    return output;
}

Testing the function: 测试功能:

array<unsigned long>^ larray = gcnew array<unsigned long> {2,4,6,8,10,12,14,16};
unsigned long* lptr = ManagedArrayToFixedPtr(larray); //returns pointer to 2

Edit: 编辑:
Rememer to #include "windows.h" to be able to use the memcpy_s function! 请记住#include "windows.h"才能使用memcpy_s函数!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM