[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Is this considered unmanaged code?

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
brettr
Asked:
brettr
3 Solutions
 
oobaylyCommented:
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
 
brettrAuthor Commented:
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
 
OutOfTouchCommented:
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
brettrAuthor Commented:
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
 
burningmaceCommented:
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
 
OutOfTouchCommented:
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now