1

I'm using EasyHook, a C# library for injecting and detouring functions from unmanaged applications. I'm trying to hook onto GetDlgItemTextA, which takes the arguments:

UINT WINAPI GetDlgItemText(
  __in   HWND hDlg,
  __in   int nIDDlgItem,
  __out  LPTSTR lpString, 
  __in   int nMaxCount
);`

In my hook, I am casting it as:

[DllImport("user32.dll",
// CharSet = CharSet.Unicode,
SetLastError = true,
CallingConvention = CallingConvention.StdCall)]
static extern uint GetDlgItemTextA(IntPtr hWin, int nIDDlgItem, StringBuilder text, int MaxCount);

And my hook is:

static uint DGetDlgItemText_Hooked(IntPtr hWin, int nIDDlgItem, StringBuilder text, int MaxCount)
{
    // call original API...
    uint ret = GetDlgItemTextA(hWin, nIDDlgItem, text, MaxCount);
    MessageBox.Show(text.ToString());
    return ret;
}

Unfortunately, the moment this is called, the hooked application crashes. Is there a better cast I can use to successfully hook onto this function? Thanks!

I've compiled, editted, and confirmed the working condition of my EasyHook setup. This is just casing and hooking only.

4

1 回答 1

0

好吧,看来我的代码确实有效,但唯一的区别是我不得不在钩子中放置一个 try catch 语句,原因不明。StringBuilder 确实正确地从 LPCSTR 转换回 LPCSTR,并且程序读取它就好了。该程序现在没有崩溃,所以我想我会添加这个作为我自己的答案。

于 2010-06-12T16:19:58.090 回答