Skip to main content

How to Resolve Error "CS0601: The DllImport attribute must be specified on a method marked 'static' and 'extern'" in C#

The Compiler Error CS0601 is an interoperability configuration error. The message reads: "The DllImport attribute must be specified on a method marked 'static' and 'extern'".

This error occurs when you use the [DllImport] attribute to call a function from an unmanaged DLL (Platform Invocation or P/Invoke) but fail to declare the method signature correctly. Because the implementation of the function exists outside the .NET runtime (in a native DLL like user32.dll or a C++ library), the C# compiler requires you to mark the method as external (extern) and static (static).

This guide explains the requirements for P/Invoke declarations and how to fix this syntax error.

Understanding P/Invoke Requirements​

P/Invoke (Platform Invocation Services) allows managed code to call unmanaged functions implemented in dynamic link libraries (DLLs).

To define a P/Invoke method, you provide a "stub" or a "prototype" in C#.

  1. [DllImport]: Tells the runtime which file contains the code.
  2. extern: Tells the compiler "There is no body { } for this method here; look for it externally."
  3. static: Native DLL exports are global functions, not instance methods of a C# class. Therefore, the wrapper must be static.

If you omit either static or extern, the compiler raises CS0601 because the definition is incomplete.

Scenario: Missing Keywords​

This error typically happens when a developer copies a C++ function signature and tries to paste it into C# without adjusting the modifiers, or simply forgets one of the required keywords.

Example of error: trying to define a wrapper for the Windows MessageBox function.

using System.Runtime.InteropServices;

public class NativeMethods
{
// ⛔️ Error CS0601: The DllImport attribute must be specified on a method
// marked 'static' and 'extern'.
// 1. Missing 'static' implies it requires an instance (incorrect for DLL exports).
// 2. Missing 'extern' implies it should have a body { } (incorrect for imports).
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public int MessageBox(System.IntPtr hWnd, string text, string caption, uint type);
}

Solution: Add 'static' and 'extern'​

To fix the error, simply add both static and extern to the method signature. The order of these keywords does not strictly matter, though static extern is the convention.

Solution:

using System.Runtime.InteropServices;

public class NativeMethods
{
// ✅ Correct: The method is static (global) and extern (implemented elsewhere).
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(System.IntPtr hWnd, string text, string caption, uint type);
}

public class Program
{
static void Main()
{
// Usage
NativeMethods.MessageBox(System.IntPtr.Zero, "Hello World", "P/Invoke", 0);
}
}

Explanation of the Keywords​

  • [DllImport("...")]: Identifies the external DLL file.
  • static: Indicates the method belongs to the type itself, mimicking the "flat" global function style of C libraries.
  • extern: Indicates that the method body is implemented externally. This allows you to end the declaration with a semicolon ; instead of a code block { ... }.
note

Why not instance methods? Unmanaged DLLs (like Win32 API) export functions globally. They do not know about C# objects or the this pointer. Therefore, you cannot map a DLL function directly to a non-static C# method.

Conclusion​

CS0601 ensures that external function wrappers are defined correctly.

  1. Check Attributes: If you use [DllImport], you are doing P/Invoke.
  2. Add Modifiers: Ensure the method signature includes both static and extern.
  3. Remove Body: Ensure the method ends with a semicolon ;, not curly braces.