How do you call unmanaged code in C#?

๐Ÿ’ก Concept: Calling Unmanaged Code from C#

Unmanaged code refers to native code executed outside the .NET runtime. Calling it from C# is done via Platform Invocation Services (P/Invoke).

๐Ÿ“˜ Quick Intro

P/Invoke allows managed code to call functions exported from unmanaged DLLs.

๐Ÿง  Analogy

Like placing an international phone call through a gateway that translates protocols.

๐Ÿ”ง Technical Explanation

  • Use DllImport attribute to specify DLL and function signatures.
  • Handle data marshaling between managed and unmanaged memory.
  • Supports calling C, C++, and Win32 APIs.
  • Careful with memory management to avoid leaks and crashes.
  • Example: calling MessageBox from user32.dll.

๐ŸŽฏ Use Cases

  • โœ… Accessing legacy native libraries.
  • โœ… Calling OS-level APIs.
  • โœ… Performance critical code sections.
  • โœ… Integrating with hardware or external devices.

๐Ÿ’ป Code Example


using System.Runtime.InteropServices;

class Program {
    [DllImport(""user32.dll"", CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hWnd, string text, string caption, int options);

    static void Main() {
        MessageBox(IntPtr.Zero, ""Hello from unmanaged code!"", ""P/Invoke Example"", 0);
    }
}

โ“ Interview Q&A

Q1: What is unmanaged code?
A: Code not managed by the CLR.

Q2: What is P/Invoke?
A: Platform Invocation to call unmanaged DLL functions.

Q3: How do you specify the DLL?
A: Using DllImport attribute.

Q4: What are marshaling concerns?
A: Converting data between managed and unmanaged memory.

Q5: Can you call C++ code?
A: Yes, if exposed via C-style DLL exports.

Q6: Is memory management automatic?
A: No, must be handled carefully.

Q7: Can P/Invoke cause crashes?
A: Yes, if used incorrectly.

Q8: Is P/Invoke supported on all .NET platforms?
A: Mostly, but some restrictions exist.

Q9: Alternatives to P/Invoke?
A: COM Interop, C++/CLI.

Q10: Why call unmanaged code?
A: For legacy or performance reasons.

๐Ÿ“ MCQs

Q1. What is unmanaged code?

  • Code outside CLR management
  • Managed code
  • C# code
  • Java code

Q2. What is P/Invoke?

  • Calling managed code
  • Calling unmanaged DLLs
  • Debugging
  • Profiling

Q3. How to specify DLL?

  • DllImport attribute
  • Marshal class
  • Namespace
  • Assembly info

Q4. What is marshaling?

  • Memory allocation
  • Data conversion
  • Thread management
  • Garbage collection

Q5. Can P/Invoke call C++?

  • No
  • Yes
  • Sometimes
  • Rarely

Q6. Is memory management automatic?

  • Yes
  • No
  • Sometimes
  • Rarely

Q7. Can P/Invoke cause crashes?

  • No
  • Yes
  • Maybe
  • Never

Q8. Is P/Invoke on all .NET platforms?

  • No
  • Mostly
  • Yes
  • Sometimes

Q9. Alternatives to P/Invoke?

  • COM Interop
  • Reflection
  • Serialization
  • Attributes

Q10. Why call unmanaged code?

  • For fun
  • Legacy or performance
  • Testing
  • Debugging

๐Ÿ’ก Bonus Insight

Calling unmanaged code bridges legacy native libraries with modern .NET applications, enabling powerful integrations.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: