请教 : as3 如何dll调用windows apii?

The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.trackbacks-0
用C#调用Windows API向指定窗口发送
一、调用Windows API。
 C#下调用Windows API方法如下:
 1、引入命名空间:using
System.Runtime.InteropS
 2、引用需要使用的方法,格式:[DllImport("DLL文件")]方法的声明;
 [DllImport("user32.dll")]private static extern bool
ShowWindow(IntPtr hWnd, int nCmdShow);
 [DllImport("user32.dll")]private
static extern bool SetForegroundWindow(IntPtr hWnd);
 [DllImport("user32.dll")]private static extern IntPtr
FindWindow(string lpClassName,string
lpWindowName);
 [DllImport("user32.dll")]private static extern int
SendMessage(IntPtr hWnd,int Msg,int wParam,int
 [DllImport("user32.dll")]private static extern bool
SetCursorPos(int X, int Y);
 [DllImport("user32.dll")]private static extern void
mouse_event(int dwFlags, int dx, int dy, int dwData, int
dwExtraInfo);
 [DllImport("user32.dll")]private static extern void
keybd_event(byte bVk, byte bScan, uint dwFlags, uint
dwExtraInfo);
  [DllImport("user32.dll")]private static extern bool
SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int
cx, int cy, uint Flags);
 //ShowWindow参数
 private const int SW_SHOWNORMAL = 1;
 private const int SW_RESTORE = 9;
 private const int SW_SHOWNOACTIVATE = 4;
 //SendMessage参数
const int WM_KEYDOWN = 0X100;
 private const int WM_KEYUP = 0X101;
 private const int WM_SYSCHAR = 0X106;
 private const int WM_SYSKEYUP = 0X105;
 private const int WM_SYSKEYDOWN = 0X104;
 private const int WM_CHAR = 0X102;
二、找到目标窗口
1)、根据窗口的标题得到句柄
 IntPtr myIntPtr = FindWindow(null,"窗口名");
//null为类名,可以用Spy++得到,也可以为空
 ShowWindow(myIntPtr, SW_RESTORE);
//将窗口还原
 SetForegroundWindow(myIntPtr);
//如果没有ShowWindow,此方法不能设置最小化的窗口
2)、遍历所有窗口得到句柄
定义委托方法CallBack,枚举窗口API(EnumWindows),得到窗口名API(GetWindowTextW)和得到窗口类名API(GetClassNameW)
 public delegate bool CallBack(int hwnd, int
 [DllImport("user32")]public static extern int EnumWindows(CallBack
x, int y);
  [DllImport("user32.dll")]private static extern int
GetWindowTextW(IntPtr hWnd,
[MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int
nMaxCount);
 [DllImport("user32.dll")]private static extern int
GetClassNameW(IntPtr hWnd,
[MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int
nMaxCount);
2 调用EnumWindows遍历窗口
 CallBack myCallBack = new CallBack(Recall);
 EnumWindows(myCallBack, 0);
3 回调方法Recall
 public bool Recall(int hwnd, int lParam)
  StringBuilder sb = new StringBuilder(256);
  IntPtr PW = new IntPtr(hwnd);
  GetWindowTextW(PW,sb,sb.Capacity); //得到窗口名并保存在strName中
  string strName = sb.ToString();
  GetClassNameW(PW,sb,sb.Capacity); //得到窗口类名并保存在strClass中
  string strClass = sb.ToString();
  if (strName.IndexOf("窗口名关键字") &= 0
&& strClass.IndexOf("类名关键字")
    //返回false中止EnumWindows遍历
    //返回true继续EnumWindows遍历
3)、打开窗口得到句柄
定义设置活动窗口API(SetActiveWindow),设置前台窗口API(SetForegroundWindow)
 [DllImport("user32.dll")]static extern IntPtr
SetActiveWindow(IntPtr hWnd);
 [DllImport("user32.dll")][return:
MarshalAs(UnmanagedType.Bool)]static extern bool
SetForegroundWindow(IntPtr hWnd);
2 打开窗口
 Process proc = Process.Start(@"目标程序路径");
 SetActiveWindow(proc.MainWindowHandle);
 SetForegroundWindow(proc.MainWindowHandle);
三、向指定的窗口输入数据
1 利用发送消息API(SendMessage)向窗口发送数据
 InputStr(myIntPtr, _GameID); //输入游戏ID
 SendMessage(myIntPtr, WM_SYSKEYDOWN, 0X09, 0); //输入TAB(0x09)
 SendMessage(myIntPtr, WM_SYSKEYUP, 0X09, 0);
 InputStr(myIntPtr, _GamePass); //输入游戏密码
 SendMessage(myIntPtr, WM_SYSKEYDOWN, 0X0D, 0);
//输入ENTER(0x0d)
 SendMessage(myIntPtr, WM_SYSKEYUP, 0X0D, 0);
 /// 发送一个字符串
 /// &/summary&
 /// &param
name="myIntPtr"&窗口句柄&/param&
 /// &param
name="Input"&字符串&/param&
 public void InputStr(IntPtr myIntPtr, string Input)
  byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input));
  for (int i = 0; i & ch.L i++)
   SendMessage(PW, WM_CHAR, ch,
利用鼠标和键盘模拟向窗口发送数据
 SetWindowPos(PW, (IntPtr)(-1), 0, 0, 0, 0, 0x0040 | 0x0001);
//设置窗口位置
 SetCursorPos(476, 177); //设置鼠标位置
 mouse_event(0x, 0, 0); //模拟鼠标按下操作
 mouse_event(0x, 0, 0); //模拟鼠标放开操作
 SendKeys.Send(_GameID);&&
//模拟键盘输入游戏ID
 SendKeys.Send("{TAB}"); //模拟键盘输入TAB
 SendKeys.Send(_GamePass); //模拟键盘输入游戏密码
 SendKeys.Send("{ENTER}"); //模拟键盘输入ENTER
另:上面还提到了keybd_event方法,用法和mouse_event方法类似,作用和SendKeys.Send一样。
阅读(...) 评论()flex能不能调用windows api
[问题点数:40分]
flex能不能调用windows api
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2013年3月 总版技术专家分月排行榜第二
2014年2月 Java大版内专家分月排行榜第一2013年8月 Java大版内专家分月排行榜第一2013年5月 Java大版内专家分月排行榜第一2013年4月 Java大版内专家分月排行榜第一2013年3月 Java大版内专家分月排行榜第一2013年2月 Java大版内专家分月排行榜第一
2013年10月 总版技术专家分月排行榜第三
2014年3月 Java大版内专家分月排行榜第一2014年1月 Java大版内专家分月排行榜第一2013年12月 Java大版内专家分月排行榜第一2013年11月 Java大版内专家分月排行榜第一2013年10月 Java大版内专家分月排行榜第一
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 c 调用windows api 的文章

 

随机推荐