LearnWordManage/AsposeHook/Utils.cs

106 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ExtractResFile.AsposeHook
{
internal static class Utils
{
private static bool _enableLog;
public static void EnableLog()
{
_enableLog = true;
}
public static void DisableLog()
{
_enableLog = false;
}
public static void LogWrite(string msg)
{
if (_enableLog)
{
Console.Write(msg);
}
}
public static void LogWriteLine(string msg, ConsoleColor forgroundColor = ConsoleColor.Gray)
{
if (_enableLog)
{
Console.ForegroundColor = forgroundColor;
Console.WriteLine(msg);
Console.ResetColor();
}
}
public static string To16String(this IntPtr address)
{
if (Environment.Is64BitProcess)
{
return Convert.ToString(address.ToInt64(), 16).PadLeft(16, '0');
}
return Convert.ToString(address.ToInt32(), 16).PadLeft(8, '0');
}
public static IntPtr ReadIntPtr(this IntPtr address)
{
return Marshal.ReadIntPtr(address);
}
public static void ShowMethodInfo(MethodMeta meta)
{
if (_enableLog)
{
Console.WriteLine();
Console.Write("{0,-19}", "Method:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0,-15}", meta.Method.Name);
Console.ResetColor();
Console.Write("{0,-19}", "MethodDesc Addr:");
Console.WriteLine("{0}", meta.MethodDescAddress.To16String());
Console.ResetColor();
Console.Write("{0,-19}", "MethodDesc Value:");
Console.WriteLine("{0}", meta.MethodDescValue.To16String());
Console.ResetColor();
Console.Write("{0,-19}", "GetFunctionPointer:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0} => ", meta.FunctionPointerBeforePrepare.To16String());
if (meta.FunctionPointerBeforePrepare != meta.FunctionPointerAfterPrepare)
{
Console.ForegroundColor = ConsoleColor.Green;
}
Console.WriteLine("{0}", meta.FunctionPointerAfterPrepare.To16String());
Console.ResetColor();
Console.Write("{0,-19}", "IntPtr1:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0} => ", meta.Next1IntptrBeforePrepare.To16String());
if (meta.Next1IntptrBeforePrepare != meta.Next1IntptrAfterPrepare)
{
Console.ForegroundColor = ConsoleColor.Green;
}
Console.WriteLine("{0}", meta.Next1IntptrAfterPrepare.To16String());
Console.ResetColor();
Console.Write("{0,-19}", "IntPtr2:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0} => ", meta.Next2IntptrBeforePrepare.To16String());
if (meta.Next2IntptrBeforePrepare != meta.Next2IntptrAfterPrepare)
{
Console.ForegroundColor = ConsoleColor.Green;
}
Console.WriteLine("{0}", meta.Next2IntptrAfterPrepare.To16String());
Console.ResetColor();
}
}
}
}