Skip to content

Instantly share code, notes, and snippets.

@xameeramir
Created January 29, 2016 13:36
Show Gist options
  • Save xameeramir/e28634c07537567ce109 to your computer and use it in GitHub Desktop.
Save xameeramir/e28634c07537567ce109 to your computer and use it in GitHub Desktop.
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
// For Windows Mobile, replace user32.dll with coredll.dll
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
enum GetWindow_Cmd : 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", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, UIntPtr wParam,
IntPtr lParam);
[DllImport("oleacc.dll", PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
static extern object ObjectFromLresult(UIntPtr lResult,
[MarshalAs(UnmanagedType.LPStruct)] Guid refiid, IntPtr wParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
[Flags]
enum SendMessageTimeoutFlags : uint
{
SMTO_NORMAL = 0x0,
SMTO_BLOCK = 0x1,
SMTO_ABORTIFHUNG = 0x2,
SMTO_NOTIMEOUTIFNOTHUNG = 0x8,
SMTO_ERRORONEXIT = 0x20
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd,
uint Msg,
UIntPtr wParam,
IntPtr lParam,
SendMessageTimeoutFlags fuFlags,
uint uTimeout,
out UIntPtr lpdwResult);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr windowHandle,
uint Msg,
IntPtr wParam,
IntPtr lParam,
SendMessageTimeoutFlags flags,
uint timeout,
out IntPtr result);
/* Version specifically setup for use with WM_GETTEXT message */
[DllImport("user32.dll", EntryPoint = "SendMessageTimeout", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint SendMessageTimeoutText(
IntPtr hWnd,
int Msg, // Use WM_GETTEXT
int countOfChars,
StringBuilder text,
SendMessageTimeoutFlags flags,
uint uTImeoutj,
out IntPtr result);
/* Version for a message which returns an int, such as WM_GETTEXTLENGTH. */
[DllImport("user32.dll", EntryPoint = "SendMessageTimeout", CharSet = CharSet.Auto)]
public static extern int SendMessageTimeout(
IntPtr hwnd,
uint Msg,
int wParam,
int lParam,
uint fuFlags,
uint uTimeout,
out int lpdwResult);
// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
private struct UUID{
public long Data1;
public int Data2, Data3;
public byte[] Data4();
}
public IHTMLDocument GetWinIEDOM(IntPtr hWnd){
UUID typUUID;
UIntPtr lngRes;
uint lngMsg, uTimeout = 1000;
lngMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");
if( lngMsg != 0){
SendMessageTimeout(hWnd, lngMsg, UIntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, uTimeout, out lngRes);
if (lngRes != UIntPtr.Zero)
{
typUUID.Data1 = &H626FC520;
typUUID.Data2 = &HA41E;
typUUID.Data3 = &H11CF;
typUUID.Data4[0] = &HA7;
typUUID.Data4[1] = &H31;
typUUID.Data4[2] = &H0;
typUUID.Data4[3] = &HA0;
typUUID.Data4[4] = &HC9;
typUUID.Data4[5] = &H8;
typUUID.Data4[6] = &H26;
typUUID.Data4[7] = &H37;
typUUID.Data4[8] = &H38;
}
ObjectFromLresult(lngRes, (Guid)typUUID, IntPtr.Zero);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment