How to correctly hook and return GetDlgItemTextA from C++ to C# to C++ from EasyHook
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开发者_StackOverflow中文版 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.
Well, it appears that the code I had did work, but the the only difference I had to put a try catch statement in the hook for an unknown reason. StringBuilder does correctly get converted from LPCSTR and back to LPCSTR, and the program reads it just fine. The program does not crash now, so I thought I would add this as my own answer.
精彩评论