Santhanam
Santhanam

Reputation: 176

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?

Upvotes: 3

Views: 322

Answers (2)

Matthew Layton
Matthew Layton

Reputation: 42270

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

Upvotes: 2

Dave New
Dave New

Reputation: 40042

As long as your code properly cleans up after making these unmanaged win32 calls, then your library will be managed.

Upvotes: 2

Related Questions