当我的应用程序在x86上运行时,程序运行正常。当我在x64上运行它时,发生访问冲突异常。异常细节如下所述。
“ System.AccessViolationException:'试图读取或写入受保护的内存。这通常表明其他内存已损坏。'
我认为问题出在编组。我是否必须手动封送结构?需要帮助。我希望x64和x86具有相同的结构。 我的本机方法定义如下。
[StructLayout(LayoutKind.Sequential)]
internal struct CWPSTRUCT
{
internal IntPtr lParam;
internal IntPtr wParam;
internal int message;
internal IntPtr hwnd;
}
CallNextHookEx是,
[DllImport("user32", CharSet=CharSet.Auto, ExactSpelling=true)]
extern public static IntPtr CallNextHookEx(IntPtr hhook, int code, int wparam, int lparam) ;
还有发生异常的方法
private IntPtr MessageHookProc(int nCode, int wParam, int lParam, ref bool processMessage)
{
GC.KeepAlive(this);
if (nCode >= 0 && !this.DisableMessageHook)
{
IMessageFilter imf = client as IMessageFilter;
if (imf != null)
{
**CWPSTRUCT cwp =
(CWPSTRUCT)(Marshal.PtrToStructure((IntPtr)lParam, typeof(CWPSTRUCT)));**
Message msg = Message.Create(cwp.hwnd, cwp.message, cwp.wParam, cwp.lParam);
if(imf.PreFilterMessage(ref msg))
{
processMessage = false;
return (IntPtr)1;
}
}
}
return NativeHookMethods.CallNextHookEx(this.MessageHookHandle,nCode,wParam,lParam);
}
以及使用特定方法MessageHookProc的代码,
WndProcHooker messageHooker = (WndProcHooker)this.reference.Target;
retval = messageHooker.MessageHookProc(nCode,wParam,lParam, ref processMessage);