简体   繁体   中英

Is my code managed or unmanaged?

I am developing a .NET library which is so far 100% managed. Now I need to use some win32 calls.

All I need to know is when is a .NET library called unmanaged? when the assembly has unmanaged code or when the unmanaged code in the assembly gets executed?

只要您的代码在进行这些非托管的win32调用后正确清理,那么您的库将被管理。

You think you're writing 100% managed code, but actually if you delve into the bowels of the .NET framework, even that calls unmanaged code. Try using ILSpy or .NET Reflector to find DllImport or extern method calls. The assembly itself is managed because .NET is doing all the JITing, security, garbage collection ect. to the objects in the assembly. At the point that .NET calls an unmanaged function (Win32 etc), .NET no longer has control over the code, therefore this is unmanaged!

It is good practice to learn how to "manage" unmanaged code imports. You should wrap interoperable calls so that .NET can clean up as much as possible once the interoperable call is complete.

Take a look here for information on how Platform Invoking should be used:

http://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM