---
title: "How do you directly call a native function exported from a DLL?"  
description: "How do you directly call a native function exported from a DLL?"  
author: "Varun Agrawal"  
published: 2011-03-18  
updated: 2020-09-22  
canonical: https://www.mindstick.com/interview/493/how-do-you-directly-call-a-native-function-exported-from-a-dll  
category: "assembly"  
tags: ["assembly"]  
reading_time: 2 minutes  

---

# How do you directly call a native function exported from a DLL?

Here’s a quick example of the DllImport attribute in action:

```
using System.Runtime.InteropServices; \class C{	[DllImport(\"user32.dll\")]	public static extern int MessageBoxA(int h, string m, string c, int type);	public static int Main()	{		return MessageBoxA(0, \"Hello World!\", \"Caption\", 0);	}}
```

This example shows the minimum requirements for declaring a C# method that is implemented in a native DLL. The method C.MessageBoxA() is declared with the static and external modifiers, and has the DllImport attribute, which tells the compiler that the implementation comes from the user32.dll, using the default name of MessageBoxA. For more information, look at the Platform Invoke tutorial in the documentation.

## Answers

### Answer by Varun Agrawal

Here’s a quick example of the DllImport attribute in action:

```
using System.Runtime.InteropServices; \class C{	[DllImport(\"user32.dll\")]	public static extern int MessageBoxA(int h, string m, string c, int type);	public static int Main()	{		return MessageBoxA(0, \"Hello World!\", \"Caption\", 0);	}}
```

This example shows the minimum requirements for declaring a C# method that is implemented in a native DLL. The method C.MessageBoxA() is declared with the static and external modifiers, and has the DllImport attribute, which tells the compiler that the implementation comes from the user32.dll, using the default name of MessageBoxA. For more information, look at the Platform Invoke tutorial in the documentation.


---

Original Source: https://www.mindstick.com/interview/493/how-do-you-directly-call-a-native-function-exported-from-a-dll

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
