Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8359885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:17:39+00:00 2026-06-09T11:17:39+00:00

I have a class in managed C++ that has all its member variables initialized

  • 0

I have a class in managed C++ that has all its member variables initialized inside the constructor. 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. 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.

Now, I would like to call the function in the .cs file as follows:

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. I have no idea how to get past this point since I’m a newbie to managed 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:

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’

BTW: NCTYPE_STATUS is unsigned long, while NCTYPE_ATTRID_P is unsigned long*.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:17:40+00:00Added an answer on June 9, 2026 at 11:17 am

    cannot convert parameter 3 from ‘cli::array^’ to ‘NCTYPE_ATTRID_P’
    NCTYPE_ATTRID_P is unsigned long*

    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.

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface IRenderable and a class that manages rendering all instances of
I have a class called 'Inventory' that has two subclasses, 'Drink' and 'Condiment'. They
I have a MenuObject class that represents a websites Top Menu. This object has
I have a NSObject class MyItem that holds several instance variables, among them a
Im making a program for class that manages a Hotel. I have a function
I have created a class that will manage connection and commands, I named it
I have a GUI tool that manages state sequences. One component is a class
In my game I have a concept of resource manager, a class that stores
I have a managed class library (say mylib.dll) and a 3rd party managed app
i have an managed bean(session scope) like this: class Home {// as homeBean public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.