How do I mix C# and C++ code in a single assembly?
If your C++ code is not compiled with /clr:safe (i.e. it is compiled with /clr or /clr:pure), do the following:
1) compile your C++ code into .obj files
2) compile your C# code into a .netmodule, using /AddModule to reference the C++ .obj files
3) link the C# netmodule directly with the C++ object files using the C++ linker to create a mixed language assembly
If your C++ code is compiled with /clr:safe, build your C++ code as a .netmodule. You can use it just like you would use a .netmodule from any other language.
This applies to .Net framework beta2+.
MSDN新推出了一个系列
http://msdn.microsoft.com/netframework/programming/classlibraries/
|
|
The Designing .NET Class Libraries series presents design guidelines for developing classes and components that extend the .NET Framework. The goal of the Designing .NET Class Libraries series is to encourage consistency and predictability in public APIs while enabling Web and cross-language integration. The guidelines presented in Designing .NET Class Libraries are intended to help class library designers understand the trade-offs between different solutions. There might be situations where good library design requires that you violate these design guidelines. Such cases should be rare, however it is important that you provide a solid justification for your decision.
|
每一个讲座都带有一段录象. 这些录象其实是微软内部的课程.
如果你的工作涉及设计类库的话, 这可能是你最好的资源了. |
|
如果你关心VC 2005的话,你应该阅读Stan Lippman的blog.
http://blogs.msdn.com/slippman
Stan Lippman写的<<C++ Primer>>,可能是世界上最流行的C++入门书了.
Herb Sutter在他的blog上讨论了C++/CLI的deterministic destruction.
http://pluralsight.com/blogs/hsutter/archive/2004/11/23/3666.aspx
Herb Sutter是C++ Standard Committee 的主席.他现在在微软主持VC 2005(即C++/CLI)的设计.
每个人都说Ngen可以加快程序的启动速度.可到底有多大作用呢?
Rico 给了个例子:
Cold Startup Performance
他的数据表明,NGEN最大的作用还是在warm startup的时候.如果你有多个程序运行的话,基本上就是warm startup了.
有朋友问为什么.Net Framework Service Pack没有用publisher policy.
下面是我的一个回答:
http://blogs.msdn.com/junfeng/archive/2004/10/11/240822.aspx
There are several reasons:
1. Publisher Policy can be opted out by applications.
In App.Config you can say publisherPolicy="No", effectively ignore any publisher policy. Majority of the fixes in SP are security fixes. We don't want people to opt out security fixes.
In retrospection, the reason we introduce publisherPolicy="No" is for app compat. Since then the thinking has been shifted to security first.
2. Framework assemblies are tested in a group. And we want to ship them in a group.
This is the same reason we introduce framework unification policy. We tested all the framework assemblies in redist SP together. And we ship them together. Say we shipped publisher policy. If you intend to opt out publisher policy, you end up with a configuration we did not test, and we do not want to support. Of course if you do not intend to opt out publisher policy, then it makes no difference for you whether we use publisher policy or not.
The bottom line is the current policy system is not optimal for .Net framework Servicing, and we intend to change that in future releases.
C5 http://www.itu.dk/research/c5/
C5 is a library of generic collection classes for the upcoming version 2.0 of the .Net platform. C5 extends the standard .Net System.Collections.Generic namespace with tree data structures, heap based priority queues and hash indexed array lists and linked lists, making it more comprehensive than most collection class libraries on similar platforms. Unlike many other collection class libraries, C5 is designed with a strict policy of supporting "code to interface not implementation".
之所以看到那么多的Collection,我想还是因为.Net framework现在没有提供完整的数据结构.我的intern就向我抱怨说.Net framework 1.1里没有linkedList和Tree.
Jeffrey Richter和他的在Wintellect的朋友开始了一个Open Source的项目
PowerCollectiolns
http://www.wintellect.com/powercollections/
他们的目的是要实现更多的.Net Collection.
数据结构这种东西,作为一个社区项目可能更合理一些.
在我的英文blog上发表了一篇关于Domain Neutral Assemblies的文章.
http://blogs.msdn.com/junfeng/archive/2004/08/05/208375.aspx
很抱歉没有中文版.如果谁有兴趣帮忙的话不胜感激.
我在我的英文blog上发表了一篇关于MultiModule assemblies的文章 http://blogs.msdn.com/junfeng/archive/2004/07/15/183813.aspx.
比较有意义的是,在VC++ 2005里, link.exe可以把多个netmodule连接成一个assembly.这个东东很多人已经期待很久了.
不知道有多少人注意VC++ 2005。在我们这里,有很多人对VC++ 2005很兴奋。有些人认为,VC++ 2005会把很大一部分程序员从C#阵营转到VC里:)
Stephen Toub的这篇文章是一个很好的总结。
Write Faster Code with the Modern Language Features of Visual C++ 2005
http://msdn.microsoft.com/msdnmag/issues/04/05/visualc2005/default.aspx
以Hello, World结尾吧。
C:\HelloWorld>more hello.cpp
int main()
{
System::Console::WriteLine("Hello, World!");
return 0;
}
C:\HelloWorld>cl hello.cpp /clr
ure
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40607.16
for Microsoft (R) .NET Framework version 2.00.40607.16
Copyright
Microsoft Corporation. All rights reserved.
hello.cpp
Microsoft (R) Incremental Linker Version 8.00.40607.16
Copyright
Microsoft Corporation. All rights reserved.
/out:hello.exe
/clrimagetype
ure
hello.obj
C:\HelloWorld>hello
Hello, World!
Use the following app.config to enable server GC.
<configuration>
<runtime>
<gcServer enabled="true" />
</runtime>
</configuration>
Use System.Environment.IsServerGC to check if server GC is turned on or not.
Gotdotnet.com 上发表的break changes from version 1.1 to 2.0 上有一条:
Cache load failures in order to ensure that different app domains do not have different dependency loading success/failure characteristics in domain neutral sharing scenarios
http://www.gotdotnet.com/team/changeinfo/Backwards1.1to2.0/default.aspx#00000067
link里有说明这是怎么回事。简单的说,在同一个AppDomain里,如果 Assembly.Load/Assembly.LoadFrom()第一次失败了的话,以后同一个Assembly.Load()/Assembly.LoadFrom()会继续失败,并且会返回同一个exception,即使你在第一次和第二次之间把assembly拷到正确的地方。
举个例子。假设a.dll在c:\temp\a.dll。 现在当前AppDomain的AppBase是c:\myapp.
Assembly.Load(“A, version=0.0.0.0, culture=neutral, publickeytoken=0123456789abcde”
; // will fail with FileNotFoundException since a.dll is not in c:\myapp;
File.Copy(“c:\\temp\\a.dll”, “c:\\myapp\a.dll”
;
Assembly.Load(“A, version=0.0.0.0, culture=neutral, publickeytoken=0123456789abcde”
; // this will succeed in 1.0/1.1, but will fail in 2.0, since in 2.0 the binding failure of the first bind is cached.
这个改变的原因是让assembly binding in one AppDomain有个一致的行为。第一次成功了,以后就会成功。第一次失败了,以后就会失败。
当然最根本的原因是为了共享。没有这个保证的话共享是不可能的。