using System; using System.Runtime.InteropServices; public class MarshalAsExample { public static void Main() { API.OSVERSIONINFO info = new API.OSVERSIONINFO(); info.dwOSVersionInfoSize = Marshal.SizeOf(info); bool resp = API.GetVersionEx(ref info); if (resp == false) { Console.WriteLine("GetVersion failed"); } Console.WriteLine("{0}.{1}.{2}", info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber); Console.WriteLine("Press [Enter] Key to end..."); Console.ReadLine(); } } public class API { [DllImport("kernel32.dll")] public static extern bool GetVersionEx(ref OSVERSIONINFO lpVersionInfo); [StructLayout(LayoutKind.Sequential)] public struct OSVERSIONINFO { public System.Int32 dwOSVersionInfoSize; public System.Int32 dwMajorVersion; public System.Int32 dwMinorVersion; public System.Int32 dwBuildNumber; public System.Int32 dwPlatformId; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)] public string szCSDVersion; } } using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class StructLayoutAttributeExample : Form { public static void Main() { StructLayoutAttributeExample obj = new StructLayoutAttributeExample(); obj.ShowScreenSize(); } private void ShowScreenSize() { API.Rect client = new API.Rect(); API.GetWindowRect(this.Handle, out client); string temp = string.Format("Using struct: Left {0}, Top {1}, Right {2}, Bottom {3}", client.left, client.top, client.right, client.bottom); API2.Rect2 client2 = new API2.Rect2(); API2.GetWindowRect(this.Handle, client2); temp += string.Format("\n\nUsing class: Left {0}, Top {1}, Right {2}, Bottom {3}", client.left, client.top, client.right, client.bottom); MessageBox.Show(temp); } } public class API { // Rect is a struct, which is a value type. Value types passed by value, by default. // To pass by pointer use "out" keyword. [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hWnd, out Rect windowRect); [StructLayout(LayoutKind.Sequential)] public struct Rect { public int left; public int top; public int right; public int bottom; } } public class API2 { // Rect2 is a class. Classes are passed by reference, by default, so they don't // need the "out" keyword. [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hWnd, Rect2 windowRect); [StructLayout(LayoutKind.Sequential)] public class Rect2 { public int left; public int top; public int right; public int bottom; } } using System; using System.Runtime.InteropServices; public class UnmanagedCallbackExample { public static void Main() { UnmanagedCallbackExample obj = new UnmanagedCallbackExample(); obj.ShowWindowHandles(); } public void ShowWindowHandles() { API.EnumWindows(new API.APICallback(GetWindowHandle), 0); Console.WriteLine("Press [Enter] Key to end..."); Console.ReadLine(); } private bool GetWindowHandle(int hWnd, int lParam) { Console.WriteLine("{0:0000000}", hWnd); return true; } } public class API { public delegate bool APICallback(int hWnd, int lParam); [DllImport("user32.dll")] public static extern bool EnumWindows(APICallback lpEnumFunc, System.Int32 lParam); }