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

// 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;
}
发布了9 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/bukajiushang/article/details/103914782