C# 紅色警戒2無限金錢+電力負載 外掛源碼樣本
如有轉載請註明出處:http://www.cnblogs.com/flydoos/archive/2012/01/19/2326149.html
C# 紅色警戒2無限金錢+電力負載 外掛源碼樣本
背景說明:
這幾天放假,在家閑著沒事做(有正事但是也不想做),於是乎,就玩起了多年前的遊戲——紅色警戒2!但是不知為啥,幾年沒玩,現在如此生疏~~和電腦對戰 1 VS 7 居然打不贏,蛋疼了。剛把基地建好,7個國家就同時進攻,一下子把我K掉了。
“我不服”,所以就想動手寫一個協助工具輔助了。其實,現在說這個教程沒啥意義。因為多年前我也寫過這個小工具,只是當初不是用C#寫的,反正閑著沒事,今天就用C#寫一個吧。寫得不好的地方,希望大家指出來,謝謝。
存在問題:
不知道為啥修改了電力之後,在遊戲中也顯示被修改了,但是實際上依然會提示“電力不足”。修改後的電力,只對“愛國者飛彈、光稜塔”之類的有效,但是對於其他建築物是無效的。
有人說是沒有找對基址。但是我對CE也不太熟悉,搞了很久沒解決。希望有人能解釋下,為什麼無法改電力(不是負載)。
2012-01-21 14:22 更新
今天,又花了幾個小時去研究上次的“電力負載”問題,嘻嘻,皇天不負有心人。這次已經搞定了,現在已經可以實現 無限電力+無限金錢。 上次的那個基址也是正確的,但是不能直接寫記憶體去修改,而是要NOP,大家只要在上次的基礎上,增加以下代碼就行:
Helper.WriteMemoryValue(0x004F2D9B, ProcessName, -1869574000);
Helper.WriteMemoryValue(0x004F2D9C, ProcessName, -1869574000);
Helper.WriteMemoryValue(0x004F2D9D, ProcessName, -1869574000);
Helper.WriteMemoryValue(0x004F2D9E, ProcessName, -1953460080);
Helper.WriteMemoryValue(0x004F2DA0, ProcessName, -1743418480);
完整源碼:
// Program.cs
using System;
using System.Threading;
/* * * 作者:牛A與牛C之間 * Q Q:1046559384 C#/Java技術交流群:96020642 * 微博:http://weibo.com/flydoos * 部落格:http://www.cnblogs.com/flydoos * 日期:2012-01-19 * * 金錢:0x00A35DB4 + 0x24C * 負載:0x00A35DB4 + 0x52D4 * */
namespace RedAlert2
{
class Program
{
private const int BaseAddress = 0x00A35DB4;
private const string ProcessName = "GAME";
static void Main()
{
while (true)
{
if (Helper.GetPidByProcessName(ProcessName) == 0)
{
Console.WriteLine("對不起,您還沒有啟動紅色警戒Ⅱ遊戲!");
Console.Read();
return;
}
var moneyAddress = Helper.ReadMemoryValue(BaseAddress, ProcessName) + 0x24C;
Helper.WriteMemoryValue(moneyAddress, ProcessName, 999999999);
Console.WriteLine(DateTime.Now + ":" + Helper.ReadMemoryValue(moneyAddress, ProcessName));
Thread.Sleep(1000);
}
}
}
}
// Helper.cs
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace RedAlert2
{
public abstract class Helper
{
#region API
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory
(
IntPtr hProcess,
IntPtr lpBaseAddress,
IntPtr lpBuffer,
int nSize,
IntPtr lpNumberOfBytesRead
);
[DllImportAttribute("kernel32.dll")]
public static extern bool WriteProcessMemory
(
IntPtr hProcess,
IntPtr lpBaseAddress,
int[] lpBuffer,
int nSize,
IntPtr lpNumberOfBytesWritten
);
[DllImportAttribute("kernel32.dll")]
public static extern IntPtr OpenProcess
(
int dwDesiredAccess,
bool bInheritHandle,
int dwProcessId
);
[DllImport("kernel32.dll")]
private static extern void CloseHandle
(
IntPtr hObject
);
#endregion
#region 方法
/// <summary>
/// 根據視窗標題擷取PID
/// </summary>
/// <param name="windowTitle">視窗標題</param>
/// <returns></returns>
public static int GetPidByTitle(string windowTitle)
{
int rs = 0;
Process[] arrayProcess = Process.GetProcesses();
foreach (Process p in arrayProcess)
{
if (p.MainWindowTitle.IndexOf(windowTitle) != -1)
{
rs = p.Id;
break;
}
}
return rs;
}
/// <summary>
/// 根據進程名擷取PID
/// </summary>
/// <param name="processName">進程名字</param>
/// <returns></returns>
public static int GetPidByProcessName(string processName)
{
Process[] arrayProcess = Process.GetProcessesByName(processName);
foreach (Process p in arrayProcess)
{
return p.Id;
}
return 0;
}
/// <summary>
/// 根據視窗標題尋找視窗控制代碼
/// </summary>
/// <param name="title">視窗標題</param>
/// <returns></returns>
public static IntPtr FindWindow(string title)
{
Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
if (p.MainWindowTitle.IndexOf(title) != -1)
{
return p.MainWindowHandle;
}
}
return IntPtr.Zero;
}
/// <summary>
/// 讀取記憶體中的值
/// </summary>
/// <param name="baseAddress">地址</param>
/// <param name="processName">進程名</param>
/// <returns></returns>
public static int ReadMemoryValue(int baseAddress, string processName)
{
try
{
var buffer = new byte[4];
IntPtr byteAddress = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); //擷取緩衝區地址
IntPtr hProcess = OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName));
ReadProcessMemory(hProcess, (IntPtr) baseAddress, byteAddress, 4, IntPtr.Zero); //將制定記憶體中的值讀入緩衝區
CloseHandle(hProcess);
return Marshal.ReadInt32(byteAddress);
}
catch
{
return 0;
}
}
/// <summary>
/// 將值寫入指定記憶體位址中
/// </summary>
/// <param name="baseAddress">地址</param>
/// <param name="processName">進程名</param>
/// <param name="value"></param>
public static void WriteMemoryValue(int baseAddress, string processName, int value)
{
IntPtr hProcess = OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName)); //0x1F0FFF 最高許可權
WriteProcessMemory(hProcess, (IntPtr) baseAddress, new[] {value}, 4, IntPtr.Zero);
CloseHandle(hProcess);
}
#endregion
}
}
運行介面:
檔案下載:
孤楓紅警1006下載(紅色警戒2標準版)下載:http://www.myra2.com/html/hongjingxiazai/ra2/20111015/4929.html
外掛源碼(VS2010項目)下載:1 2 備用地地址3