C# FAQ: Can I directly call a native function exported from a DLL?

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

Can I directly call a native function exported from a DLL?

Yes. The following example uses the minimum requirements for declaring a C# method implemented in a native DLL. The method Test.MessageBoxT() is declared with static and external modifiers. And, it has the DllImport attribute which notifies the compiler that the implementation comes from the user32.dll under the default name of MessageBoxT.

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

There is more detail in the Platform Invoke tutorial.



Personal tools