by robert2098 » Fri Sep 24, 2010 3:31 pm
If you call a ?native? .dll from .NET then the code from the .dll is not executed by the .NET JIT, but run directly. Thus code inside the native .dll should run just as fast if it was executed from a non .NET application. Only marshaling the data between .NET and the native .dll costs more time since data has to be converted. So keep the communication between .NET and native .dll at a minimum.
Some reasons why C++ code in a dll runs faster than .NET code is that C++ code runs without bounds checking and other safety checks. And also explicit memory management (new/delete) is faster than a garbage collector.
Since code in the native .dll runs outside the .NET framework, its execution can not be controlled/checked by .NET and might be a security risk when the .dll contains misbehaving code. Therefore, when your .NET program accesses a native .dll, your code is also marked as unsafe and can?t be executed anymore if the .NET program is started with lower permission (e.g. when downloaded and run from the internet).
Robert