在非托管类中混合Mananged和Unmanaged [英] Mixing Mananged and Unmanaged in an Unmanaged class

查看:61
本文介绍了在非托管类中混合Mananged和Unmanaged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以编写课程,取决于课堂上的功能顺序。


我的问题是(见下面的例子):

1)类''WillCompile''将编译和执行,或者是
2)是一个错误,还是类'WillNotCompile''不能编译的错误? br />

#include" stdafx.h"

#include< iostream>

#include< memory>

使用命名空间std;


#using< mscorlib.dll>


使用命名空间系统;


#pragma unmanaged

类WillCompile {

public:

static void Umg(){

}

#pragma managed

static void Mng(String * str){

Console :: WriteLine(str);

}

};


#pragma unmanaged

class WillNotCompile {

public:

#pragma managed

static void Mng(String * str){

Console :: WriteLine(str);

}

#pragma unmanaged

static void Umg(){

}

};


#pragma managed

int _tmain()

{

WillCompile :: Mng(S" This works。 );


Console :: WriteLine(S"按Enter键继续。);

Console :: ReadLine();

返回0;

}

Whether I can compile a class or not, depends on the order of functions in my class.

My question is (see example below):
1) Is it a bug that the class ''WillCompile'' will compile and execute, or
2) or is it a bug that the class ''WillNotCompile'' will not compile?.

#include "stdafx.h"
#include <iostream>
#include <memory>
using namespace std;

#using <mscorlib.dll>

using namespace System;

#pragma unmanaged
class WillCompile {
public:
static void Umg() {
}
#pragma managed
static void Mng( String* str ) {
Console::WriteLine( str );
}
};

#pragma unmanaged
class WillNotCompile {
public:
#pragma managed
static void Mng( String* str ) {
Console::WriteLine( str );
}
#pragma unmanaged
static void Umg() {
}
};

#pragma managed
int _tmain()
{
WillCompile::Mng( S"This works." );

Console::WriteLine(S"Press Enter to continue.");
Console::ReadLine();
return 0;
}

推荐答案

Jon Jon,

目前我正在寻找一些资源来调查此问题。我们将在这里回复更多信息



如果您有任何疑问,请随时在此发布。

感谢您的理解!


祝你好运,

Gary Chang

Microsoft在线合作伙伴支持/>

安全! - www.microsoft.com/security

此帖子原样是按原样提供的。没有保证,也没有赋予任何权利。

--------------------

Hi Jon,

Currently I am looking for some resource to investigate this issue. We will
reply here with more information as soon as possible.
If you have any more concerns on it, please feel free to post here.
Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------


你好Jon
Hi Jon
1)该类''WillCompile''将编译并执行,
或2)或者它是一个错误类''WillNotCompile''无法编译的错误?。
1) Is it a bug that the class ''WillCompile'' will compile and execute, or 2) or is it a bug that the class ''WillNotCompile'' will not compile?.




我们的Visual C ++团队成员Ronald确认这两个场景都是

设计,简单的规则是:任何触及托管类型的代码必须是编译为MSIL的
,任何无法编译的代码都可以编译。


你对这个问题有什么特别的期望或关注,如果是的话,

请随时回复此信息。

谢谢!


祝你好运,

Gary Chang

微软在线合作伙伴支持


安全! - www.microsoft.com/security

此帖子原样是按原样提供的。没有保证,也没有赋予任何权利。

--------------------



Our Visual C++ team member Ronald confirmed these 2 scenarios are both as
designed, simply the rule is: any code that touches managed types must be
compiled as MSIL, any code that does not can be compiled either way.

Do you have any particular expectation or concern on this problem, if so,
please feel free to reply this message.
Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------


在我的测试中,似乎是否管理了类是在定义的最后确定的。这似乎至少有点不清楚


1)WillNotCompile2就像WillCompile一样,除了它最后还有一个额外的#pragma unmanaged。这似乎使整个类无法管理,这会产生更复杂的错误


2)WillCompile2就像WillNotCompile一样,除了它在最后管理了额外的#pragma。这似乎使整个班级得到了管理,这避免了更复杂的错误


3)改变功能顺序似乎没有任何影响,我唯一能看到影响是否编译是否是类定义中的最后一个#pragma语句


这就是我从测试中确定的,我可能是错的。它上面的MSDN页面只提到#pragma会影响函数的编译方式。它似乎正在影响课程的编写方式


#pragma unmanage

class WillNotCompile2

public

static void Umg()


#pragma manage

static void Mng(String * str)

控制台: :WriteLine(str)


#pragma unmanage

}


#pragma unmanage

class WillCompile2

public

#pragma manage

static void Mng(String * str)

控制台: :WriteLine(str)


#pragma unmanage

static void Umg()


#pragma manage

}
http://msdn.microsoft.com/library/en...dUnmanaged.asp
From my testing, it appears that whether the class is managed is determined at the end of the definition. This seems at least a little bit unclear

1) WillNotCompile2 is just like WillCompile except it has an extra #pragma unmanaged at the end. This appears to make the whole class unmanaged, which creates the compilier error

2) WillCompile2 is just like WillNotCompile except it has an extra #pragma managed at the end. This appears to make the whole class managed, which avoids the compilier error

3) Changing the function order doesn''t seem to have any affect, the only thing I can see as affecting whether it compiles or not is what the last #pragma statement within the class definintion is

This is just what I''ve determined from testing, I could be wrong. The MSDN page on it only mentions that #pragma affects how functions are compilied. It appears that it is affecting how classes are compiled as well

#pragma unmanage
class WillNotCompile2
public
static void Umg()

#pragma manage
static void Mng( String* str )
Console::WriteLine( str )

#pragma unmanage
}

#pragma unmanage
class WillCompile2
public
#pragma manage
static void Mng( String* str )
Console::WriteLine( str )

#pragma unmanage
static void Umg()

#pragma manage
}
http://msdn.microsoft.com/library/en...dUnmanaged.asp


这篇关于在非托管类中混合Mananged和Unmanaged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆