|
|
|
Development: Win32
Utilizing transactional NTFS through .NET
We're used to using transactions when dealing with the database layer. Transactions ensure we can perform multiple queries as one atomic event, either they all succed or they all fail, obeying the rules of ACIDity. Until Vista, performing transactional file operations haven't been possible. Transaction NTFS (or TxF) is available from Vista and onwards, which means Server 2008 is also capable. XP and Server 2003 do not support TxF and there are currently no plans of adding TxF support in systems previous to Vista. So what is the benefit of using TxF? The benefit is that we can...
MTH going open
Some of you may know that I used to play a lot of poker. Unfortunately that's not the case any more. I really enjoy live poker when I'm in Vegas, I enjoy the major tournaments and I've definitely not participated in my last WSOP. But as for online poker and the daily grind, I've quit it. I just don't find it exciting any more. While the mathematical aspect acquired my interest early on, I never enjoyed grinding as such, it was purely for monetary reasons. Anyways, one of the ways I kept enjoying was by utilizing one of my...
Cascading windows
In Photoshop we often work with multiple windows open. They can be cascaded to more easily be able to view the different windows and tell them apart. There's an API function that does the same to any windows you specify, you can even define the rectangle where they should be cascaded within. using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
namespace Cascading_Windows
{
class Program
{
// The CascadeWindows function cascades the specified child windows of the specified parent window. It can be used to
// cascade all windows (as in this example) or just the child windows of a specific window by passing in a handle to...
Animating windows
Let's be a bit more graphic. This time I'll show you how to use the Windows API to make your forms fade in/out, slide in from the side or do various other animations. For this example we'll have to use a Windows Forms project as we have to utilize a Form object in the example. using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Animating_windows
{
public partial class Form1 : Form
{
// The possible AW flags for use with the AnimateWindow function.
public enum AW : int
{
SLIDE = 262144,
ACTIVATE = 131072,
BLEND = 524288,
HIDE = 65536,
CENTER = 16,
HOR_POSITIVE = 1,
HOR_NEGATIVE = 2,
VER_POSITIVE =...
Activating windows
Now we'll see how to activate windows and sending them to the foreground. I will be using the WindowFinder class that I introduced in the blog Finding specific windows. using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Activating_windows
{
class Program
{
// This enumeration holds all the possible values that can be passed onto the ShowWindow function.
public enum SW : int
{
HIDE = 0,
SHOWNORMAL = 1,
SHOWMINIMIZED = 2,
SHOWMAXIMIZED = 3,
SHOWNOACTIVATE = 4,
SHOW = 5,
MINIMIZE = 6,
SHOWMINNOACTIVE = 7,
SHOWNA = 8,
RESTORE = 9,
SHOWDEFAULT = 10
}
// The SetForegroundWindow will activate the window, setting the window thread to the foreground thread, as
// well as activating keyboard input for the specified...
Getting key state
Here's an example of how to retrieve the state of any keyboard key. using System;
using System.Runtime.InteropServices;
namespace Getting_key_state
{
class Program
{
// The GetAsyncKeyState takes a virtual key code as the nVirtKey parameter. It then checks on the state of
// this key (down/up). The return code is either zero for up or any non-zero value for pressed,
// thus it's easiest to convert the result to a boolean and use that result.
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int nVirtKey);
// These are all the possible values in the VK enumeration. It covers most of the special buttons on a keyboard.
// See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
// for full documentation.
public enum VK...
Minimizing and maximizing windows
This time I will show how to maximize and minimize windows. I will be using the WindowFinder class that I introduced in the blog Finding specific windows. using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Minimizing_and_maximizing_windows
{
class Program
{
// The ShowWindowAsync method alters the windows show state through the nCmdShow parameter.
// The nCmdShow parameter can have any of the SW values.
// See http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/showwindowasync.asp
// for full documentation.
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(int hWnd, int nCmdShow);
// An enumeration containing all the possible SW values.
public enum SW : int
{
HIDE = 0,
SHOWNORMAL = 1,
SHOWMINIMIZED = 2,
SHOWMAXIMIZED = 3,
SHOWNOACTIVATE = 4,
SHOW = 5,
MINIMIZE = 6,
SHOWMINNOACTIVE = 7,
SHOWNA = 8,
RESTORE...
Modifying window location and size
Last time we saw how to obtain a windows location and size. This time I'll show how to change a windows size and location. I will be using the WindowFinder class that I introduced in the blog Finding specific windows. using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Drawing;
namespace Modifying_window_location_and_size
{
class Program
{
// The SetWindowPos function is used to both resize and change the location of windows. The uFlags parameter
// can take any number of flags, with zero being a neutral flag, the same goes for the hWndInsertAfter parameter.
// X, Y is the new location of the window, cx and cy is the...
Getting window location and size
This time I'll show how to obtain the size and location of a window. I will be using the WindowFinder class that I introduced in the blog Finding specific windows. Note that the location is not in relation to it's parent windows location, it is always the absolute screen position. using System.Runtime.InteropServices;
using System.Drawing;
using System.Text.RegularExpressions;
using System;
using System.Text;
using System.Globalization;
namespace Getting_window_location_and_size
{
class Program
{
// Win32 constants.
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
// Win32 functions that have all been used in previous blogs.
[DllImport("User32.Dll")]
private static extern void GetClassName(int hWnd, StringBuilder s, int nMaxCount);
[DllImport("User32.dll")]
private static extern Int32 SendMessage(int hWnd, int Msg, int wParam, StringBuilder...
Finding specific windows
Last time I made an example of how to enumerate windows. This time I present to you a class that greatly simplifies the process of searching for specific windows, types of windows, windows belonging to a specific process, having a specific text. You can search for any number of these parameters at the same time, using regular expressions for all string matches to provide optimal flexibility. using System.Runtime.InteropServices;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace Searching_for_windows
{
class Program
{
// Win32 constants.
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
// Win32 functions that have all been used in previous blogs.
[DllImport("User32.Dll")]
private static extern void GetClassName(int hWnd,...
Enumerating windows
Until now we've seen how to retrieve basic properties of windows as well as how to interact with them by sending keypresses. Up until now we've had to find the handle by using Winspector or a similar program. This time I'll present a way of finding the handles programmatically. using System.Runtime.InteropServices;
using System.Text;
using System;
namespace Enumerating_windows
{
class Program
{
// These are two Win32 constants that we'll need, they were explained in an earlier blog.
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
// SendMessage overload.
[DllImport("User32.dll")]
public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, int lParam);
// SendMessage overload.
[DllImport("User32.dll")]
public static extern Int32 SendMessage(int hWnd, int Msg, int...
Sending keypresses to a window
Now to complete the toolset required to make a great spyware / browser hijacking application, we'll make Internet Explorer navigate to the address we set. Like before, open an Internet Explorer browser and obtain a handle to the address field. using System;
using System.Runtime.InteropServices;
using System.Globalization;
namespace Sending_keypresses_to_a_window
{
class Program
{
// A Win32 constant
const int WM_SETTEXT = 0x000C;
const int WM_KEYDOWN = 0x0100;
const int VK_RETURN = 0x0D;
// An overload of the SendMessage function, this time taking in a string as the lParam.
[DllImport("User32.dll")]
public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, string lParam);
// PostMessage is very similar to SendMessage. They both send a...
Set text by handle
This time we won't be reading the text from a window, we'll be setting it. Like last time, open an Internet Explorer browser and obtain a handle to the address field. using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;
namespace Set_text_by_handle
{
class Program
{
// A Win32 constant
const int WM_SETTEXT = 0x000C;
// An overload of the SendMessage function, this time taking in a string as the lParam.
[DllImport("User32.dll")]
public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, string lParam);
static void Main(string[] args)
{
// First, read the handle from the console, remember this has to be in HEX format!
int handle = int.Parse(Console.ReadLine(), NumberStyles.HexNumber);
// Now we'll send the...
Getting text from handle
This time we want to retrieve the text from a given window, represented by a handle. Like last time, open an Internet Explorer instance. Now open Winspector and select the address field, ensure that it is the address field itself (class = Edit) and not the ComboBox that you select. using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;
namespace Get_text_from_handle
{
class Program
{
// These are two Win32 constants that we'll need, they'll be explained later.
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
// The SendMessage function sends a Win32 message to the specified handle, it takes three
// ints as parameters, the message to...
Getting a process from a handle
So we have a handle, what process does it belong to? Our goal is to obtain a .NET System.Diagnostics.Process object that corresponds to the owner process of the handle we input. Let's first open up an Internet Explorer window, just leave it at the start page, whatever yours is. Now fire up Winspector and locate the Internet Explorer window, you'll see the handle in HEX format in the treeview. using System;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Diagnostics;
namespace Get_process_from_handle
{
class Program
{
// The DllImport attribute specifies the Win32 DLL that contains the function we're importing,
// in this case it's...
The basics
First of all, to manipulate and use the Win32 API, we must know about the system itself, the windows, the controls and so forth. Visual Studio comes bundled with Spy++ which enables us to identity the various windows and controls of application, but honestly, it's pretty bad. Instead you should download Winspector. Threads & processes Each application/window in Windows belongs to a given thread under a given process. A process may have multiple threads and windows, but a thread and a window can only belong to a single process. Windows & handles Usually we refer to...
Dedicated to the API
Unfortunately it's been quite silent around here for the last couple of months. I want to change that, so why not make a promise? I've always been bullied by my wannabe developer friends for memorizing most of the Win32 (XP onwards) API. What they don't realize is just how useful the API is - how powerful it is! For the complete month of April 2007, I will post a new blog each day containing a Win32 API trick including demonstration code. Although my samples will be in C#, this being the Win32 API, it's actually usable in more...
|