c#类,封装了键盘,和鼠标模拟,和内存读取

这是一个C#程序,封装了键盘、鼠标操作和内存读取功能。通过实例调用展示了如何模拟键盘按下和释放,以及使用Windows API进行进程内存读取。代码包括了获取窗体句柄、鼠标事件、键盘事件和内存访问的相关函数声明及实现。
摘要由CSDN通过智能技术生成
key kk  =   new  key();
            
//   IntPtr a = new IntPtr();
            kk.sendwinio();
            kk.MykeyDown((
int )key.VirtualKeys.VK_F1);
            System.Threading.Thread.Sleep(
2000 );
            kk.MykeyUp((
int )key.VirtualKeys.VK_F1);
这是模拟键盘的调用方式

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.InteropServices;
using  System.Diagnostics;
using  System.Management;
namespace  sendkey
{
    
public class key
    
{
        
const uint PROCESS_ALL_ACCESS = 0x001F0FFF;
        
const uint KEYEVENTF_EXTENDEDKEY = 0x1;
        
const uint KEYEVENTF_KEYUP = 0x2;
        
private readonly int MOUSEEVENTF_LEFTDOWN = 0x2;
        
private readonly int MOUSEEVENTF_LEFTUP = 0x4;
        
const uint KBC_KEY_CMD = 0x64;
        
const uint KBC_KEY_DATA = 0x60;
        
//得到窗体句柄的函数,FindWindow函数用来返回符合指定的类名( ClassName )和窗口名( WindowTitle )的窗口句柄
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        
public static extern IntPtr FindWindow(
        
string lpClassName, //   pointer   to   class   name   
        string lpWindowName   //   pointer   to   window   name   
        );
        [DllImport(
"user32.dll")]
        
private static extern int GetWindowThreadProcessId(IntPtr id,int pid);
       
        [DllImport(
"kernel32.dll")]
        
private static extern void CloseHandle
        (
         
uint hObject //Handle to object
        );
        
//读取进程内存的函数
        [DllImport("kernel32.dll")]
        
static extern bool ReadProcessMemory(uint hProcess, IntPtr lpBaseAddress,
           IntPtr lpBuffer, 
uint nSize, ref   uint lpNumberOfBytesRead);
        
//得到目标进程句柄的函数
        [DllImport("kernel32.dll")]
        
public static extern uint OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
        
//鼠标事件声明
        [DllImport("user32.dll")]
        
static extern bool setcursorpos(int x, int y);
        [DllImport(
"user32.dll")]
        
static extern void mouse_event(mouseeventflag flags, int dx, int dy, uint data, UIntPtr extrainfo);
        
//键盘事件声明
        [DllImport("user32.dll")]
        
static extern byte MapVirtualKey(byte wCode, int wMap);
        [DllImport(
"user32.dll")]
        
static extern short GetKeyState(int nVirtKey);
        [DllImport(
"user32.dll")]
        
static extern void keybd_event( byte bVk, byte bScan,uint dwFlags,uint dwExtraInfo);
        
//键盘事件声明winio
        [DllImport("winio.dll")]
        
public static extern bool InitializeWinIo();
        [DllImport(
"winio.dll")]
        
public static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize);
        [DllImport(
"winio.dll")]
        
public static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize);
        [DllImport(
"winio.dll")]
        
public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle);
        [DllImport(
"winio.dll")]
        
public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr);
        [DllImport(
"winio.dll")]
        
public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal);
        [DllImport(
"winio.dll")]
        
public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal);
        [DllImport(
"winio.dll")]
        
public static extern void ShutdownWinIo();
         


        

       

       

        
/// <summary>
        
/// 获取进程pid
        
/// </summary>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值