using dll files and converting from C# to C

Discussion in 'C#' started by codoll53, Mar 23, 2007.

  1. codoll53

    codoll53 New Member

    Joined:
    Feb 23, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    I have found the following article which is basically trying to do what I want, but I need it in C instead of C# and I have absolutely no experience in C#. Also how do I use the Dll import files, i am not really sure how to handle this. Thanks!

    Ok, first I'll describe how the program works:

    It moves the mouse to the top-left corner of the screen (0,0) and clicks the mouse (causing any other program to lose focus).

    The following API moves the mouse:

    [DllImport("user32")]
    public static extern int SetCursorPos(int x, int y);

    This is fairly easy to understand, it gets the coordinates and moves the mouse to that position, but how do I click then?

    [DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    Ok then, there's an API called mouse_event. you're probably wondering how it's used, so here's an example:

    First, you make an enumeration (you might as well use the integers themselves, i just find this way to be more organized.

    Code:
    public enum MouseEventType : int
    {
      LeftDown = 0x02,
      LeftUp = 0x04,
      RightDown = 0x08,
      RightUp = 0x10
    }
    Now, what exactly is a mouse click? it's a click down and then up. only sending LeftDown will be like a mouse hold, and only sending LeftUp...will just be weird, that's why in order to emulate a mouse click, you have to send both:

    mouse_event((int)MouseEventType.LeftDown, Cursor.Position.X, Cursor.Position.Y, 0, 0);
    mouse_event((int)MouseEventType.LeftUp, Cursor.Position.X, Cursor.Position.Y, 0, 0);<code>

    Woohoo! we got ourselves a Mouseclick!

    The full code:
    Code:
    [DllImport("user32.dll")]
    public static extern void mouse_event(MouseEventType dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    
    [DllImport("user32")]
    public static extern int SetCursorPos(int x, int y);
    
    public enum MouseEventType : int
    {
      LeftDown = 0x02,
      LeftUp = 0x04,
      RightDown = 0x08,
      RightUp = 0x10
    } 
    
    private void button1_Click(object sender, EventArgs e)
    { 
      SetCursorPos(0, 0);
      mouse_event(MouseEventType.LeftDown, Cursor.Position.X, Cursor.Position.Y, 0, 0);
      mouse_event(MouseEventType.LeftUp, Cursor.Position.X, Cursor.Position.Y, 0, 0);
    }
    By putting the click in a function and calling it with a timer (with a 1 millisecond interval, for example), you'll be making the exact same virus (but viruses are bad...so don't).
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,345
    Likes Received:
    382
    Trophy Points:
    83
    Do you mean C under windows or Linux.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice