您的位置:首页 > 其它

vc ++.net2005 managed unmanaged mixing code

2007-11-30 14:11 197 查看
编译时,不要预编译头,要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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: