Calling a Function with a Structure Parameter : Native Windows Function « Windows « C# / CSharp Tutorial






// Code from 
// A Programmer's Introduction to C# 2.0, Third Edition
// copyright 2000 Eric Gunnerson

using System;
using System.Runtime.InteropServices;

struct Point
{
    public int x;
    public int y;
    
    public override string ToString()
    {
        return(String.Format("({0}, {1})", x, y));
    }
}

struct Rect
{
    public int left;
    public int top;
    public int right;
    public int bottom;
    
    public override string ToString()
    {
        return(String.Format("({0}, {1})\n    ({2}, {3})", left, top, right, bottom));
    }
}

struct WindowPlacement
{
    public uint length;
    public uint flags;
    public uint showCmd;
    public Point minPosition;
    public Point maxPosition;
    public Rect normalPosition;    
    
    public override string ToString()
    {
        return(String.Format("min, max, normal:\n{0}\n{1}\n{2}",
        minPosition, maxPosition, normalPosition));
    }
}

class MainClass
{
    [DllImport("user32")]
    static extern IntPtr GetForegroundWindow();
    
    [DllImport("user32")]
    static extern bool GetWindowPlacement(IntPtr handle, ref WindowPlacement wp);
    
    public static void Main()
    {
        IntPtr window = GetForegroundWindow();
        
        WindowPlacement wp = new WindowPlacement();
        wp.length = (uint) Marshal.SizeOf(wp);
        
        bool result = GetWindowPlacement(window, ref wp);
        
        if (result)
        {
            Console.WriteLine(wp);
        }
    } 
}
min, max, normal:
(-32000, -32000)
(-60, -8)
(-1, -1)
    (1273, 728)








29.10.Native Windows Function
29.10.1.Calling Native DLL Functions
29.10.2.Calling a Function with a Structure Parameter
29.10.3.Enumerate Display Monitors
29.10.4.Get Workstation information
29.10.5.Get Computer name (char * parameter)
29.10.6.Get free disk space
29.10.7.Use native windows function to read file
29.10.8.The windows version information
29.10.9.Get current Active Window
29.10.10.Writing INI file: Write Private Profile String
29.10.11.Reading INI file: Get Private Profile String
29.10.12.GetVersionEx by using kernel32.dll
29.10.13.Get computer name (StringBuilder parameter)
29.10.14.Lock work station
29.10.15.Get Monitor Information