Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   using dll files and converting from C# to C (http://www.go4expert.com/forums/using-dll-files-converting-c-sharp-c-t3576/)

codoll53 23Mar2007 10:36

using dll files and converting from C# to C
 
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).

shabbir 23Mar2007 10:58

Re: using dll files and converting from C# to C
 
Do you mean C under windows or Linux.


All times are GMT +5.5. The time now is 03:29.