MC++ tips

1. 同时使用C++操作符new和MC++操作符new

使用__gc和__nogc关键字:
例如,托管类M和本地类N
M* m = __gc new M;
N* n = __nogc new N;

2. 含有析构函数定义的托管派生类链接时错误:
error LNK2001: unresolved external symbol "void __cdecl __CxxCallUnwindDtor(void (__thiscall*)(void *),void *)" (?__CxxCallUnwindDtor@@$$J0YAXP6EXPAX@Z0@Z)

解决方案:
1、在项目中显式引用msvcrt.lib或者msvcrtd.lib,具体做法是在头文件中增加一行
#pragma comment( lib, "msvcrt" ) // 或msvcrtd

2、如果不需要exception unwinding,可以取消对C++ Exception的支持,具体做法(以vs2003为例):
项目属性 -> C/C++ -> Code Generation -> Enable C++ Exceptions
注:/EHsc选项同样可以解决STL等需要支持C++ Exceptions的库的连接错误问题

3. 没有显示引入dll造成的TypeLoadException
例如
// 在头文件中
public __gc class ImageProvider
{
    System::Drawing::Image __gc
* GetImage () return NULL; }
}
上述代码可以通过编译,但是可能会在运行时引发TypeLoadException异常。
解决方案是在头文件中使用#using指令引入System.Drawing.dll
#using <system.drawing.dll>



posted on 2007-05-11 15:34  omnislash  阅读(936)  评论(0编辑  收藏  举报