Game Development Reference
In-Depth Information
public struct Message
{
public IntPtr hWnd;
public Int32 msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
Adding this C struct is just a case of correctly matching the C types to the C#
types. The attribute
[StructLayout(LayoutKind.Sequential)]
tells
C# to lay out the structure in memory exactly the way it's written. Without
this attribute, C# might try to be clever and make the structure more memory-
efficient by rearranging it. C expects the structure to be laid out in memory the
exact way it's written.
Now the message type is imported; all that remains is to import the
PeekMes-
sage
function.
public class FastLoop
{
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("User32.dll", CharSet
¼
CharSet.Auto)]
public static extern bool PeekMessage(
out Message msg,
IntPtr hWnd,
uint messageFilterMin,
uint messageFilterMax,
uint flags);
The first attribute,
[System.Security.SuppressUnmanagedCodeSe-
curity]
, just says, ''We are calling C, an unmanaged language, so don't do any
managed security checks.'' The second attribute,
[DllImport("User32.
dll", CharSet
¼
CharSet.Auto)],
references the DLL file that the
C function is to be imported from.
User32.dll
is one of the major files
for interacting with the Windows operating system using C. The
PeekMessage
function fills out the
Message
structure, so it needs to be able to write
to it. That's why the
out
keyword is used on the first argument. The
remaining arguments can all be ignored and no useful information will be passed
into them.
Search WWH ::
Custom Search