The [DllImport()] attribute is used in C# to import unmanaged functions from DLLs (Dynamic Link Libraries) into your managed code. Here's an example of how to use the [DllImport()] attribute:
using System; using System.Runtime.InteropServices; class Program { [DllImport("User32.dll")] private static extern int MessageBox(IntPtr hWnd, string text, string caption, int options); static void Main(string[] args) { // Display a message box using the MessageBox function from User32.dll MessageBox(IntPtr.Zero, "Hello, World!", "Greetings", 0); } } In this example, the [DllImport("User32.dll")] attribute is used to import the MessageBox() function from the User32.dll library. The function is declared as private static extern and its signature matches the definition of the unmanaged function.
The MessageBox() function is then called from the Main() method of the program to display a message box with the text "Hello, World!" and the caption "Greetings".
Note that you may need to adjust the name of the DLL and the function signature to match the specific DLL and function you are importing. Additionally, you should ensure that the DLL is available on the target system and that you have the appropriate permissions to access it.
"C# DllImport example"
DllImport in C# to call functions from native DLLs, providing a fundamental example.using System.Runtime.InteropServices; public class MyClass { [DllImport("user32.dll")] public static extern int MessageBox(int hWnd, string text, string caption, uint type); public void ShowMessageBox() { MessageBox(0, "Hello, DllImport!", "MessageBox Title", 1); } } "C# DllImport with CallingConvention"
DllImport with different calling conventions in C#, such as StdCall or Cdecl, to match the calling convention used by the native DLL.using System.Runtime.InteropServices; public class MyClass { [DllImport("MyNativeLibrary.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void MyFunction(); } "C# DllImport with EntryPoint"
EntryPoint property with DllImport to specify the exact name of the function in the native DLL.using System.Runtime.InteropServices; public class MyClass { [DllImport("MyNativeLibrary.dll", EntryPoint = "MyFunction")] public static extern void CustomFunctionName(); } "C# DllImport with CharSet"
CharSet property in DllImport to specify the character set used by the native DLL (e.g., Unicode or ANSI).using System.Runtime.InteropServices; public class MyClass { [DllImport("MyNativeLibrary.dll", CharSet = CharSet.Unicode)] public static extern void MyUnicodeFunction(); } "C# DllImport for 64-bit and 32-bit"
DllImport in a way that works for both 64-bit and 32-bit platforms by specifying the CallingConvention and using conditional compilation.using System.Runtime.InteropServices; public class MyClass { [DllImport("MyNativeLibrary.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "MyFunction")] public static extern void MyFunction(); } "C# DllImport with SetLastError"
SetLastError property with DllImport to preserve the last error code from the native DLL.using System.Runtime.InteropServices; public class MyClass { [DllImport("MyNativeLibrary.dll", SetLastError = true)] public static extern int MyFunction(); } "C# DllImport with CallingConvention and StructLayout"
DllImport with CallingConvention and StructLayout to ensure proper memory layout when working with structures in native DLLs.using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential)] public struct MyStruct { // Structure fields } public class MyClass { [DllImport("MyNativeLibrary.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void MyFunction(ref MyStruct myStruct); } "C# DllImport and Marshaling Arrays"
DllImport for functions that expect arrays as parameters by applying proper marshaling techniques.using System.Runtime.InteropServices; public class MyClass { [DllImport("MyNativeLibrary.dll")] public static extern void ProcessIntArray([MarshalAs(UnmanagedType.LPArray)] int[] array, int length); } "C# DllImport with CallingConvention and EntryPoint for x64 and x86"
DllImport with CallingConvention and EntryPoint for both x64 and x86 architectures, allowing compatibility with different platforms.using System.Runtime.InteropServices; public class MyClass { [DllImport("MyNativeLibrary.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "MyFunction_x86", EntryPoint = "MyFunction_x64")] public static extern void MyFunction(); } "C# DllImport with LoadLibrary and FreeLibrary"
LoadLibrary and FreeLibrary with DllImport to dynamically load and unload native DLLs at runtime.using System; using System.Runtime.InteropServices; public class MyClass { [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr LoadLibrary(string dllToLoad); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool FreeLibrary(IntPtr hModule); public delegate void MyFunctionDelegate(); public static MyFunctionDelegate MyFunction; static MyClass() { IntPtr moduleHandle = LoadLibrary("MyNativeLibrary.dll"); if (moduleHandle == IntPtr.Zero) { // Handle error } try { MyFunction = (MyFunctionDelegate)Marshal.GetDelegateForFunctionPointer( GetProcAddress(moduleHandle, "MyFunction"), typeof(MyFunctionDelegate) ); } finally { FreeLibrary(moduleHandle); } } } uivideoeditorcontroller modalviewcontroller text-parsing cryptographic-hash-function redux-thunk call servlet-3.0 cjk fasta meteor