The Stars ...My Destination

adamxx

天下事,法无定法,然后知非法法之
世间人,尤了未了,何妨以不了了之

导航

.net实现游戏外挂 一

Posted on 2005-11-10 22:33  adamxx  阅读(1743)  评论(1编辑  收藏  举报

大一的时候,寝室的室友在玩一个新出来的网络游戏,叫做密传”,于没有合适的练级外挂,由于他玩的是弓箭手,我便用C#写了一个弓箭手练级外挂.

今天我偶然看到这个很久以前写的程序,于是整理出来奉献给大家,其实用VC++来实现更好,不过这样希望能给.net的初学者一点帮助.

首先,这不是介绍如何破解IP封包的文章,当时写这个的外挂只是做自动练级用,出于技术不够,便想了一个很笨的方法,从屏幕上的HP值的变化来判断是否自动补血,而打怪更简单,这个游戏本身就带有寻找怪的功能,而弓箭手就只需要不断的瞄准和射击就可以了,而瞄准和射击也就是不断的按下(keydown)玩家指定的键,F1. 从这点来看,说是外挂其实有点勉强了. 

对于自动找怪和打怪, 说白了就是一个按键精灵. .net下发送键盘消息可以用这个方法来实现System.Windows.Forms.SendKeys.Send(strKey),strKey是参数,代表要按下的键比如a就代表要按下A键,而 {F4} 就是ALT + F4,我们利用这个函数来实现模拟按键,但是这个有一个不好的地方,就是他只把模拟按键发送给激活窗口,如果是vc的话当然可以用event-keyboard来实现,在这个地方,我们可以强制把游戏窗口置顶.我们用下面的API来实现.

//这个函数是查找窗口句柄的, 参数sClassName,是类名, 参数sWindowTitle是窗口名 
[DllImport("user32.dll", CharSet=CharSet.Auto)] 
public static extern IntPtr FindWindow(IntPtr sClassName, string sWindowTitle);   



//这个函数用来置顶显示,参数hwnd为窗口句柄 
[DllImport("user32.dll", CharSet=CharSet.Auto)] 
public static extern void SetForegroundWindow(int hwnd); 



//这个函数用来显示窗口,参数hwnd为窗口句柄,nCmdShow是显示类型的枚举 
[DllImport("user32.dll")] 
public static extern bool ShowWindow(int hWnd, nCmdShow nCmdShow); 

public enum nCmdShow:uint
              SW_FORCEMINIMIZE
=0x0
              SW_HIDE
=0x1
              SW_MAXIMIZE
=0x2
              SW_MINIMIZE
=0x3
              SW_RESTORE
=0x4
              SW_SHOW
=0x5
              SW_SHOWDEFAULT
=0x6
              SW_SHOWMAXIMIZED
=0x7
              SW_SHOWMINIMIZED
=0x8
              SW_SHOWMINNOACTIVE
=0x9
              SW_SHOWNA
=0xA
              SW_SHOWNOACTIVATE
=0xB
              SW_SHOWNORMAL
=0xC
              WM_CLOSE
=0x10
         }
   


现在让我们看看如何实现自动键盘精灵.

我先定义了一个Timer,用于每各一段时间就向窗口发送一次按键.由于攻击是要先瞄准在攻击,所以在我在定义了一个bool whichKey,用来指示现在是在瞄准还是攻击.

private System.Windows.Forms.Timer timer1; //timer1主要控制瞄准与打怪 
private System.Windows.Forms.Timer timer2; //timer2 主要监视当前血量和补血 
private string strCollimation;    //瞄准键 
private string strAttack;         //攻击键 
private string strHP;             //补血键 
private string strMP;             //补蓝键 
private bool whichKey;            //瞄准和攻击的状态 
private int count;                //定义一个变化指示 

// 桌面工作区的尺寸 
Size workingArea; 

// Form 的初始位置和在左下角,右下角的位置 
Point formLoc, ptLeftBottom, ptRightBottom; 



//初始化设置 
private void Init()
     
this.timer1.Interval =Convert.ToInt32(Convert.ToDouble(this.txtTime.Text) * 1000); 
     
this.whichKey = false
     strCollimation 
= this.txtCollimation.Text; 
     strAttack 
= this.txtAttack.Text; 
     strHP 
= this.txtHP.Text; 
     strMP 
= this.txtMP.Text; 
}
 

我们在进行如上的初始化设置,其中txtTime,txtCollimation,txtAttackTextBox.  

timer1的Tick事件如下   ( timer2 见后面)

private void timer1_Tick(object sender, System.EventArgs e)
     
if(this.whichKey == false)
         System.Windows.Forms.SendKeys.Send(strCollimation); 
         
this.whichKey = true
     }
 
     
else
         System.Windows.Forms.SendKeys.Send(strAttack); 
         
this.whichKey = false
         }
 
     }
 
}   

whichKey变量使timer1的TICK事件不断的交替发送瞄准和攻击按键.

刚才我们说过, System.Windows.Forms.SendKeys.Send(string str) 函数只能发送给当前的激活窗口,我们还需要把窗口强制激活了才行.我定义了一个按钮(Button)name=btnOK,按下后既开启外挂.下面我们来看看btnOK的事件处理.

private void btnOK_Click(object sender, System.EventArgs e)
     Init(); 
     
string strWindowsName = "Tantra Launcher"//窗口标题 
     IntPtr hWnd = FindWindow( “” ,strWindowsName);//窗口句柄 
     if(hWnd.ToInt32()>0)
          SetForegroundWindow(hNext.ToInt32());
//置顶显示 
          ShowWindow(hNext.ToInt32(),nCmdShow.SW_SHOWMINNOACTIVE); //显示窗口   
          this.timer1.Enabled = true;       //开始发送按键 
          this.timer2.Enabled = true;       //开始监控当前HP值 
     }
 
     
else 
                   MessageBox.Show(
"错误","未发现游戏,请先启动游戏后在打开外挂!",MessageBoxButtons.OK,MessageBoxIcon.Error); 
     formLoc         
= this.Location; 
     
this.Location   = ptRightBottom; 
}
   

如上,当点下确定按钮后,首先查找一个叫Tantra Launcher窗口,这是密传的窗口名,返回该窗口的句柄,然后SetForegroundWindow函数把他置顶显示,又由ShowWindow将他显示,最后this.timer1.Enabled = true.

这样我们用来实现自动打怪的按键精灵就实现了.