1
votes

So I am working on a c# Windows Form application and part of my code uses some methods from user32.dll. Listed them below.

  • public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
  • public static extern bool ReleaseCapture();
  • public static extern IntPtr GetForegroundWindow();
  • public static extern int GetWindowText(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
  • public static extern uint GetWindowThreadProcessId
  • public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii)

At first I had them in the file with all the main forms code as part of the public partial class MainForm and this all works. Then when I ran code analysis in Visual Studio Community 2015 it complained that "because they are P/Invoke methods they should be defined in a class named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods.

So being the good coder I am who always obeys the whims of his IDE I promptly make a new class file and run into the first issue.

Would the class containing these functions be unsafe (is it also unmanaged)? Which name should I use and should the class be declared as internal static with the [DebuggerNonUserCode] attribute?

As I did more reading to try to figure this out I kept coming across references to making a wrapper class for the code so I started looking into that. That yielded tons of stuff about security and wrappers for c++ or c, prototypes, and tons of other information that doesn't seem helpful and got myself completely lost.

So I guess what I want to know is two things. 1st, would I need to (or would it be best practice to do so) make a wrapper class to use these methods, and if so, how would I go about doing it?

2nd, If I make the class Unsafe/SafeNativeMethods should I call it safe or unsafe? Should all the methods be left public? Or do should have the methods declared private and write some getters/setters like:

public IntPtr getGetForegroundWindow()
{
    return GetForegroundWindow()
}

or

public setSendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr, lParam)
{
    SendMessage(hWnd, Msg, wParam, lParam);
}

Or do I need to use delegates or something instead of setters and getters?

Sorry about the question being a bit of a brain dump. Every time I think I found an answer I just end up with more questions and all the talk of safe/unsafe/security has me worried. I don't mind google searches and reading up on topics but the reading list just to figure out what to read is getting huge so figured I would stop and ask here to see if I am even asking the right question. Thanks in advance.

1

1 Answers

1
votes

I would create a static class.

namespace NativeMethods
{
    public static class User32
    {
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref RECT lParam);
    }
}

Only create a wrapper when it does something, like holding a state of parameter conversion/checking.


Cases when you want to wrap pInvokes

An example for wrapping PInvoke methods could be when the native method allocates memory and returns a handle. This case you'll need to create a wrapper that keeps track of a handle. It should implement the IDisposable for releasing the unmanaged resources.

for (pseudo) example: (you should check the dispose pattern)

public class MyWrapper : IDisposable
{
    [DllImport("MyLibrary.dll")]
    private static extern IntPtr DoAllocSomeMemory(int size);

    [DllImport("MyLibrary.dll")]
    private static extern void ReleaseMe(IntPtr handle);


    private IntPtr _handle;

    public MyWrapper()
    {
        _handle = DoAllocSomeMemory(8000);
    }

    public void Dispose()
    {
        ReleaseMe(_handle);
    }    
}