您尚未登录,请登录后浏览更多内容! 登录 | 注册并了解

QQ登录

只需一步,快速开始

 找回密码
 注册并了解

QQ登录

只需一步,快速开始

查看: 3202|回复: 4
打印 上一主题 下一主题
收起左侧

c#外挂常用类

[复制链接]

点击这里给我发消息

QQ

跳转到指定楼层
楼主
发表于 2013-6-7 07:46:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Diagnostics;
  6. using System.Management;
  7. namespace sendkey
  8. {
  9.     public class key
  10.     {
  11.         const uint PROCESS_ALL_ACCESS = 0x001F0FFF;
  12.         const uint KEYEVENTF_EXTENDEDKEY = 0x1;
  13.         const uint KEYEVENTF_KEYUP = 0x2;
  14.         private readonly int MOUSEEVENTF_LEFTDOWN = 0x2;
  15.         private readonly int MOUSEEVENTF_LEFTUP = 0x4;
  16.         const uint KBC_KEY_CMD = 0x64;
  17.         const uint KBC_KEY_DATA = 0x60;
  18.         //得到窗体句柄的函数,FindWindow函数用来返回符合指定的类名( ClassName )和窗口名( WindowTitle )的窗口句柄
  19.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  20.         public static extern IntPtr FindWindow(
  21.         string lpClassName, // pointer to class name
  22.         string lpWindowName // pointer to window name
  23.         );
  24.         [DllImport("user32.dll")]
  25.         private static extern int GetWindowThreadProcessId(IntPtr id, int pid);
  26.         [DllImport("kernel32.dll")]
  27.         private static extern void CloseHandle
  28.         (
  29.         uint hObject //Handle to object
  30.         );
  31.         //读取进程内存的函数
  32.         [DllImport("kernel32.dll")]
  33.         static extern bool ReadProcessMemory(uint hProcess, IntPtr lpBaseAddress,
  34.         IntPtr lpBuffer, uint nSize, ref uint lpNumberOfBytesRead);
  35.         //得到目标进程句柄的函数
  36.         [DllImport("kernel32.dll")]
  37.         public static extern uint OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  38.         //鼠标事件声明
  39.         [DllImport("user32.dll")]
  40.         static extern bool setcursorpos(int x, int y);
  41.         [DllImport("user32.dll")]
  42.         static extern void mouse_event(mouseeventflag flags, int dx, int dy, uint data, UIntPtr extrainfo);
  43.         //键盘事件声明
  44.         [DllImport("user32.dll")]
  45.         static extern byte MapVirtualKey(byte wCode, int wMap);
  46.         [DllImport("user32.dll")]
  47.         static extern short GetKeyState(int nVirtKey);
  48.         [DllImport("user32.dll")]
  49.         static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
  50.         //键盘事件声明winio
  51.         [DllImport("winio.dll")]
  52.         public static extern bool InitializeWinIo();
  53.         [DllImport("winio.dll")]
  54.         public static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize);
  55.         [DllImport("winio.dll")]
  56.         public static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize);
  57.         [DllImport("winio.dll")]
  58.         public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle);
  59.         [DllImport("winio.dll")]
  60.         public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr);
  61.         [DllImport("winio.dll")]
  62.         public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal);
  63.         [DllImport("winio.dll")]
  64.         public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal);
  65.         [DllImport("winio.dll")]
  66.         public static extern void ShutdownWinIo();
  67.         /// <summary>
  68.         /// 获取进程pid
  69.         /// </summary>
  70.         /// <param name="name"></param>
  71.         /// <returns></returns>
  72.         private int pid(String name)
  73.         {
  74.             try
  75.             {
  76.                 ObjectQuery oQuery = new ObjectQuery("select * from Win32_Process where Name='" + name + "'");
  77.                 ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);
  78.                 ManagementObjectCollection oReturnCollection = oSearcher.Get();
  79.                 string pid = "";
  80.                 string cmdLine;
  81.                 StringBuilder sb = new StringBuilder();
  82.                 foreach (ManagementObject oReturn in oReturnCollection)
  83.                 {
  84.                     pid = oReturn.GetPropertyvalue("ProcessId").ToString();
  85.                     //cmdLine = (string)oReturn.GetPropertyvalue("CommandLine");
  86.                     //string pattern = "-ap /"(.*)/"";
  87.                     //Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
  88.                     // Match match = regex.Match(cmdLine);
  89.                     //string appPoolName = match.Groups[1].ToString();
  90.                     //sb.AppendFormat("W3WP.exe PID: {0} AppPoolId:{1}/r/n", pid, appPoolName);
  91.                 }
  92.                 return Convert.ToInt32(pid);
  93.             }
  94.             catch (Exception ss)
  95.             { return 0; }
  96.         }
  97.         private int pid(IntPtr id)
  98.         {
  99.             int pid = 0;
  100.             pid = GetWindowThreadProcessId(id, pid);
  101.             return 260;
  102.         }
  103.         /// <summary>
  104.         /// 读取内存值
  105.         /// </summary>
  106.         /// <param name="name">进程id</param>
  107.         /// <param name="dizhi">读取的内存地址</param>
  108.         /// <returns></returns>
  109.         //public String getread(String QEC,String EC, IntPtr dizhi, uint size)
  110.         //{
  111.         // Byte bt = new Byte();
  112.         // IntPtr id=FindWindow(QEC, EC);
  113.         // uint hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid(id));
  114.         // IntPtr fanhui = new IntPtr();
  115.         // String gg = null;
  116.         // if (hProcess == 0)
  117.         // {
  118.         // // gg = ReadProcessMemory(hProcess, dizhi, fanhui, size, 0);
  119.         // // CloseHandle(hProcess);
  120.         // }
  121.         // return gg;
  122.         //}
  123.         public String getread(String jincheng, String EC, IntPtr dizhi, uint size)
  124.         {
  125.             byte[] vBuffer = new byte[4];
  126.             IntPtr vBytesAddress = Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0); // 得到缓冲区的地址
  127.             uint vNumberOfBytesRead = 0;
  128.             Byte bt = new Byte();
  129.             //IntPtr id = FindWindow(QEC, EC);
  130.             uint hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid(jincheng));
  131.             //pid(0);
  132.             IntPtr fanhui = new IntPtr();
  133.             String gg = null;
  134.             //if (hProcess == 0)
  135.             //{
  136.             if (ReadProcessMemory(hProcess, dizhi, vBytesAddress, (uint)vBuffer.Length, ref hProcess))
  137.             {
  138.                 CloseHandle(hProcess);
  139.             }
  140.             else
  141.             {
  142.                 CloseHandle(hProcess);
  143.             }
  144.             // }
  145.             int vInt = Marshal.ReadInt32(vBytesAddress);
  146.             return vInt.ToString();
  147.         }
  148.         /// <summary>
  149.         /// 获取键盘状态
  150.         /// </summary>
  151.         /// <param name="Key"></param>
  152.         /// <returns></returns>
  153.         public bool GetState(VirtualKeys Key)
  154.         {
  155.             return (GetKeyState((int)Key) == 1);
  156.         }
  157.         /// <summary>
  158.         /// 发送键盘事件
  159.         /// </summary>
  160.         /// <returns></returns>
  161.         public void Send(VirtualKeys Key, bool State)
  162.         {
  163.             if (State != GetState(Key))
  164.             {
  165.                 byte a = MapVirtualKey((byte)Key, 0);
  166.                 keybd_event((byte)Key, MapVirtualKey((byte)Key, 0), 0, 0);
  167.                 System.Threading.Thread.Sleep(1000);
  168.                 keybd_event((byte)Key, MapVirtualKey((byte)Key, 0), KEYEVENTF_KEYUP, 0);
  169.             }
  170.         }
  171.         /// <summary>
  172.         /// 初始化winio
  173.         /// </summary>
  174.         public void sendwinio()
  175.         {
  176.             if (InitializeWinIo())
  177.             {
  178.                 KBCWait4IBE();
  179.             }
  180.         }
  181.         private void KBCWait4IBE() //等待键盘缓冲区为空
  182.         {
  183.             //int[] dwVal = new int[] { 0 };
  184.             int dwVal = 0;
  185.             do
  186.             {
  187.                 //这句表示从&H64端口读取一个字节并把读出的数据放到变量dwVal中
  188.                 //GetPortVal函数的用法是GetPortVal 端口号,存放读出数据的变量,读入的长度
  189.                 bool flag = GetPortVal((IntPtr)0x64, out dwVal, 1);
  190.             }
  191.             while ((dwVal & 0x2) > 0);
  192.         }
  193.         /// <summary>
  194.         /// 模拟键盘标按下
  195.         /// </summary>
  196.         /// <param name="vKeyCoad"></param>
  197.         public void MykeyDown(int vKeyCoad)
  198.         {
  199.             int btScancode = 0;
  200.             btScancode = MapVirtualKey((byte)vKeyCoad, 0);
  201.             // btScancode = vKeyCoad;
  202.             KBCWait4IBE(); // '发送数据前应该先等待键盘缓冲区为空
  203.             SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1);// '发送键盘写入命令
  204.             //SetPortVal函数用于向端口写入数据,它的用法是SetPortVal 端口号,欲写入的数据,写入数据的长度
  205.             KBCWait4IBE();
  206.             SetPortVal(KBC_KEY_DATA, (IntPtr)0xe2, 1);// '写入按键信息,按下键
  207.             KBCWait4IBE(); // '发送数据前应该先等待键盘缓冲区为空
  208.             SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1);// '发送键盘写入命令
  209.             //SetPortVal函数用于向端口写入数据,它的用法是SetPortVal 端口号,欲写入的数据,写入数据的长度
  210.             KBCWait4IBE();
  211.             SetPortVal(KBC_KEY_DATA, (IntPtr)btScancode, 1);// '写入按键信息,按下键
  212.         }
  213.         /// <summary>
  214.         /// 模拟键盘弹出
  215.         /// </summary>
  216.         /// <param name="vKeyCoad"></param>
  217.         public void MykeyUp(int vKeyCoad)
  218.         {
  219.             int btScancode = 0;
  220.             btScancode = MapVirtualKey((byte)vKeyCoad, 0);
  221.             //btScancode = vKeyCoad;
  222.             KBCWait4IBE(); // '发送数据前应该先等待键盘缓冲区为空
  223.             SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1); //'发送键盘写入命令
  224.             KBCWait4IBE();
  225.             SetPortVal(KBC_KEY_DATA, (IntPtr)0xe0, 1);// '写入按键信息,释放键
  226.             KBCWait4IBE(); // '发送数据前应该先等待键盘缓冲区为空
  227.             SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1); //'发送键盘写入命令
  228.             KBCWait4IBE();
  229.             SetPortVal(KBC_KEY_DATA, (IntPtr)btScancode, 1);// '写入按键信息,释放键
  230.         }
  231.         /// <summary>
  232.         /// 模拟鼠标按下
  233.         /// </summary>
  234.         /// <param name="vKeyCoad"></param>
  235.         public void MyMouseDown(int vKeyCoad)
  236.         {
  237.             int btScancode = 0;
  238.             btScancode = MapVirtualKey((byte)vKeyCoad, 0);
  239.             //btScancode = vKeyCoad;
  240.             KBCWait4IBE(); // '发送数据前应该先等待键盘缓冲区为空
  241.             SetPortVal(KBC_KEY_CMD, (IntPtr)0xD3, 1);// '发送键盘写入命令
  242.             //SetPortVal函数用于向端口写入数据,它的用法是SetPortVal 端口号,欲写入的数据,写入数据的长度
  243.             KBCWait4IBE();
  244.             SetPortVal(KBC_KEY_DATA, (IntPtr)(btScancode | 0x80), 1);// '写入按键信息,按下键
  245.         }
  246.         /// <summary>
  247.         /// 模拟鼠标弹出
  248.         /// </summary>
  249.         /// <param name="vKeyCoad"></param>
  250.         public void MyMouseUp(int vKeyCoad)
  251.         {
  252.             int btScancode = 0;
  253.             btScancode = MapVirtualKey((byte)vKeyCoad, 0);
  254.             // btScancode = vKeyCoad;
  255.             KBCWait4IBE(); // '发送数据前应该先等待键盘缓冲区为空
  256.             SetPortVal(KBC_KEY_CMD, (IntPtr)0xD3, 1); //'发送键盘写入命令
  257.             KBCWait4IBE();
  258.             SetPortVal(KBC_KEY_DATA, (IntPtr)(btScancode | 0x80), 1);// '写入按键信息,释放键
  259.         }
  260.         /// <summary>
  261.         /// 发送鼠标事件
  262.         /// </summary>
  263.         /// <returns></returns>
  264.         public void SendMouse()
  265.         {
  266.         }
  267.         /// <summary>
  268.         /// 鼠标动作枚举
  269.         /// </summary>
  270.         public enum mouseeventflag : uint
  271.         {
  272.             move = 0x0001,
  273.             leftdown = 0x0002,
  274.             leftup = 0x0004,
  275.             rightdown = 0x0008,
  276.             rightup = 0x0010,
  277.             middledown = 0x0020,
  278.             middleup = 0x0040,
  279.             xdown = 0x0080,
  280.             xup = 0x0100,
  281.             wheel = 0x0800,
  282.             virtualdesk = 0x4000,
  283.             absolute = 0x8000
  284.         }
  285.         /// <summary>
  286.         /// 键盘动作枚举
  287.         /// </summary>
  288.         public enum VirtualKeys : byte
  289.         {
  290.             //VK_NUMLOCK = 0x90, //数字锁定键
  291.             //VK_SCROLL = 0x91, //滚动锁定
  292.             //VK_CAPITAL = 0x14, //大小写锁定
  293.             //VK_A = 62, //键盘A
  294.             VK_LBUTTON = 1, //鼠标左键
  295.             VK_RBUTTON = 2,  //鼠标右键
  296.             VK_CANCEL = 3,    //Ctrl+Break(通常不需要处理)
  297.             VK_MBUTTON = 4,   //鼠标中键
  298.             VK_BACK = 8,     //Backspace
  299.             VK_TAB = 9,     //Tab
  300.             VK_CLEAR = 12,    //Num Lock关闭时的数字键盘5
  301.             VK_RETURN = 13,   //Enter(或者另一个)
  302.             VK_SHIFT = 16,    //Shift(或者另一个)
  303.             VK_CONTROL = 17,   //Ctrl(或者另一个)
  304.             VK_MENU = 18,    //Alt(或者另一个)
  305.             VK_PAUSE = 19,    //Pause
  306.             VK_CAPITAL = 20,   //Caps Lock
  307.             VK_ESCAPE = 27,   //Esc
  308.             VK_SPACE = 32,    //Spacebar
  309.             VK_PRIOR = 33,    //Page Up
  310.             VK_NEXT = 34,    //Page Down
  311.             VK_END = 35,     //End
  312.             VK_HOME = 36,    //Home
  313.             VK_LEFT = 37,    //左箭头
  314.             VK_UP = 38,     //上箭头
  315.             VK_RIGHT = 39,    //右箭头
  316.             VK_DOWN = 40,    //下箭头
  317.             VK_SELECT = 41,   //可选
  318.             VK_PRINT = 42,    //可选
  319.             VK_EXECUTE = 43,   //可选
  320.             VK_SNAPSHOT = 44,  //Print Screen
  321.             VK_INSERT = 45,   //Insert
  322.             VK_DELETE = 46,   //Delete
  323.             VK_HELP = 47,   //可选
  324.             VK_NUM0 = 48, //0
  325.             VK_NUM1 = 49, //1
  326.             VK_NUM2 = 50, //2
  327.             VK_NUM3 = 51, //3
  328.             VK_NUM4 = 52, //4
  329.             VK_NUM5 = 53, //5
  330.             VK_NUM6 = 54, //6
  331.             VK_NUM7 = 55, //7
  332.             VK_NUM8 = 56, //8
  333.             VK_NUM9 = 57, //9
  334.             VK_A = 65, //A
  335.             VK_B = 66, //B
  336.             VK_C = 67, //C
  337.             VK_D = 68, //D
  338.             VK_E = 69, //E
  339.             VK_F = 70, //F
  340.             VK_G = 71, //G
  341.             VK_H = 72, //H
  342.             VK_I = 73, //I
  343.             VK_J = 74, //J
  344.             VK_K = 75, //K
  345.             VK_L = 76, //L
  346.             VK_M = 77, //M
  347.             VK_N = 78, //N
  348.             VK_O = 79, //O
  349.             VK_P = 80, //P
  350.             VK_Q = 81, //Q
  351.             VK_R = 82, //R
  352.             VK_S = 83, //S
  353.             VK_T = 84, //T
  354.             VK_U = 85, //U
  355.             VK_V = 86, //V
  356.             VK_W = 87, //W
  357.             VK_X = 88, //X
  358.             VK_Y = 89, //Y
  359.             VK_Z = 90, //Z
  360.             VK_NUMPAD0 = 96, //0
  361.             VK_NUMPAD1 = 97, //1
  362.             VK_NUMPAD2 = 98, //2
  363.             VK_NUMPAD3 = 99, //3
  364.             VK_NUMPAD4 = 100, //4
  365.             VK_NUMPAD5 = 101, //5
  366.             VK_NUMPAD6 = 102, //6
  367.             VK_NUMPAD7 = 103, //7
  368.             VK_NUMPAD8 = 104, //8
  369.             VK_NUMPAD9 = 105, //9
  370.             VK_NULTIPLY = 106,  //数字键盘上的*
  371.             VK_ADD = 107,    //数字键盘上的+
  372.             VK_SEPARATOR = 108, //可选
  373.             VK_SUBTRACT = 109,  //数字键盘上的-
  374.             VK_DECIMAL = 110,  //数字键盘上的.
  375.             VK_DIVIDE = 111,   //数字键盘上的/
  376.             VK_F1 = 112,
  377.             VK_F2 = 113,
  378.             VK_F3 = 114,
  379.             VK_F4 = 115,
  380.             VK_F5 = 116,
  381.             VK_F6 = 117,
  382.             VK_F7 = 118,
  383.             VK_F8 = 119,
  384.             VK_F9 = 120,
  385.             VK_F10 = 121,
  386.             VK_F11 = 122,
  387.             VK_F12 = 123,
  388.             VK_NUMLOCK = 144,  //Num Lock
  389.             VK_SCROLL = 145   // Scroll Lock
  390.         }
  391.     }
  392. }
复制代码



上一篇:各种语言转换为C#
下一篇:不要做学究【少走歪路】
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对 订阅订阅

QQ

沙发
发表于 2016-3-5 15:14:48 | 只看该作者
谢谢,过来支持一下

QQ

3#
发表于 2016-3-6 02:09:56 | 只看该作者
支持楼主,加油。加油!

QQ

4#
发表于 2016-3-6 18:16:10 | 只看该作者

QQ

5#
发表于 2016-3-7 16:01:01 | 只看该作者
谢谢,过来支持一下
您需要登录后才可以回帖 登录 | 注册并了解

本版积分规则

       
    闽ICP备18004340号-3

GMT+8, 2024-4-24 20:03 , Processed in 0.418092 second(s), 48 queries .

返回顶部