所以我遇到一个非常奇怪的问题,我的小测试应用程序.而且,由于问题,我的意思是它崩溃了…很难.没有例外(至少没有什么可以抓住)被抛出,只是“Blah Blah已经停止响应…”消息.当我以x64,发行模式和IDE外部运行应用程序时,它才会崩溃.如果我以x86模式运行,或者如果我在x64中运行它,或者我在x64中独立运行它作为DEBUG,它可以正常运行.
我缩小了我的p / invoke PeekMessage调用.所以,我需要这里的辉煌的头脑,看看我写的废话,告诉我,如果我做的正确.因为,认真的,我即将失去我的国王心灵.我已经在2台电脑上试过了,他们都表现出相同的行为.我有点担心这可能是.NET 4.0的错误.
无论如何,这是我的p / invoke代码.请让我知道,如果你看到任何奇怪或只是平淡无奇:
这是PeekMessage的电话:
- private static bool PeekMessage()
- {
- MSG message = new MSG(); // Message to retrieve.
- return Win32API.PeekMessage(ref message,IntPtr.Zero,PeekMessageFlags.NoRemove);
- }
这里是PeekMessage(注意:抑制安全属性在类定义上,因此正在应用):
- [return: MarshalAs(UnmanagedType.Bool)]
- [DllImport("User32.dll",CharSet=CharSet.Auto)]
- public static extern bool PeekMessage(ref MSG msg,IntPtr hwnd,uint wFilterMin,uint wFilterMax,PeekMessageFlags flags);
这是MSG:
- [StructLayout(LayoutKind.Sequential)]
- internal struct MSG
- {
- /// <summary>Window handle.</summary>
- public IntPtr hwnd;
- /// <summary>Message to process.</summary>
- public uint Message;
- /// <summary>Window message parameter 1.</summary>
- public uint wParam;
- /// <summary>Window message parameter 2.</summary>
- public uint lParam;
- /// <summary>Time message was sent?</summary>
- public uint time;
- /// <summary>Mouse pointer position.</summary>
- public Point pt;
- }
最后,PeekMessageFlags:
- internal enum PeekMessageFlags
- {
- /// <summary>Keep message on the message queue.</summary>
- NoRemove = 0,/// <summary>Remove message from the queue.</summary>
- Remove = 1,/// <summary>Do not yield execution to waiting threads.</summary>
- NoYield = 2
- }
我检查了事件日志,我得到了:
- Faulting application name: Tester_Graphics.exe,version: 1.0.0.0,time stamp: 0x4ec0ba85
- Faulting module name: unknown,version: 0.0.0.0,time stamp: 0x00000000
- Exception code: 0xc0000005
- Fault offset: 0x00000000000001cb
- Faulting process id: 0x1260
- Faulting application start time: 0x01cca299e2c21a77
- Faulting application path: D:\Code\Current\Gorgon\Branches\2.x\Dorian\Examples\Tester_Graphics\bin\Release\Tester_Graphics.exe
- Faulting module path: unknown
- Report Id: 20ac891f-0e8d-11e1-a5d7-bcaec5753ddd
所以,如果你看到不正确的事情,请让我知道.我讨厌这不是我的错.
对不起,如果它不够详细,如果你需要更多的信息,只要留言.
解决方法
MSG的lParam和wParam字段的大小是错误的.你应该使用IntPtr而不是uint / int.
如果你看看Windows Data Types你可以看到:
> LPARAM是一个LONG_PTR,即32位平台的32位,64位平台上的64位.
> PARAM是一个UINT_PTR,它在32位平台上大小为32位,64位平台的大小为64位.
相反,int和uint类型都是32位大小,无论平台如何,这意味着在64位平台上,您的MSG结构体的64位太小,这将导致某种内存损坏.