internal static IntPtr GetWindowHandleFromAutomationElement(AutomationElement element)
{
if( element == null )
throw new ArgumentException("Automation Element cannot be null");
object objHwnd = element.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty);
IntPtr ptr = IntPtr.Zero;
if (objHwnd is IntPtr)
ptr = (IntPtr)objHwnd;
else
ptr = new IntPtr(Convert.ToInt64(objHwnd, CultureInfo.CurrentCulture));
if (ptr == IntPtr.Zero)
throw new InvalidOperationException("Could not get the handle to the element(window)");
return ptr;
}