代码改变世界

如何使用C#操作WinAPI

2010-06-25 15:00  轩脉刃  阅读(4644)  评论(7编辑  收藏  举报

Windows API是对Windows操作系统的API函数,在C#中调用Windows API的实质是托管代码对非托管代码的调用。

主要使用的格式就是:

 

using System.Runtime.InteropServices;

namespace TestWinAPI1
{
class Program
{
static void Main(string[] args)
{
Beep(
100, 100);
}


[DllImport(
"kernel32", CharSet = CharSet.Ansi)]
public static extern bool Beep(int frequery, int duration);
}
}

 

其中的Beep就是Win API的调用,使用[DllImport("kernel32")]属性进行调用。

这个函数在MSDN中的原本定义是:

 

C++
BOOL WINAPI Beep(
__in DWORD dwFreq,
__in DWORD dwDuration
);

 

我们想要调用BeepAPI,就必须:

1.将DWORD对应为C#中的int,相应的参数个数和位置设置正确

2.调用的函数名和WinAPI中的函数名一致

这样,我们在C#中就可以使用Win API对Windows进行操作。

 

这里几个资源是使用WindowsAPI不可或缺的:

MSDN:http://msdn.microsoft.com/en-us/library/ee663300(VS.85).aspx

推荐的入门教程:http://www.docin.com/p-4510006.html

 

 

使用WINAPI的难点:

 

1.C++中的各个数据类型如何对应到C#中?

使用C#中的那个数据类型对应那个C++的数据类型没有唯一的规定,但是应该站在内存使用的角度,选择内存占用大小一致。

当C++中存在指针的时候,我们可以使用ref来传递指针

 

2.如果C++中定义了数据结构如何操作?

我们也应该在C#中定义与之存储结构一致的数据结构

 

以下是用WinAPI 模拟鼠标定位和单机左键的操作:

 

代码
namespace TestWinAPI1
{
public struct Point
{
int x;
int y;
}
class Program
{
static void Main(string[] args)
{
Point point
= new Point();
bool getResult = GetCursorPos(ref point);
int setRight = SetCursorPos(27, 881);
MouseClick(
"left");
}

[DllImport(
"user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(ref Point point);

[DllImport(
"user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetCursor();


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

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

[Flags]
public enum MouseEventFlags:uint
{
LEFTDOWN
= 0x00000002,
LEFTUP
= 0x00000004,
MIDDLEDOWN
= 0x00000020,
MIDDLEUP
= 0x00000040,
MOVE
= 0x00000001,
ABSOLUTE
= 0x00008000,
RIGHTDOWN
= 0x00000008,
RIGHTUP
= 0x00000010
}

/// <summary>
/// checks for the currently active window then simulates a mouseclick
/// </summary>
/// <param name="button">which button to press (left middle up)</param>
/// <param name="windowName">the window to send to</param>
public static void MouseClick(string button, string windowName)
{
if (WindowActive(windowName))
MouseClick(button);
}

/// <summary>
/// simulates a mouse click see http://pinvoke.net/default.aspx/user32/mouse_event.html?diff=y
/// </summary>
/// <param name="button">which button to press (left middle up)</param>
public static void MouseClick(string button)
{
switch (button)
{
case "left":
mouse_event((
uint)MouseEventFlags.LEFTDOWN|(uint)MouseEventFlags.LEFTUP, 0, 0, 0, 0);
break;
case "right":
mouse_event((
uint)MouseEventFlags.RIGHTDOWN, 0, 0, 0, 0);
mouse_event((
uint)MouseEventFlags.RIGHTUP, 0, 0, 0, 0);
break;
case "middle":
mouse_event((
uint)MouseEventFlags.MIDDLEDOWN, 0, 0, 0, 0);
mouse_event((
uint)MouseEventFlags.MIDDLEUP, 0, 0, 0, 0);
break;
}
}
}
}

 

简要描述:

使用了mouse_event,GetCursorPos,SetCursorPos三个API

mouse_event((uint)MouseEventFlags.LEFTDOWN|(uint)MouseEventFlags.LEFTUP, 0, 0, 0, 0);

代表了单击左键的动作

int setRight = SetCursorPos(27, 881); 中的27,881是屏幕上的绝对位置

 

 

PS:使用API可以做一些游戏的小外挂,比如模拟鼠标,键盘的操作...嘿嘿