公布C#写的网游外挂源代码

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