Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bbepis committed Mar 30, 2016
1 parent 040e095 commit 0017728
Show file tree
Hide file tree
Showing 9 changed files with 2,142 additions and 0 deletions.
22 changes: 22 additions & 0 deletions AA2TranslationLoader.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AA2TranslationLoader", "AA2TranslationLoader\AA2TranslationLoader.csproj", "{C2833569-5FE1-41CE-AB8B-32A56C2D8422}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C2833569-5FE1-41CE-AB8B-32A56C2D8422}.Debug|x86.ActiveCfg = Debug|x86
{C2833569-5FE1-41CE-AB8B-32A56C2D8422}.Debug|x86.Build.0 = Debug|x86
{C2833569-5FE1-41CE-AB8B-32A56C2D8422}.Release|x86.ActiveCfg = Release|x86
{C2833569-5FE1-41CE-AB8B-32A56C2D8422}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
56 changes: 56 additions & 0 deletions AA2TranslationLoader/AA2TranslationLoader.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{C2833569-5FE1-41CE-AB8B-32A56C2D8422}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<OutputType>Exe</OutputType>
<AssemblyName>AA2TranslationLoader</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<WarningLevel>4</WarningLevel>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x86' ">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>bin\Release\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Resources\icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject>AA2TranslationLoader.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ListViewHeaderHelper.cs" />
<Compile Include="Program.cs" />
<Compile Include="TabControlHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<Content Include="dictionary.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Resources\icon.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
210 changes: 210 additions & 0 deletions AA2TranslationLoader/ListViewHeaderHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace AA2TranslationLoader
{
internal class ListViewHeaderHelper
{
public struct HDITEM
{
public uint mask;

public int cxy;

public IntPtr pszText;

public IntPtr hbm;

public int cchTextMax;

public int fmt;

public IntPtr lParam;

public int iImage;

public int iOrder;

public uint type;

public IntPtr pvFilter;

public uint state;
}

private const int HDM_FIRST = 4608;

private const int HDM_GETITEM = 4611;

private const int HDM_SETITEM = 4612;

private const int HDM_GETITEMCOUNT = 4608;

private const uint HDI_TEXT = 2u;

private const uint PROCESS_ALL_ACCESS = 2035711u;

private const uint MEM_COMMIT = 4096u;

private const uint MEM_RELEASE = 32768u;

private const uint PAGE_READWRITE = 4u;

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int message, int wParam, IntPtr lParam);

[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam);

[DllImport("kernel32")]
private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);

[DllImport("kernel32")]
private static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);

[DllImport("kernel32")]
private static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint dwFreeType);

[DllImport("kernel32")]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref ListViewHeaderHelper.HDITEM lvColumnBuffer, int dwSize, IntPtr lpNumberOfBytesWritten);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, IntPtr lpNumberOfBytesWritten);

[DllImport("kernel32")]
private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, IntPtr lpNumberOfBytesRead);

[DllImport("kernel32")]
private static extern bool CloseHandle(IntPtr hObject);

public static int GetColumnCount(IntPtr hWnd)
{
return (int)ListViewHeaderHelper.SendMessage(hWnd, 4608, 0, IntPtr.Zero);
}

public static void SetColumnHeaderText(IntPtr hWnd, int processId, int tabIndex, string newText, string targetEncoding)
{
if (!string.IsNullOrEmpty(newText))
{
IntPtr intPtr = IntPtr.Zero;
IntPtr intPtr2 = IntPtr.Zero;
IntPtr zero = IntPtr.Zero;
int num = Marshal.SizeOf(typeof(ListViewHeaderHelper.HDITEM));
try
{
intPtr = ListViewHeaderHelper.OpenProcess(2035711u, false, processId);
if (intPtr == IntPtr.Zero)
{
throw new Exception("OpenProcess failed");
}
intPtr2 = ListViewHeaderHelper.VirtualAllocEx(intPtr, IntPtr.Zero, 1024, 4096u, 4u);
if (intPtr2 == IntPtr.Zero)
{
throw new Exception("VirtualAllocEx failed");
}
ListViewHeaderHelper.HDITEM hDITEM = default(ListViewHeaderHelper.HDITEM);
hDITEM.mask = 2u;
hDITEM.pszText = (IntPtr)(intPtr2.ToInt32() + num);
hDITEM.cchTextMax = 255;
if (!ListViewHeaderHelper.WriteProcessMemory(intPtr, intPtr2, ref hDITEM, num, IntPtr.Zero))
{
throw new Exception(string.Format("WriteProcessMemory failed (struct copy, text={0})", newText));
}
byte[] bytes = Encoding.GetEncoding(targetEncoding).GetBytes(newText);
if (!ListViewHeaderHelper.WriteProcessMemory(intPtr, hDITEM.pszText, bytes, num, IntPtr.Zero))
{
throw new Exception(string.Format("WriteProcessMemory failed (string buffer copy, text={0})", newText));
}
ListViewHeaderHelper.SendMessage(hWnd, 4612, new IntPtr(tabIndex), intPtr2);
}
finally
{
if (zero != IntPtr.Zero)
{
Marshal.FreeHGlobal(zero);
}
if (intPtr2 != IntPtr.Zero)
{
ListViewHeaderHelper.VirtualFreeEx(intPtr, intPtr2, 0, 32768u);
}
if (intPtr != IntPtr.Zero)
{
ListViewHeaderHelper.CloseHandle(intPtr);
}
}
}
}

public static string GetColumnHeaderText(IntPtr hWnd, int processId, int colIndex, string targetEncoding)
{
IntPtr intPtr = IntPtr.Zero;
IntPtr intPtr2 = IntPtr.Zero;
IntPtr intPtr3 = IntPtr.Zero;
int num = Marshal.SizeOf(typeof(ListViewHeaderHelper.HDITEM));
string result = "";
try
{
intPtr = ListViewHeaderHelper.OpenProcess(2035711u, false, processId);
if (intPtr == IntPtr.Zero)
{
throw new Exception("OpenProcess failed");
}
intPtr3 = Marshal.AllocHGlobal(1024);
intPtr2 = ListViewHeaderHelper.VirtualAllocEx(intPtr, IntPtr.Zero, 1024, 4096u, 4u);
if (intPtr2 == IntPtr.Zero)
{
throw new Exception("VirtualAllocEx failed");
}
ListViewHeaderHelper.HDITEM hDITEM = default(ListViewHeaderHelper.HDITEM);
hDITEM.mask = 2u;
hDITEM.pszText = (IntPtr)(intPtr2.ToInt32() + num);
hDITEM.cchTextMax = 255;
if (!ListViewHeaderHelper.WriteProcessMemory(intPtr, intPtr2, ref hDITEM, num, IntPtr.Zero))
{
throw new Exception("WriteProcessMemory failed");
}
ListViewHeaderHelper.SendMessage(hWnd, 4611, new IntPtr(colIndex), intPtr2);
if (!ListViewHeaderHelper.ReadProcessMemory(intPtr, intPtr2, intPtr3, 1024, IntPtr.Zero))
{
throw new Exception("ReadProcessMemory failed");
}
byte[] array = new byte[255];
Marshal.Copy((IntPtr)(intPtr3.ToInt32() + num), array, 0, 255);
result = ListViewHeaderHelper.DecodeString(array, targetEncoding);
}
finally
{
if (intPtr3 != IntPtr.Zero)
{
Marshal.FreeHGlobal(intPtr3);
}
if (intPtr2 != IntPtr.Zero)
{
ListViewHeaderHelper.VirtualFreeEx(intPtr, intPtr2, 0, 32768u);
}
if (intPtr != IntPtr.Zero)
{
ListViewHeaderHelper.CloseHandle(intPtr);
}
}
return result;
}

private static string DecodeString(byte[] buffer, string targetEncoding)
{
int num = Array.IndexOf<byte>(buffer, 0, 0);
if (num < 0)
{
num = buffer.Length;
}
return Encoding.GetEncoding(targetEncoding).GetString(buffer, 0, num);
}

public static string ByteArrayToHexRepresentation(byte[] ba)
{
string text = BitConverter.ToString(ba);
return text.Replace("-", "");
}
}
}
Loading

0 comments on commit 0017728

Please sign in to comment.