vc ++.net2005 managed unmanaged mixing code

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

编译时,不要预编译头,要CLR,

//1111111111111111111111111111111111111111111111111111111111111111111111111111
#using <mscorlib.dll>
using namespace System;

#include "stdio.h"
void ManagedFunction()
{
 System::Console::WriteLine("Hello, I'm managed in this section/n");
}

//#pragma unmanaged 本来下面是对应的managed或unmanaged code 但
//这里去掉了也没有报错
void UnmanagedFunction()
{
 printf("Hello, I am unmanaged through the wonder of IJW!/n");
 ManagedFunction();
}

//#pragma managed
int main()
{
 UnmanagedFunction();
 return 0;
}

//2222222222222222222222222222222222222222222222222222222222222222222222222222
#using <mscorlib.dll>
using namespace System;
#include <string>

class Container
{
 int value_;

public:
 Container() : value_(0) {}
 void SetValue(int *val) { value_ = *val;}
 const int& GetValue() { return value_; }

};

ref class ManagedContainer
{
 Container *pContainer;
public:

 ManagedContainer()
 {
  pContainer = new Container();
 }


 void SetValue(int val)
 {
  int someValue = val;


  pContainer->SetValue(&someValue);
 }
 ~ManagedContainer()
 {
  delete pContainer;
 }
};

void main()
{
 ManagedContainer ^mc = gcnew ManagedContainer();
 int someValue = 42;
 mc->SetValue(someValue);

 System::Console::WriteLine("The value is "+
  someValue.ToString());
}


//3333333333333333333333333333333333333333333333333333333333333333333333333333333333
#using <mscorlib.dll>
using namespace System;
#include <string>

class Container
{
 int value_;

public:
 Container() : value_(0) {}
 void SetValue(int *val) { value_ = *val;}
 const int& GetValue() { return value_; }

};

ref class ManagedContainer
{
 Container* pContainer;
public:

 ManagedContainer()
 {
  pContainer = new Container();
 }


 void SetValue(int val)
 {
  int someValue = val;
  pin_ptr<int> pinnedInt = &someValue;
  pContainer->SetValue(pinnedInt);

 }
 ~ManagedContainer()
 {
  delete pContainer;
 }
};

void main()
{
 ManagedContainer ^mc = gcnew ManagedContainer();
 int someValue = 42;
 mc->SetValue(someValue);

 System::Console::WriteLine("The value is " + someValue.ToString());
}
//4444444444444444444444444444444444444444444444444444444444444444444444444444

#using <mscorlib.dll>
#include <vcclr.h>
using namespace System;

class CppClass {
public:
 gcroot<String^> str;   // can use str as if it were String*
 CppClass() {}
};

int main() {
 CppClass c;
 c.str = gcnew String("hello");
 Console::WriteLine( c.str ); // no cast required
}s 

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值