Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put DllImport?

Tags:

c#

dllimport

static class Class    
{
    public static void methodRequiringStuffFromKernel32()
    {
        // code here...
    }
} 

Where do I put [DllImport("Kernel32.dll")] here?

like image 966
randall Z Avatar asked May 20 '11 19:05

randall Z


People also ask

What is use of DllImport?

DllImport attribute is used at run time to call a function exported in an external DLL implemented with unmanaged code that is executed outside the control of common language runtime (CLR).

What is extern C#?

The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code. In this case, the method must also be declared as static , as shown in the following example: C# Copy.


2 Answers

You put it on the method you're importing from Kernel32.dll.

For example,

static class Class    
{
    [DllImport("Kernel32.dll")]
    static extern Boolean Beep(UInt32 frequency, UInt32 duration);

    public static void methodRequiringStuffFromKernel32()
    {
        // code here...
        Beep(...);
    }
}

From @dtb: Note that the class should be named NativeMethods, SafeNativeMethods or UnsafeNativeMethods. See Naming Convention for Unmanaged Code Methods for more details.

CA1060: Move P/Invokes to NativeMethods class:

  • NativeMethods - This class does not suppress stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute must not be applied to this class.) This class is for methods that can be used anywhere because a stack walk will be performed.

  • SafeNativeMethods - This class suppresses stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute is applied to this class.) This class is for methods that are safe for anyone to call. Callers of these methods are not required to perform a full security review to make sure that the usage is secure because the methods are harmless for any caller.

  • UnsafeNativeMethods - This class suppresses stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute is applied to this class.) This class is for methods that are potentially dangerous. Any caller of these methods must perform a full security review to make sure that the usage is secure because no stack walk will be performed.

like image 78
Adam Lear Avatar answered Oct 13 '22 08:10

Adam Lear


This is an example of DllImport:

using System;
using System.Runtime.InteropServices;

class MsgBoxTest
{
  [DllImport("user32.dll")]
  static extern int MessageBox (IntPtr hWnd, string text, string caption,
                                int type);
  public static void Main()
  {
    MessageBox (IntPtr.Zero, "Please do not press this again.", "Attention", 0);
  }
}

I suggest you to study Platform Invoke Tutorial.

like image 24
Alireza Maddah Avatar answered Oct 13 '22 08:10

Alireza Maddah