Solved

Is this considered unmanaged code?

Posted on 2009-04-08
6
293 Views
Last Modified: 2012-05-06
I'm making use of Microsoft.Interop.Security.AzRoles.  I may catch a COMException in some locations.  Would this make some of my code considered as unmanaged?  In other words, does accessing COM related assemblies mean you are doing unmanaged code?  Seems you still get garbage collection.

Would unmanaged code be only anything using the DLLImport or unsafe blocks?

0
Comment
Question by:brettr
6 Comments
 
LVL 15

Assisted Solution

by:oobayly
oobayly earned 100 total points
ID: 24099639
According to this article, yes

"Code executing under the control of the runtime is called managed code. Conversely, code that runs outside the runtime is called unmanaged code. COM components, ActiveX interfaces, and Win32 API functions are examples of unmanaged code."
0
 

Author Comment

by:brettr
ID: 24099678
Don't understand.  My calls are in managed code.  The assemblies I'm calling are managed but have COM references somewhere I'm sure.  So what does that make my code?
0
 
LVL 3

Expert Comment

by:OutOfTouch
ID: 24099858
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:brettr
ID: 24099993
Thanks.  I was looking for someone to answer my specific questions in their own words rather than providing links.

I see the azman DLL as about unmanaged as calls into System.Windows.Forms namespace.   Under the hood, it calls GDI+ but we don't say our code is unmanaged.  How close to the unmanaged implementation do you have to be before it is unmanaged?  The way I see it, if everything you are writing can be cleaned up by the runtime, it is managed code. Not exactly sure where that puts DLLImport. You might reference something like the azman API but those implicit COM object instances are disposed of automatically by the runtime because of how you referenced them.  Disagreements?
0
 
LVL 5

Accepted Solution

by:
burningmace earned 250 total points
ID: 24100343
I think you're misinterpreting the way .NET handles managed and unmanaged code. Say I were to declare FindWindow using DLLImport in a C# application, then call it with a pair of string variables. The string variables are managed. The FindWindow method (see code sample) itself is actually managed. When I call the FindWindow method, the [DLLImport("user32.dll"]) part tells InteropServices to translate the contents of the method parameters to unmanaged types and call the API. The core .NET framework itself is unmanaged because it is not written in VB.NET/C#/J#, but rather an unmanaged language such as C++.

A simple analogy is PHP. In PHP you cannot call GetFileSize in kernel32.dll, but you can get the PHP interpreter to call it for you using the filesize function. When you call it, PHP translates the file name string you gave it into a char* (or similar) and calls the appropriate APIs (OpenFile, GetFileSize, etc) to find the information it needs. It then translates the result into a format that can be used as a variable within the PHP script.
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Open in new window

0
 
LVL 3

Assisted Solution

by:OutOfTouch
OutOfTouch earned 150 total points
ID: 24103194
Okay in my own words: Your code is managed code but your code is using an Interop assembly to call into unmangaged COM code. The interop assembly itself is managed code. The underlying COM api's being called are not managed code.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 47
Unlocking a column in excel using C# 17 28
Visual Studio 2015 auto inserted code 12 71
asp.net repeater 2 15
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question