15

I'm working on an Windows Form Application in C#, Framework 4 (32 bit).

I have a list that holds coords of the mouse, and I can capture them. So far so good.

But at some point, I want to go to those coords and left mouse click on it.

This is how it looks like right now:

for (int i = 0; i < coordsX.Count; i++)
{
    Cursor.Position = new Point(coordsX[i], coordsY[i]);
    Application.DoEvents();
    Clicking.SendClick();
}

And the Clicking class:

class Clicking
    {
        private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
        private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
        private static extern void mouse_event(
               UInt32 dwFlags, // motion and click options
               UInt32 dx, // horizontal position or change
               UInt32 dy, // vertical position or change
               UInt32 dwData, // wheel movement
               IntPtr dwExtraInfo // application-defined information
        );

        // public static void SendClick(Point location)
        public static void SendClick()
        {
            // Cursor.Position = location;
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
        }
    }

But I'm getting this error:

Could not load type 'program.Clicking' from assembly 'program, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'mouse_event' has no implementation (no RVA).

And i realy don't understand what the problem is... Do you guys know what the problem is? or do you know an better way to do what i'm trying to do?

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Mathlight
  • 6,436
  • 17
  • 62
  • 107

2 Answers2

12

Have you included the following line?

[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
   UIntPtr dwExtraInfo);

This will import the function mouse_event from the user32 dll, which is what you are trying to use within your program. Currently your program does not know about this method within the DLL untill you specify wher it comes from.

The website PInvoke.net user32 Mouse Event is quite handy for the basics on this sort of thing.

The answer to Directing mouse events [DllImport(“user32.dll”)] click, double click will be of great help to your understanding as well.

The flags are what commands you want to send into the mouse_input function, in that example you can see that he is sending both mouse down and mouse up in the same line, this is fine because the mouse_event function will split those flags up and execute them consecutively.


Also note that this method has been superseded by the SendInput command, a good example of SendInput and SetMousePos can be found At this Blog

Community
  • 1
  • 1
Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • I inserted that line into the clicking class, but still the same error – Mathlight Nov 22 '12 at 22:42
  • Did you delete your decleration of the `mouse_event`? make sure you do that and replace it with this. – Serdalis Nov 22 '12 at 22:45
  • And how would that sendinput work particular to my question? because i'm verry new to this, and i don't know if i need all the code from the link... – Mathlight Nov 22 '12 at 22:45
  • `SendInput` is a strange beast, the best thing you can do is experiment, it's been a while since I did this and I don't know what problem you are trying to solve, but my advice is include all the code and delete stuff till it doesn't work. Thats what I usually do :P, That said, I don't think anyone would blame you for continuing to use `mouse_input`, since it is far simpler if you are just a beginner. – Serdalis Nov 22 '12 at 22:48
  • I'm not afraid of trying. And the thing that i'm trying to do is just to click on certain points on the screen... That's all. And i want to use the best method for that... And if that is SendInput, then i'm intrested in how to use that – Mathlight Nov 22 '12 at 22:51
  • What is supposed to be the variable dwFlags? what type of variable / what should it hold... Sorry for all those question btw – Mathlight Nov 22 '12 at 22:54
  • 1
    dwFlags is the type of command you are sending through, people define it as all sorts of things but i feel `uint` is the most accurate since it is an `unsigned integer`, there are no negative flags. I edited my answer with a bit more information. – Serdalis Nov 22 '12 at 23:02
  • Thank you so very very very much. After changing the mousevent variables to static uint and all the above things, it workd like a charm... Thank you very, very much – Mathlight Nov 22 '12 at 23:10
2

I guess you are missing the following line

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138
  • that trows the error: Attribute 'DllImport' is not valid on this declaration type. It is only valid on 'method' declarations. – Mathlight Nov 22 '12 at 22:48