C#游戏外挂代码

原文链接: http://www.cnblogs.com/wysky/archive/2007/08/13/854016.html
转自http://www.yitian130.com/article.asp?id=19&page=3


XDF.GamePlugInCommon 类库项目
  1  // API.cs 文件,定义一些常用API函数及常量 
  2 
  3  using  System; 
  4  using  System.IO; 
  5  using  System.Threading; 
  6  using  System.Diagnostics; 
  7  using  System.Runtime.InteropServices; 
  8 
  9  namespace  XDF.GamePlugInCommon 
 10 
 11  /**/ ///   <summary>  
 12  ///  API 的摘要说明。 
 13  ///   </summary>  
 14  public   sealed   class  API 
 15 
 16  public   static   int  WM_KEYDOWN  =   0x0100
 17  public   static   int  WM_KEYUP  =   0x0101
 18  public   static   int  WM_SYSKEYDOWN  =   0x0104
 19  public   static   int  WM_SYSKEYUP  =   0x0105
 20 
 21  public   static   int  WM_MOUSEMOVE  =   0x0200
 22  public   static   int  WM_LBUTTONDOWN  =   0x0201
 23  public   static   int  WM_LBUTTONUP  =   0x0202
 24  public   static   int  WM_LBUTTONDBLCLK  =   0x0203
 25  public   static   int  WM_RBUTTONDOWN  =   0x0204
 26  public   static   int  WM_RBUTTONUP  =   0x0205
 27  public   static   int  WM_RBUTTONDBLCLK  =   0x0206
 28  public   static   int  WM_USER  =   0x0400
 29 
 30  public   static   int  MK_LBUTTON  =   0x0001
 31  public   static   int  MK_RBUTTON  =   0x0002
 32  public   static   int  MK_SHIFT  =   0x0004
 33  public   static   int  MK_CONTROL  =   0x0008
 34  public   static   int  MK_MBUTTON  =   0x0010
 35 
 36  public   static   int  MK_XBUTTON1  =   0x0020
 37  public   static   int  MK_XBUTTON2  =   0x0040
 38 
 39  [DllImport( " user32.dll " )] 
 40  public   static   extern   int  SendMessage(IntPtr hWnd, int  Msg, int  wParam, int  lParam); 
 41 
 42  // 此处主要用来让窗口置于最前(SetWindowPos(this.Handle,-1,0,0,0,0,0x4000|0x0001|0x0002);) 
 43  [System.Runtime.InteropServices.DllImport( " user32.dll " )] 
 44  public   static   extern   bool  SetWindowPos(IntPtr hWnd, 
 45  int  hWndInsertAfter, 
 46  int  X, 
 47  int  Y, 
 48  int  cx, 
 49  int  cy, 
 50  int  uFlags 
 51  ); 
 52 
 53  /**/ ///   <summary>  
 54  ///  窗口置前 
 55  ///   </summary>  
 56  ///   <param name="hWnd"></param>  
 57  public   static   void  SetWindowPos(IntPtr hWnd) 
 58 
 59  SetWindowPos(hWnd, - 1 , 0 , 0 , 0 , 0 , 0x4000 | 0x0001 | 0x0002 ); 
 60 
 61 
 62  /**/ ///   <summary>  
 63  ///  
 64  ///   </summary>  
 65  ///   <param name="processName"></param>  
 66  ///   <returns></returns>  
 67  public   static  Process GetGameProcess( string  processName) 
 68 
 69  Process pro  =   null
 70  Process[] pros  =  Process.GetProcessesByName(processName); 
 71  if (pros.Length  >   0
 72 
 73  pro  =  pros[ 0 ]; 
 74 
 75  return  pro; 
 76 
 77 
 78 
 79 
 80  项目(应用程序) 
 81  XDF.TantraPlugIn 
 82  // ControlItem.cs 
 83  using  System; 
 84  using  System.IO; 
 85  using  System.Xml.Serialization; 
 86 
 87  namespace  XDF.TantraPlugIn 
 88 
 89  /**/ ///   <summary>  
 90  ///  ControlItem 的摘要说明。 
 91  ///   </summary>  
 92  [Serializable] 
 93  public   sealed   class  ControlItem 
 94 
 95  private   string  m_Name  =   ""
 96  public   string  Name 
 97 
 98  get  
 99 
100  return   this .m_Name; 
101 
102  set  
103 
104  this .m_Name  =  value; 
105 
106 
107  private   char  m_KeyChar  =   ' a '
108  public   char  KeyChar 
109 
110  get  
111 
112  return   this .m_KeyChar; 
113 
114  set  
115 
116  this .m_KeyChar  =  value; 
117 
118 
119  private   int  m_DelayTime  =   100
120  public   int  DelayTime 
121 
122  get  
123 
124  return   this .m_DelayTime; 
125 
126  set  
127 
128  this .m_DelayTime  =  value; 
129 
130 
131  public  ControlItem() 
132 
133 
134 
135 
136  [Serializable] 
137  public   sealed   class  ControlItemCollection : System.Collections.CollectionBase 
138 
139  public  ControlItem  this [ int  index] 
140 
141  get  
142 
143  return  (ControlItem)List[index]; 
144 
145  set  
146 
147  List[index]  =  value; 
148 
149 
150  public  ControlItemCollection() 
151 
152 
153  public   int  Add(ControlItem item) 
154 
155  return  List.Add(item); 
156 
157  public   void  Remove(ControlItem item) 
158 
159  List.Remove(item); 
160 
161 
162  }
163 
164  // TantraConfig.cs 
165  using  System; 
166  using  System.IO; 
167  using  System.Xml.Serialization; 
168 
169  namespace  XDF.TantraPlugIn 
170 
171  /**/ ///   <summary>  
172  ///  TantraConfig 的摘要说明。 
173  ///   </summary>  
174  [Serializable] 
175  public   class  TantraConfig 
176 
177  private  ControlItemCollection m_KillControls  =   new  ControlItemCollection(); 
178  public  ControlItemCollection KillControls 
179 
180  get  
181 
182  return   this .m_KillControls; 
183 
184  set  
185 
186  this .m_KillControls  =  value; 
187 
188 
189  private  ControlItemCollection m_BloodControls  =   new  ControlItemCollection(); 
190  public  ControlItemCollection BloodControls 
191 
192  get  
193 
194  return   this .m_BloodControls; 
195 
196  set  
197 
198  this .m_BloodControls  =  value; 
199 
200 
201 
202  private   int  m_BloodRate  =   25
203 
204  public   int  BloodRate 
205 
206  get  
207 
208  return   this .m_BloodRate; 
209 
210  set  
211 
212  this .m_BloodRate  =  value; 
213 
214 
215 
216  private   string  m_ProcessName  =   " HTLauncher "
217 
218  public   string  ProcessName 
219 
220  get  
221 
222  return   this .m_ProcessName; 
223 
224  set  
225 
226  this .m_ProcessName  =  value; 
227 
228 
229 
230  public  TantraConfig() 
231 
232 
233 
234 
235  public   bool  Save( string  file) 
236 
237  bool  result  =   false
238  try  
239 
240  FileStream fs  =   new  FileStream(file,FileMode.Create,FileAccess.Write); 
241  XmlSerializer xsl  =   new  XmlSerializer( this .GetType()); 
242  xsl.Serialize(fs, this ); 
243  fs.Close(); 
244  result  =   true
245 
246  catch  
247 
248  result  =   false
249 
250  return  result; 
251 
252  public   static  TantraConfig LoadFromFile( string  file) 
253 
254  TantraConfig config  =   null
255  try  
256 
257  FileStream fs  =   new  FileStream(file,FileMode.Open,FileAccess.Read); 
258  XmlSerializer xsl  =   new  XmlSerializer( typeof (TantraConfig)); 
259  config  =  (TantraConfig)xsl.Deserialize(fs); 
260  fs.Close(); 
261 
262  catch  
263 
264 
265 
266  return  config; 
267 
268 
269 
270 
271 
272  // Frmmain.cs 
273  using  System; 
274  using  System.Drawing; 
275  using  System.Collections; 
276  using  System.ComponentModel; 
277  using  System.Windows.Forms; 
278  using  System.Data; 
279  using  System.Threading; 
280 
281  using  XDF.GamePlugInCommon; 
282 
283  namespace  XDF.TantraPlugIn 
284 
285  /**/ ///   <summary>  
286  ///  Form1 的摘要说明。 
287  ///   </summary>  
288  public   class  Frmmain : System.Windows.Forms.Form 
289 
290  private  System.Windows.Forms.Button btnSetup; 
291  private  System.Windows.Forms.Timer timerMain; 
292  private  System.Windows.Forms.Button btnStart; 
293  private  System.ComponentModel.IContainer components; 
294 
295  public  Frmmain() 
296 
297  //  
298  //  Windows 窗体设计器支持所必需的 
299  //  
300  InitializeComponent(); 
301 
302 
303  this .Closing  += new  CancelEventHandler(Frmmain_Closing); 
304 
305 
306  /**/ ///   <summary>  
307  ///  清理所有正在使用的资源。 
308  ///   </summary>  
309  protected   override   void  Dispose(  bool  disposing ) 
310 
311  if ( disposing ) 
312 
313  if  (components  !=   null
314 
315  components.Dispose(); 
316 
317 
318  base .Dispose( disposing ); 
319 
320 
321  Windows 窗体设计器生成的代码 #region  Windows 窗体设计器生成的代码 
322  /**/ ///   <summary>  
323  ///  设计器支持所需的方法 - 不要使用代码编辑器修改 
324  ///  此方法的内容。 
325  ///   </summary>  
326  private   void  InitializeComponent() 
327 
328  this .components  =   new  System.ComponentModel.Container(); 
329  System.Resources.ResourceManager resources  =   new  System.Resources.ResourceManager( typeof (Frmmain)); 
330  this .btnStart  =   new  System.Windows.Forms.Button(); 
331  this .btnSetup  =   new  System.Windows.Forms.Button(); 
332  this .timerMain  =   new  System.Windows.Forms.Timer( this .components); 
333  this .SuspendLayout(); 
334  //  
335  //  btnStart 
336  //  
337  this .btnStart.Location  =   new  System.Drawing.Point( 8 16 ); 
338  this .btnStart.Name  =   " btnStart "
339  this .btnStart.Size  =   new  System.Drawing.Size( 65 22 ); 
340  this .btnStart.TabIndex  =   0
341  this .btnStart.Text  =   " 开始(&S) "
342  this .btnStart.Click  +=   new  System.EventHandler( this .btnStart_Click); 
343  //  
344  //  btnSetup 
345  //  
346  this .btnSetup.Location  =   new  System.Drawing.Point( 152 16 ); 
347  this .btnSetup.Name  =   " btnSetup "
348  this .btnSetup.Size  =   new  System.Drawing.Size( 65 22 ); 
349  this .btnSetup.TabIndex  =   1
350  this .btnSetup.Text  =   " 设置(&C) "
351  this .btnSetup.Click  +=   new  System.EventHandler( this .btnSetup_Click); 
352  //  
353  //  Frmmain 
354  //  
355  this .AutoScaleBaseSize  =   new  System.Drawing.Size( 6 14 ); 
356  this .ClientSize  =   new  System.Drawing.Size( 226 55 ); 
357  this .Controls.Add( this .btnSetup); 
358  this .Controls.Add( this .btnStart); 
359  this .FormBorderStyle  =  System.Windows.Forms.FormBorderStyle.FixedDialog; 
360  this .Icon  =  ((System.Drawing.Icon)(resources.GetObject( " $this.Icon " ))); 
361  this .MaximizeBox  =   false
362  this .MinimizeBox  =   false
363  this .Name  =   " Frmmain "
364  this .StartPosition  =  System.Windows.Forms.FormStartPosition.CenterScreen; 
365  this .Text  =   " Tantra PlugIn beta1 "
366  this .ResumeLayout( false ); 
367 
368 
369  #endregion  
370 
371  /**/ ///   <summary>  
372  ///  应用程序的主入口点。 
373  ///   </summary>  
374  [STAThread] 
375  static   void  Main() 
376 
377  Application.Run( new  Frmmain()); 
378 
379 
380  private  TantraConfig m_TantraConfig  =   null
381  private  Thread m_Thread  =   null
382  private   bool  m_Stop  =   true
383  private  IntPtr m_GameMainWindowHandle  =  IntPtr.Zero; 
384 
385  private   void  btnSetup_Click( object  sender, System.EventArgs e) 
386 
387  TantraConfig config  =   new  TantraConfig(); 
388 
389  ControlItemCollection items  =  config.KillControls; 
390 
391  ControlItem item_e  =   new  ControlItem(); 
392  item_e.DelayTime  =   50
393  item_e.KeyChar  =   ' E '
394  item_e.Name  =   " 选择最近的攻击目标 "
395  items.Add(item_e); 
396 
397  ControlItem item_r  =   new  ControlItem(); 
398  item_r.DelayTime  =   6000
399  item_r.KeyChar  =   ' R '
400  item_r.Name  =   " 攻击选定的目标 "
401  items.Add(item_r); 
402 
403  ControlItem item_f  =   new  ControlItem(); 
404  item_f.DelayTime  =   500
405  item_f.KeyChar  =   ' F '
406  item_f.Name  =   " 捡起打完怪物掉下的物品 "
407  items.Add(item_f); 
408 
409  ControlItem item_f2  =   new  ControlItem(); 
410  item_f2.DelayTime  =   500
411  item_f2.KeyChar  =   ' F '
412  item_f2.Name  =   " 捡起打完怪物掉下的金币 "
413  items.Add(item_f2); 
414 
415  ControlItem item_blood  =   new  ControlItem(); 
416  item_blood.DelayTime  =   1000
417  item_blood.KeyChar  =   ' 1 '
418  item_blood.Name  =   " 自动增加体能秘技 "
419  config.BloodControls.Add(item_blood); 
420 
421  config.Save( " c:\\tantra.xml " ); 
422 
423 
424 
425  private   void  btnStart_Click( object  sender, System.EventArgs e) 
426 
427  if ( this .m_Stop) 
428 
429  this .StartControl(); 
430 
431  else  
432 
433  this .StopControl(); 
434 
435  this .btnStart.Text  =  ( this .m_Stop) ? " 开始(&S) " : " 停止(&S) "
436 
437 
438  private   void  StartControl() 
439 
440  string  file  =  Environment.CurrentDirectory  +   " \\tantra.xml "
441  this .m_TantraConfig  =  TantraConfig.LoadFromFile(file); 
442  if ( this .m_TantraConfig  ==   null
443 
444  MessageBox.Show( " 配置文件未找到,无法启动! " ); 
445  return
446 
447 
448  // HTLauncher 
449  // string proname = "TantraPlugIn"; 
450  System.Diagnostics.Process pro  =  API.GetGameProcess( this .m_TantraConfig.ProcessName); 
451  if (pro  ==   null
452 
453  MessageBox.Show( " 游戏进程  " + this .m_TantraConfig.ProcessName + "  未找到,无法启动! " ); 
454  return
455 
456  this .m_GameMainWindowHandle  =  pro.MainWindowHandle; 
457  this .Text  =   " Game name: "   +  pro.ProcessName; 
458 
459 
460  this .m_Stop  =   false
461  this .m_Thread  =   new  Thread( 
462  new  ThreadStart(TantraControl)); 
463 
464  this .m_Thread.Start(); 
465 
466 
467  private   void  StopControl() 
468 
469  if ( this .m_Thread  !=   null
470 
471  this .m_Stop  =   true
472  this .m_Thread.Abort(); 
473 
474 
475 
476  private   void  TantraControl() 
477 
478  int  count  =   0
479  while ( ! this .m_Stop) 
480 
481  for ( int  i = 0 ;i < this .m_TantraConfig.KillControls.Count;i ++
482 
483  API.SendMessage( this .m_GameMainWindowHandle,API.WM_KEYDOWN, 
484  Convert.ToInt32( this .m_TantraConfig.KillControls[i].KeyChar), 0 ); 
485  Thread.Sleep( this .m_TantraConfig.KillControls[i].DelayTime); 
486 
487  count  ++
488  if (count  >=   this .m_TantraConfig.BloodRate) 
489 
490  count  =   0
491  for ( int  i = 0 ;i < this .m_TantraConfig.BloodControls.Count;i ++
492 
493  API.SendMessage( this .m_GameMainWindowHandle,API.WM_KEYDOWN, 
494  Convert.ToInt32( this .m_TantraConfig.BloodControls[i].KeyChar), 0 ); 
495  Thread.Sleep( this .m_TantraConfig.BloodControls[i].DelayTime); 
496 
497 
498 
499 
500 
501  protected   override   void  WndProc( ref  Message m) 
502 
503  base .WndProc ( ref  m); 
504  if (m.Msg  ==  API.WM_KEYDOWN) 
505 
506  this .Text  =  m.WParam.ToInt32().ToString(); 
507  if ( this .Text  ==   " 1 "
508 
509  MessageBox.Show( " blood " ); 
510 
511 
512 
513 
514  private   void  Frmmain_Closing( object  sender, CancelEventArgs e) 
515 
516  try  
517 
518  this .StopControl(); 
519 
520  catch  
521 
522 
523 
524 
525 
526 
以上是全部代码 
设置功能未完善,可以通过手动修改XML配置文件实现其他类似游戏的外挂 
附带典型(12级)外挂配置,配置文件随着各人级别不同和技能不同自己做修改。

转载于:https://www.cnblogs.com/wysky/archive/2007/08/13/854016.html

猜你喜欢

转载自blog.csdn.net/weixin_30340353/article/details/94961437