Memory API
using System;
using System.Runtime.InteropServices;
namespace _051_Memory_API
{
//[StructLayout(LayoutKind.Sequential)]
// struct MEMORYSTATUSEX
//{
// public uint dwLength;
// public uint dwMemoryLoad;
// public ulong ullTotalPhys;
// public ulong ullAvailPhys;
// public ulong ullTotalPageFile;
// public ulong ullAvailPageFile;
// public ulong ullTotalVirtual;
// public ulong ullAvailVirtual;
// public ulong ullAvailExtendedVirtual;
//}
// Use this version of the struct to generate an error
[StructLayout(LayoutKind.Sequential )]
struct MEMORYSTATUSEX
{
public ulong dwLength;
public ulong dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("kernel32.dll")]
static extern bool GlobalMemoryStatusEx( ref MEMORYSTATUSEX lpBuffer);
[DllImport( "kernel32.dll")]
static extern int GetLastError();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static unsafe void Main(string[] args)
{
MEMORYSTATUSEX m = new MEMORYSTATUSEX();
m.dwLength = ( uint)sizeof(MEMORYSTATUSEX);
bool success = GlobalMemoryStatusEx( ref m);
if (success)
{
displayMemoryData(m);
}
else
{
Console.WriteLine(" Error Number " + GetLastError());
}
Console.ReadKey();
}
private static void displayMemoryData(MEMORYSTATUSEX m)
{
Console.WriteLine ("{0,-30}{1}", "dwLength",m.dwLength);
Console.WriteLine( "{0,-30}{1}%"," dwMemoryLoad",m.dwMemoryLoad);
Console.WriteLine(" {0,-30}{1} MB","ullTotalPhys ",m.ullTotalPhys/1024/ 1024);
Console.WriteLine("{0,-30}{1} MB ","ullAvailPhys" ,m.ullAvailPhys/1024/1024 );
Console.WriteLine("{0,-30}{1} MB" ,"ullTotalPageFile",m.ullTotalPageFile /1024/1024);
Console.WriteLine("{0,-30}{1} MB", "ullAvailPageFile",m.ullAvailPageFile/ 1024/1024);
Console.WriteLine( "{0,-30}{1} MB"," ullTotalVirtual",m.ullTotalVirtual/1024 /1024);
Console.WriteLine("{0,-30}{1} MB ","ullAvailVirtual ",m.ullAvailVirtual/1024/ 1024);
Console.WriteLine("{0,-30}{1} MB ","ullAvailExtendedVirtual" ,m.ullAvailExtendedVirtual/1024/1024 );
}
}
}
using System;
using System.Runtime.InteropServices;
namespace _051_Memory_API
{
[StructLayout(LayoutKind.Sequential)]
struct MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
}
// Use this version of the struct to generate an error
//[StructLayout(LayoutKind.Sequential )]
//struct MEMORYSTATUSEX
//{
// public ulong dwLength;
// public ulong dwMemoryLoad;
// public ulong ullTotalPhys;
// public ulong ullAvailPhys;
// public ulong ullTotalPageFile;
// public ulong ullAvailPageFile;
// public ulong ullTotalVirtual;
// public ulong ullAvailVirtual;
// public ulong ullAvailExtendedVirtual;
//}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GlobalMemoryStatusEx( ref MEMORYSTATUSEX lpBuffer);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
MEMORYSTATUSEX m = new MEMORYSTATUSEX();
m.dwLength = (uint)Marshal.SizeOf(m);
bool success = GlobalMemoryStatusEx(ref m);
if (success)
{
displayMemoryData(m);
}
else
{
Console.WriteLine("Error Number " + Marshal.GetLastWin32Error());
}
}
private static void displayMemoryData(MEMORYSTATUSEX m)
{
Console.WriteLine("{0,-30}{1} ","dwLength ",m.dwLength);
Console.WriteLine("{0,-30}{1}% ","dwMemoryLoad" ,m.dwMemoryLoad);
Console.WriteLine("{0,-30}{1} MB" ,"ullTotalPhys",m.ullTotalPhys /1024/1024);
Console.WriteLine( "{0,-30}{1} MB", "ullAvailPhys",m.ullAvailPhys/ 1024/1024);
Console.WriteLine(" {0,-30}{1} MB"," ullTotalPageFile",m.ullTotalPageFile/1024 /1024);
Console.WriteLine(" {0,-30}{1} MB","ullAvailPageFile ",m.ullAvailPageFile/1024/ 1024);
Console.WriteLine("{0,-30}{1} MB ","ullTotalVirtual" ,m.ullTotalVirtual/1024/1024 );
Console.WriteLine("{0,-30}{1} MB" ,"ullAvailVirtual",m.ullAvailVirtual /1024/1024);
Console.WriteLine("{0,-30}{1} MB", "ullAvailExtendedVirtual",m.ullAvailExtendedVirtual /1024/1024);
}
}
}
Windows Message
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text;
using System.Collections;
namespace _045_ID_Oracle
{
public enum windowTypes : uint
{
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1 ,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6,
};
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("user32.dll ")]
private static extern IntPtr GetWindow(IntPtr hWnd, windowTypes uCmd);
[ DllImport(" user32.dll") ]
private static extern IntPtr GetNextDlgTabItem
(IntPtr hDlg, IntPtr hCtl, bool bPrevious);
[ DllImport("user32.dll ") ]
public static extern int GetDlgCtrlID(IntPtr hCtl);
[ DllImport("user32.dll" ) ]
private static extern int GetDlgItemText(
IntPtr hDlg,
int nIDDlgItem,
StringBuilder lpString,
int nMaxCount
);
[ DllImport("user32.dll") ]
private static extern IntPtr GetNextDlgGroupItem
(IntPtr hDlg, IntPtr hCtl, bool bPrevious);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string [] args)
{
string formatter = "{0,-20}{1,-20:x}{2,-20}";
Process np = new Process();
np.StartInfo.FileName = "notepad ";
np.Start();
np.WaitForInputIdle(10000);
SendKeys.SendWait("dummy text");
np.WaitForInputIdle(10000);
SendKeys.SendWait(" ^f");
np.WaitForInputIdle(10000);
SendKeys.SendWait("dummy text" );
IntPtr hDlg =
GetWindow(np.MainWindowHandle, windowTypes.GW_ENABLEDPOPUP);
ArrayList controlHandles = new ArrayList();
if (hDlg != IntPtr.Zero)
{
IntPtr firstHandle = GetNextDlgTabItem(hDlg, IntPtr.Zero, false);
IntPtr hCtl = firstHandle;
if (hCtl != IntPtr.Zero)
{
// tab through all of the controls on this form
do
{
// if this control is part of a group, get all the group members
IntPtr subHandle = hCtl;
do
{
// don't allow duplicates
if ( !controlHandles.Contains(hCtl))
{
controlHandles.Add(hCtl);
}
hCtl = GetNextDlgGroupItem(hDlg, hCtl, false);
} while (hCtl != subHandle);
hCtl = GetNextDlgTabItem(hDlg, hCtl, false);
} while (hCtl != firstHandle);
Console.WriteLine(formatter,"Caption", "ID value in Hex", "Handle");
Console.WriteLine(formatter, "-------"," ---------------","------ ");
StringBuilder caption = new StringBuilder();
foreach (IntPtr handle in controlHandles)
{
int Id = GetDlgCtrlID(handle);
GetDlgItemText(hDlg, Id, caption, caption.Capacity);
Console.WriteLine(formatter,caption,Id,handle);
}
}
}
Console.ReadKey();
np.Kill();
}
}
}
using System;
using System.Diagnostics;
using System.Windows.Forms ;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace CheckBoxWithID
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
public enum ids : int {
MatchCase = 0x411, Direction = 0x430 , Up = 0x420, Down = 0x421}
public enum windowTypes : uint
{
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4 ,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6,
};
[DllImport( "user32.dll")]
private static extern int SendNotifyMessage(
IntPtr hWndControl, // handle to destination control
uint msg, // message ID
IntPtr wParam, // checkbox/radio button state
StringBuilder lParam); // pointer to null terminated string
[DllImport( "user32.dll")]
private static extern int PostMessage(
IntPtr hWndControl, // handle to destination control
uint msg, // message ID
IntPtr wParam, // checkbox/radio button state
StringBuilder lParam); // pointer to null terminated string
private const uint BM_SETCHECK = 0x00F1;
private const int BST_CHECKED = 0x0001;
private const int BST_UNCHECKED = 0x0000;
public enum buttonSetting : int {on = BST_CHECKED, off = BST_UNCHECKED}
[ DllImport( "user32.dll") ]
private static extern IntPtr GetWindow (IntPtr hWnd, windowTypes uCmd);
[ DllImport("user32.dll" ) ]
public static extern IntPtr GetDlgItem(
IntPtr hDlg, int nIDDlgItem );
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string [] args)
{
Process p = new Process();
p.StartInfo.FileName = " notepad";
p.Start();
p.WaitForInputIdle(10000 );
SendKeys.SendWait("Dummy Input" );
p.WaitForInputIdle(10000);
SendKeys.SendWait(" ^f");
p.WaitForInputIdle(10000);
IntPtr hDlg = GetWindow(
p.MainWindowHandle, windowTypes.GW_ENABLEDPOPUP);
IntPtr hCtl = GetDlgItem(hDlg, (int)ids.MatchCase);
RadioAndCheckBoxClick(hCtl, buttonSetting.on);
hCtl = GetDlgItem(hDlg, (int )ids.Up);
RadioAndCheckBoxClick(hCtl, buttonSetting.on);
hCtl = GetDlgItem(hDlg, ( int)ids.Down);
RadioAndCheckBoxClick(hCtl, buttonSetting.off);
Thread.Sleep(5000 );
Console.ReadKey();
p.Kill();
}
public static void RadioAndCheckBoxClick(IntPtr hCtl, buttonSetting state)
{
PostMessage( // SendNotifyMessage will work too
hCtl, // handle to destination control
BM_SETCHECK, // message ID
(IntPtr)state, // new state for radio button or checkbox
null // = 0; not used, must be zero
);
}
}
}
using System;
using System.Diagnostics;
using System.Runtime.InteropServices ;
using System.Windows.Forms;
using System.Text ;
using System.Threading;
using System.Collections ;
namespace _043_Button_Click
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
const int NORMALWAIT = 10000;
Process p = new Process();
p.StartInfo.FileName = "notepad";
p.Start();
p.WaitForInputIdle(NORMALWAIT);
// Bring up the "About Notepad Window"
SendKeys.SendWait("%h");
p.WaitForInputIdle(NORMALWAIT);
SendKeys.SendWait("a" );
p.WaitForInputIdle(NORMALWAIT);
// Locate the Dialog Box
IntPtr hWnd =
ControlHelper.GetDialog(p.MainWindowHandle, "About Notepad");
if (hWnd != IntPtr.Zero)
{
// locate the OK button, and click it
IntPtr buttonHwnd = ControlHelper.GetItemHandleByCaption(hWnd,"OK" );
if (buttonHwnd != IntPtr.Zero)
{
Console.WriteLine("About to click the OK button ");
Thread.Sleep(NORMALWAIT/3 );
ControlHelper.ButtonClick(buttonHwnd);
Console.WriteLine("Done clicking the OK button ");
Thread.Sleep(NORMALWAIT/ 3);
}
}
p.CloseMainWindow();
}
}
class ControlHelper
{
// Enumeration used to find the dialog box
public enum windowTypes : uint
{
GW_HWNDFIRST = 0 ,
GW_HWNDLAST = 1 ,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6,
}
// Constant value representing the Button Click Windows Message
private const uint BM_CLICK = 0x00F5;
// Windows API DLL Imports
[DllImport("user32.dll ")]
private static extern IntPtr GetWindow(IntPtr hWnd, windowTypes uCmd);
[DllImport(" user32", EntryPoint= "GetWindowText")]
public static extern int GetWindowText(
IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32" )]
public static extern int GetWindowTextLength(
IntPtr hWnd);
[ DllImport( "user32.dll") ]
public static extern int GetDlgCtrlID(IntPtr hwndCtl);
[ DllImport(" user32.dll") ]
private static extern IntPtr GetNextDlgTabItem
(IntPtr hDlg, IntPtr hCtl, bool bPrevious);
[ DllImport("user32.dll ") ]
private static extern int GetDlgItemText(
IntPtr hDlg,
int nIDDlgItem,
StringBuilder lpString,
int nMaxCount
);
[DllImport("user32.dll ")]
private static extern int SendNotifyMessage(
IntPtr hWnd,
uint Msg,
IntPtr wParam,
StringBuilder lParam
);
[DllImport( "user32.dll")]
private static extern int PostMessage(
IntPtr hWnd,
uint Msg,
IntPtr wParam,
StringBuilder lParam
);
[ DllImport("user32.dll ") ]
private static extern IntPtr GetNextDlgGroupItem
(IntPtr hDlg, IntPtr hCtl, bool bPrevious);
// Wrappers for the Windows API functions
public static string WindowCaption(IntPtr hWnd)
{
int length = GetWindowTextLength(hWnd) + 1;
StringBuilder caption = new StringBuilder(length);
int result =
GetWindowText(hWnd, caption, caption.Capacity);
if (result != 0)
{
return caption.ToString();
}
else
{
return "";
}
}
public static IntPtr GetDialog(IntPtr hWnd, string caption)
{
IntPtr hDlg = GetWindow(hWnd, windowTypes.GW_ENABLEDPOPUP);
if (hDlg != IntPtr.Zero && WindowCaption(hDlg) == caption)
{
return hDlg;
}
else
{
return IntPtr.Zero;
}
}
// Get all of the control handles on the designed dialog box
public static IntPtr[] GetHandles(IntPtr hDlg)
{
ArrayList controlHandles = new ArrayList();
IntPtr[] result = null ;
if (hDlg != IntPtr.Zero)
{
IntPtr firstHandle = GetNextDlgTabItem(hDlg, IntPtr.Zero, false );
IntPtr hCtl = firstHandle;
if (hCtl != IntPtr.Zero)
{
// tab through all of the controls on this form
do
{
// if this control is part of a group, get all the group members
IntPtr subHandle = hCtl;
do
{
// don't allow duplicates
if ( !controlHandles.Contains(hCtl))
{
controlHandles.Add(hCtl);
}
hCtl = GetNextDlgGroupItem(hDlg, hCtl, false);
} while (hCtl != subHandle);
hCtl = GetNextDlgTabItem(hDlg, hCtl, false);
} while (hCtl != firstHandle);
}
result = (IntPtr[])controlHandles.ToArray(typeof (IntPtr));
}
return result;
}
public static IntPtr GetItemHandleByCaption(IntPtr hDlg, string caption)
{
IntPtr[] itemHandlesArray = GetHandles(hDlg);
IntPtr result = IntPtr.Zero;
foreach (IntPtr hCtl in itemHandlesArray)
{
string s = GetDialogItemText(hDlg,hCtl);
if (caption == s)
{
result = hCtl;
break;
}
}
return result;
}
public static string GetDialogItemText(IntPtr hDlg, IntPtr hCtl)
{
StringBuilder lpString = new StringBuilder();
int id = GetDlgCtrlID(hCtl);
GetDlgItemText(hDlg, id, lpString, lpString.Capacity );
return lpString.ToString();
}
public static void ButtonClick(IntPtr hCtl)
{
PostMessage( // SendNotifyMessage will work too
hCtl, // handle to destination control
BM_CLICK, // message ID
IntPtr.Zero, // = 0; not used, must be zero
null // = 0; not used, must be zero
);
}
}
}