使用C#调用EasyHook来Hook系统API实现游戏加速,宿主进程会卡死是什么原因?如何排查?

Kasimodo2009 2015-02-16 11:37:11
部分代码如下:

public static double SpeedUp = 1.0;
public static int StoredRealTime = 0;
public static int StoredFakeTime = 0;
public LocalHook QueryPerformanceCounterHook = null;

private bool SetSpeedUp(double NewSpeedUp)
{
SpeedUp = NewSpeedUp;
if (QueryPerformanceCounterHook == null)
{
QueryPerformanceCounterHook = LocalHook.Create(LocalHook.GetProcAddress("kernel32.dll", "QueryPerformanceCounter"), new QueryPerformanceCounterDelegate(QueryPerformanceCounterEx), this);
QueryPerformanceCounterHook.ThreadACL.SetExclusiveACL(new Int32[0]);
}
}

static bool QueryPerformanceCounterEx(out long FakeTime)
{
bool result;
long RealTime = 0;
int TickCount = timeGetTime();
result = QueryPerformanceCounter(out RealTime);

if (StoredRealTime == 0) // Initialize
{
StoredRealTime = RealTime;
StoredFakeTime = TickCount;
}

FakeTime = StoredFakeTime + (long)((RealTime - StoredRealTime) * SpeedUp);

StoredRealTime = RealTime;
StoredFakeTime = FakeTime;
return result;
}

[DllImport("Winmm.dll")]
static extern int timeGetTime();

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
delegate bool QueryPerformanceCounterDelegate(out long lpPerformanceCount);

[DllImport("kernel32.dll")]
static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
...全文
610 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinat_21947233 2015-02-27
  • 打赏
  • 举报
回复
分给我把
Kasimodo2009 2015-02-27
  • 打赏
  • 举报
回复
问题已解决,我自己说说吧!问题出在timeGetTime和QueryPerformanceCounter两个API函数有冲突,具体为什么我也不太清楚,但是注释掉第20行代码,初始化的StoredFakeTime用RealTime赋值就好了,希望有高手给出具体原因!
Kasimodo2009 2015-02-16
  • 打赏
  • 举报
回复
没人知道吗?第一次来CSDN发帖,不会让我失望吧……

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧