ImportDLL Attribute

I would like to replicate this code snippet from https://stackoverflow.com/a/2416762 to create a virtual mouse and keyboard to control an app:

public class Form1 : Form
{
   [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
   public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
   //Mouse actions
   private const int MOUSEEVENTF_LEFTDOWN = 0x02;
   private const int MOUSEEVENTF_LEFTUP = 0x04;
   private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
   private const int MOUSEEVENTF_RIGHTUP = 0x10;

   public Form1()
   {
   }

   public void DoMouseClick()
   {
      //Call the imported function with the cursor's current position
      uint X = (uint)Cursor.Position.X;
      uint Y = (uint)Cursor.Position.Y;
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
   }

   //...other code needed for the application
}

How should these attributes be applied?

hellohello,

attributes are not really supported atm, i’d either wrap this snippet in a dll or wrap an existing nugget such as this one.

1 Like

Didn’t see that one, thanks. I understand there has been some Attribute support in the pipeline, so I won’t call this a solution in case someone is searching for attributes support more broadly but this is a good fallback.

Additionally, I would have liked it if this nuget had worked which wraps Pinvoked Win32 dlls for usable c# but I got no output from it.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.