Mik*_*ike 10 .net c# pinvoke visual-studio-2010 windows-7-x64
所以,我的小测试应用程序遇到了一个非常奇怪的问题.问题是,我的意思是崩溃......很难.没有例外(至少,我没有抓到任何东西)被抛出,只是"Blah Blah已经停止响应......"的消息.它崩溃只有当我在运行应用程序64,发行方式和外界的IDE.如果我在x86模式下运行它,或者我在x64中的IDE中运行它,或者我在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, 0, 0, PeekMessageFlags.NoRemove);
}
Run Code Online (Sandbox Code Playgroud)
这是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);
Run Code Online (Sandbox Code Playgroud)
这是味精:
[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;
}
Run Code Online (Sandbox Code Playgroud)
最后,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
}
Run Code Online (Sandbox Code Playgroud)
我检查了事件日志,我得到了这个:
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
Run Code Online (Sandbox Code Playgroud)
所以,如果你看到任何不对的东西,请告诉我.我讨厌这不是我的错.
对不起,如果不够详细,如果您需要更多信息,请留言.
大小lParam和wParam字段MSG是错误的.你应该使用IntPtr而不是uint/ int.
如果您查看Windows数据类型,您可以看到:
LPARAM是LONG_PTR32位平台的32位大小,64位平台大小为64位.PARAM是UINT_PTR这又是在32个平台大小32位和64个平台的大小是64位.相反,无论平台如何,int并且uint类型都是32位大小,这意味着在64位平台上,您的结构体是64位太小,这将导致某种类型的内存损坏.MSG
| 归档时间: |
|
| 查看次数: |
1281 次 |
| 最近记录: |