using System;
using System.Text;
using System.Runtime;
using System.Runtime.InteropServices;
namespace IO.Interoperability
{
public delegate void EditStreamCallback(IntPtr dwCookie, IntPtr pbBuff, int cb, IntPtr pcb);
public struct EditStream
{
public int dwCookie;
public int dwError;
public IntPtr pfnCallback;
}
public enum ProcessAccessFlags
{
PROCESS_TERMINATE = 0x00000001,
PROCESS_CREATE_THREAD = 0x00000002,
PROCESS_SET_SESSIONID = 0x00000004,
PROCESS_VM_OPERATION = 0x00000008,
PROCESS_VM_READ = 0x00000010,
PROCESS_VM_WRITE = 0x00000020,
PROCESS_DUP_HANDLE = 0x00000040,
PROCESS_CREATE_PROCESS = 0x00000080,
PROCESS_SET_QUOTA = 0x00000100,
PROCESS_SET_INFORMATION = 0x00000200,
PROCESS_QUERY_INFORMATION = 0x00000400,
STANDARD_RIGHTS_REQUIRED = 0x000F0000,
SYNCHRONIZE = 0x00100000,
PROCESS_ALL_ACCESS = PROCESS_TERMINATE | PROCESS_CREATE_THREAD | PROCESS_SET_SESSIONID | PROCESS_VM_OPERATION |
PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_SET_QUOTA |
PROCESS_SET_INFORMATION | PROCESS_QUERY_INFORMATION | STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE
}
[Flags]
public enum AllocationType
{
Commit = 0x1000,
Reserve = 0x2000,
Decommit = 0x4000,
Release = 0x8000,
Reset = 0x80000,
Physical = 0x400000,
TopDown = 0x100000,
WriteWatch = 0x200000,
LargePages = 0x20000000
}
[Flags]
public enum MemoryProtection
{
Execute = 0x10,
ExecuteRead = 0x20,
ExecuteReadWrite = 0x40,
ExecuteWriteCopy = 0x80,
NoAccess = 0x01,
ReadOnly = 0x02,
ReadWrite = 0x04,
WriteCopy = 0x08,
GuardModifierflag = 0x100,
NoCacheModifierflag = 0x200,
WriteCombineModifierflag = 0x400
}
/// <summary>
/// The Invoke class defines platform invocations and public invocations to locate, determine, and hook into
/// windows objects using managed code assembly.
/// </summary>
internal partial class Converter
{
internal const int WM_GETTEXTLENGTH = 0x000E;
internal const int WM_GETTEXT = 0x000D;
internal const int WM_SETTEXT = 0x000C;
internal const int WM_USER = 0x400;
internal const int EM_GETLINE = 0x00C4;
internal const int EM_STREAMOUT = WM_USER + 74;
internal const int SF_RTF = 0x0002;
internal const uint INFINITE = 0xFFFFFFFF;
internal const uint WAIT_ABANDONED = 0x00000080;
internal const uint WAIT_OBJECT_0 = 0x00000000;
internal const uint WAIT_TIMEOUT = 0x00000102;
/// <summary>
/// The WM_CHAR message is posted to the window with the keyboard focus when a WM_KEYDOWN message is translated by the TranslateMessage function. The WM_CHAR message contains the character code of the key that was pressed.
/// </summary>
internal const int CHAR = 0x0102;
internal enum GETWINDOW : uint
{
/// <summary>
///The retrieved handle identifies the window of the same type that is highest in the Z order.
///If the specified window is a topmost window, the handle identifies the topmost window that is highest in the Z order. If the specified window is a top-level window, the handle identifies the top-level window that is highest in the Z order. If the specified window is a child window, the handle identifies the sibling window that is highest in the Z order.
/// </summary>
GW_HWNDFIRST = 0,
/// <summary>
/// The retrieved handle identifies the window of the same type that is lowest in the Z order. If the specified window is a topmost window, the handle identifies the topmost window that is lowest in the Z order.
/// If the specified window is a top-level window, the handle identifies the top-level window that is lowest in the Z order. If the specified window is a child window, the handle identifies the sibling window that is lowest in the Z order.
/// </summary>
GW_HWNDLAST = 1,
/// <summary>
/// The retrieved handle identifies the window below the specified window in the Z order.
/// If the specified window is a topmost window, the handle identifies the topmost window below the specified window.
/// If the specified window is a top-level window, the handle identifies the top-level window below the specified window.
/// If the specified window is a child window, the handle identifies the sibling window below the specified window.
/// </summary>
GW_HWNDNEXT = 2,
/// <summary>
/// The retrieved handle identifies the window above the specified window in the Z order.
/// If the specified window is a topmost window, the handle identifies the topmost window above the specified window.
/// If the specified window is a top-level window, the handle identifies the top-level window above the specified window.
/// If the specified window is a child window, the handle identifies the sibling window above the specified window.
/// </summary>
GW_HWNDPREV = 3,
/// <summary>
/// The retrieved handle identifies the specified window's owner window, if any. For more information, see Owned Windows.
/// </summary>
GW_OWNER = 4,
/// <summary>
/// The retrieved handle identifies the child window at the top of the Z order, if the specified window is a parent window; otherwise, the retrieved handle is NULL.
/// The function examines only child windows of the specified window. It does not examine descendant windows.
/// </summary>
GW_CHILD = 5,
/// <summary>
/// Windows 2000/XP: The retrieved handle identifies the enabled popup window owned by the specified window (the search uses the first such window found using GW_HWNDNEXT);
/// otherwise, if there are no enabled popup windows, the retrieved handle is that of the specified window.
/// </summary>
GW_ENABLEDPOPUP = 6
}
/// <summary>
/// Closes an open object handle.
/// </summary>
/// <param name="hObject">A valid handle to an open object.</param>
/// <returns></returns>
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool CloseHandle(IntPtr hObject);
/// <summary>
/// The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.
/// <br />
/// To search child windows, beginning with a specified child window, use the FindWindowEx function.
/// </summary>
/// <param name="lpClassName">
/// [in] Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero.
/// <br />
/// If lpClassName points to a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.</param>
/// <param name="lpWindowName">
/// [in] Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.
/// </param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// The FindWindowEx function retrieves a handle to a window whose class name and window name match the specified strings. The function searches child windows, beginning with the one following the specified child window. This function does not perform a case-sensitive search.
/// </summary>
/// <param name="hwndParent">
/// [in] Handle to the parent window whose child windows are to be searched.
/// <br />
/// If hwndParent is NULL, the function uses the desktop window as the parent window. The function searches among windows that are child windows of the desktop.
/// </param>
/// <param name="hwndChildAfter">
/// [in] Handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window.
/// <br />
/// If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.
/// </param>
/// <param name="lpszClass">
/// Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be placed in the low-order word of lpszClass; the high-order word must be zero.
/// </param>
/// <param name="lpszWindow">
/// [in] Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.
/// </param>
/// <returns>
/// If the function succeeds, the return value is a handle to the window that has the specified class and window names.
/// </returns>
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
/// <summary>
/// The FindWindowEx function retrieves a handle to a window whose class name and window name match the specified strings. The function searches child windows, beginning with the one following the specified child window. This function does not perform a case-sensitive search.
/// </summary>
/// <param name="hwndParent">
/// [in] Handle to the parent window whose child windows are to be searched.
/// <br />
/// If hwndParent is NULL, the function uses the desktop window as the parent window. The function searches among windows that are child windows of the desktop.
/// </param>
/// <param name="hwndChildAfter">
/// [in] Handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window.
/// <br />
/// If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.
/// </param>
/// <param name="lpszClass">
/// Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be placed in the low-order word of lpszClass; the high-order word must be zero.
/// </param>
/// <param name="lpszWindow">
/// [in] Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.
/// </param>
/// <returns>
/// If the function succeeds, the return value is a handle to the window that has the specified class and window names.
/// </returns>
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindowEx(IntPtr hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
/// <summary>
/// The FindWindowEx function retrieves a handle to a window whose class name and window name match the specified strings. The function searches child windows, beginning with the one following the specified child window. This function does not perform a case-sensitive search.
/// </summary>
/// <param name="parentHandle">
/// [in] Handle to the parent window whose child windows are to be searched.
/// <br />
/// If hwndParent is NULL, the function uses the desktop window as the parent window. The function searches among windows that are child windows of the desktop.
/// </param>
/// <param name="ChildAfter">
/// [in] Handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window.
/// <br />
/// If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.
/// </param>
/// <param name="className">
/// Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be placed in the low-order word of lpszClass; the high-order word must be zero.
/// </param>
/// <param name="windowTitle">
/// [in] Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.
/// </param>
/// <returns>
/// If the function succeeds, the return value is a handle to the window that has the specified class and window names.
/// </returns>
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
/// <summary>
/// The GetWindow function retrieves a handle to a window that has the specified relationship (Z-Order or owner) to the specified window.
/// </summary>
/// <param name="hWnd">
/// [in] Handle to a window. The window handle retrieved is relative to this window, based on the value of the uCmd parameter.
/// </param>
/// <param name="uCmd">
/// [in] Specifies the relationship between the specified window and the window whose handle is to be retrieved.
/// </param>
/// <returns>
/// If the function succeeds, the return value is a window handle. If no window exists with the specified relationship to the specified window, the return value is NULL.
/// To get extended error information, call GetLastError.
/// </returns>
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetWindow(IntPtr hWnd, GETWINDOW uCmd);
/// <summary>
/// The GetNextWindow function retrieves a handle to the next or previous window in the Z-Order.
/// The next window is below the specified window; the previous window is above.
/// If the specified window is a topmost window, the function retrieves a handle to the next (or previous) topmost window.
/// If the specified window is a top-level window, the function retrieves a handle to the next (or previous) top-level window.
/// If the specified window is a child window, the function searches for a handle to the next (or previous) child window.
/// </summary>
/// <param name="hWnd">
/// [in] Handle to a window. The window handle retrieved is relative to this window, based on the value of the wCmd parameter.
/// </param>
/// <param name="uCmd">
/// [in] Specifies whether the function returns a handle to the next window or of the previous window.
/// </param>
/// <returns>
/// If the function succeeds, the return value is a handle to the next (or previous) window.
/// If there is no next (or previous) window, the return value is NULL. To get extended error information, call GetLastError.
/// </returns>
[DllImport("user32.dll", EntryPoint = "GetWindow")]
internal static extern IntPtr GetNextWindow(IntPtr hWnd, GETWINDOW uCmd);
/// <summary>
/// The GetClassName function retrieves the name of the class to which the specified window belongs.
/// </summary>
/// <param name="hWnd">
/// [in] Handle to the window and, indirectly, the class to which the window belongs.
/// </param>
/// <param name="lpClassName">
/// [out] Pointer to the buffer that is to receive the class name string.
/// </param>
/// <param name="nMaxCount">
/// [in] Specifies the length, in TCHAR, of the buffer pointed to by the lpClassName parameter.
/// The class name string is truncated if it is longer than the buffer and is always null-terminated.
/// </param>
/// <returns>
/// If the function succeeds, the return value is the number of TCHAR copied to the specified buffer.
/// </returns>
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
/// <summary>
/// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
/// <br />
/// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
/// </summary>
/// <param name="hWnd">
/// Handle to the window whose window procedure will receive the message.
/// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
/// </param>
/// <param name="Msg">
/// [in] Specifies the message to be sent.
/// </param>
/// <param name="wParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <param name="lParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <returns>
/// The return value specifies the result of the message processing; it depends on the message sent.
/// <br />
/// If you use '[Out] StringBuilder', initialize the string builder with proper length first.
/// </returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, StringBuilder lParam);
/// <summary>
/// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
/// <br />
/// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
/// </summary>
/// <param name="hWnd">
/// Handle to the window whose window procedure will receive the message.
/// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
/// </param>
/// <param name="Msg">
/// [in] Specifies the message to be sent.
/// </param>
/// <param name="wParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <param name="lParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <returns>
/// The return value specifies the result of the message processing; it depends on the message sent.
/// <br />
/// If you use '[Out] StringBuilder', initialize the string builder with proper length first.
/// </returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
/// <br />
/// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
/// </summary>
/// <param name="hWnd">
/// Handle to the window whose window procedure will receive the message.
/// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
/// </param>
/// <param name="Msg">
/// [in] Specifies the message to be sent.
/// </param>
/// <param name="wParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <param name="lParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <returns>
/// The return value specifies the result of the message processing; it depends on the message sent.
/// </returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
/// <summary>
/// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
/// <br />
/// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
/// </summary>
/// <param name="hWnd">
/// Handle to the window whose window procedure will receive the message.
/// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
/// </param>
/// <param name="Msg">
/// [in] Specifies the message to be sent.
/// </param>
/// <param name="wParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <param name="lParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <returns>
/// The return value specifies the result of the message processing; it depends on the message sent.
/// </returns>
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto, SetLastError = false)]
internal static extern IntPtr SendMessageByString(IntPtr hWnd, uint Msg, IntPtr wParam, StringBuilder lParam);
/// <summary>
/// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
/// <br />
/// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
/// </summary>
/// <param name="hWnd">
/// Handle to the window whose window procedure will receive the message.
/// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
/// </param>
/// <param name="Msg">
/// [in] Specifies the message to be sent.
/// </param>
/// <param name="wParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <param name="lParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <returns>
/// The return value specifies the result of the message processing; it depends on the message sent.
/// </returns>
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
internal static extern IntPtr SendMessageByString(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
/// <summary>
/// Retrieves the length, in characters, of the specified window's title bar text (if the window has a title bar).
/// <br />
/// If the specified window is a control, the function retrieves the length of the text within the control.
/// <br />
/// However, GetWindowTextLength cannot retrieve the length of the text of an edit control in another application
/// </summary>
/// <param name="hWnd">
/// A handle to the window or control.
/// </param>
/// <returns>
/// If the function succeeds, the return value is the length, in characters, of the text. Under certain conditions, this value may actually be greater than the length of the text
/// </returns>
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
internal static extern void MoveMemory(IntPtr dest, IntPtr src, int size);
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
internal static extern void MoveMemory(StringBuilder dest, IntPtr src, int size);
[DllImport("user32.dll", SetLastError = true)]
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
// When you don't want the ProcessId, use this overload and pass IntPtr.Zero for the second parameter
[DllImport("user32.dll")]
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("kernel32.dll")]
internal static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, IntPtr dwProcessId);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, StringBuilder lpBuffer, int nSize, int lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, EditStream lpBuffer, int nSize, int lpNumberOfBytesWritten);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32")]
internal static extern IntPtr CreateRemoteThread(
IntPtr hProcess,
IntPtr lpThreadAttributes,
uint dwStackSize,
IntPtr lpStartAddress, // raw Pointer into remote process
IntPtr lpParameter,
uint dwCreationFlags,
uint lpThreadId
);
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
[DllImport(@"C:\Users\User\Documents\Visual Studio 2010\Projects\Unmanaged\Debug\Unmanaged.dll", EntryPoint = "GetRichTextBoxString")]
internal static extern string GetRichTextBoxString();
[DllImport(@"C:\Users\User\Documents\Visual Studio 2010\Projects\Unmanaged\Debug\Unmanaged.dll", CallingConvention = CallingConvention.StdCall)]
internal static extern int GetAddr();
}
}