欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CLI通过函数将C++自定义变量赋值给CLI自定义变量

程序员文章站 2022-07-12 21:12:04
...
// CLI-FunctionParameterPass.cpp : main project file.

#include "stdafx.h"
#include <iostream>

#pragma managed
#include <msclr\marshal_cppstd.h>

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections;
using namespace msclr::interop;

public value struct Node
{
  String^ m_Name;
  int^ m_Number;
};

class CNode {
public:
  std::string m_Name;
  int m_Number;
};


void GetNodeCollection(List<Node>^ nodeCollection, std::vector<CNode>& cnodeCollection)
{
  for each (CNode var in cnodeCollection)
  {
    Node node;
    node.m_Name = marshal_as<String^>(var.m_Name);
    node.m_Number = var.m_Number;
    nodeCollection->Add(node);
  }
}

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

  CNode cnode1;
  cnode1.m_Name = "name1";
  cnode1.m_Number = 11;
  CNode cnode2;
  cnode2.m_Name = "name2";
  cnode2.m_Number = 22;
  std::vector<CNode> cnodeCollection;
  cnodeCollection.push_back(cnode1);
  cnodeCollection.push_back(cnode2);
  for each (CNode var in cnodeCollection)
  {
    std::cout << "CNode m_Name:" << var.m_Name << std::endl;
    std::cout << "CNode m_Number:" << var.m_Number << std::endl;
  }

  List<Node>^ nodeCollection = gcnew List<Node>(cnodeCollection.size());
  GetNodeCollection(nodeCollection, cnodeCollection);

  for each (Node var in nodeCollection)
  {
    Console::WriteLine("Node m_Name:{0}", var.m_Name);
    Console::WriteLine("Node m_Number:{0}", var.m_Number);
  }

  system("pause");

    return 0;
}

 

相关标签: CLI