private object GetAccessibleObject(IntPtr hwnd) { if (hwnd != IntPtr.Zero) { int hWndChild = 0; EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hWndChild); if (hWndChild != 0) { object ptr = null; object document = null; int hr = AccessibleObjectFromWindow(hWndChild, objid_NATIVEOM, _dispatch.ToByteArray(), ref ptr); if (hr >= 0) document = NetOffice.Core.Default.Invoker.PropertyGet(ptr, "Document"); Marshal.ReleaseComObject(ptr); if (null != document) return document; } } return null; }
static void Main(string[] args) { Excel.Application app = new Excel.Application(); EnumChildCallback cb; List<Process> procs = new List<Process>(); procs.AddRange(Process.GetProcessesByName("excel")); foreach (Process p in procs) { if ((int)p.MainWindowHandle > 0) { int childWindow = 0; cb = new EnumChildCallback(EnumChildProc); EnumChildWindows((int)p.MainWindowHandle, cb, ref childWindow); if (childWindow > 0) { const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}"); Excel.Window window = null; int res = AccessibleObjectFromWindow(childWindow, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref window); if (res >= 0) { app = window.Application; Console.WriteLine(app.Name); } } } } }
//------------------------------------------------------------------------- /* List Workbooks objects from opened Excel windows */ public static List <Excel.Workbook> GetOpenedXls() { List <Excel.Workbook> wbooks = new List <Excel.Workbook>(); List <Process> procs = new List <Process>(); procs.AddRange(Process.GetProcessesByName("excel")); foreach (Process p in procs) { if ((int)p.MainWindowHandle > 0) { int childWindow = 0; var cb = new EnumChildCallback(EnumChildProc); EnumChildWindows((int)p.MainWindowHandle, cb, ref childWindow); if (childWindow > 0) { const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}"); Excel.Window window = null; if (AccessibleObjectFromWindow(childWindow, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref window) >= 0) { wbooks.AddRange(window.Application.Workbooks.Cast <Excel.Workbook>()); } } } } return(wbooks); }
private object GetAccessibleObject(IntPtr hwnd) { if (hwnd != IntPtr.Zero) { int hWndChild = 0; EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hWndChild); if (hWndChild != 0) { object ptr = null; object document = null; int hr = AccessibleObjectFromWindow(hWndChild, objid_NATIVEOM, _dispatch.ToByteArray(), ref ptr); if (hr >= 0) { document = NetOffice.Core.Default.Invoker.PropertyGet(ptr, "Document"); } Marshal.ReleaseComObject(ptr); if (null != document) { return(document); } } } return(null); }
private static object GetAccessibleObject(IntPtr hwnd) { if (IntPtr.Zero != hwnd) { IntPtr hWndChild = new IntPtr(); EnumChildCallback cb = new EnumChildCallback(EnumRDPChildProc); EnumChildWindows(hwnd, cb, ref hWndChild); } return(null); }
internal static T FromProcess <T>(Process processToAttach, string className) where T : ComWrapper { // First, get the main window handle. IntPtr hwnd = processToAttach.MainWindowHandle; // We need to enumerate the child windows to find one that // supports accessibility. To do this, instantiate the // delegate and wrap the callback method in it, then call // EnumChildWindows, passing the delegate as the 2nd arg. if (hwnd != IntPtr.Zero) { IntPtr hwndChild = IntPtr.Zero; var cb = new EnumChildCallback( (IntPtr _hwnd, ref IntPtr _lParam) => { StringBuilder buf = new StringBuilder(128); GetClassName(_hwnd, buf, 128); if (buf.ToString() == className) { _lParam = _hwnd; return(false); } return(true); }); EnumChildWindows(hwnd, cb, ref hwndChild); // If we found an accessible child window, call // AccessibleObjectFromWindow, passing the constant // OBJID_NATIVEOM (defined in winuser.h) and // IID_IDispatch - we want an IDispatch pointer // into the native object model. if (hwndChild != IntPtr.Zero) { const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid( "{00020400-0000-0000-C000-000000000046}"); object ptr = null; int hr = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref ptr); if (hr >= 0) { // If we successfully got a native OM // IDispatch pointer, we can QI this for // an Office Application (using the implicit // cast operator supplied in the PIA). return(Activator.CreateInstance(typeof(T), new object[] { ptr }) as T); } } } return(null); }
private bool bindWindow() { try { EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(handle.ToInt32(), cb, ref childWindow); if (childWindow != 0) { // We call AccessibleObjectFromWindow, passing the constant OBJID_NATIVEOM (defined in winuser.h) // and IID_IDispatch - we want an IDispatch pointer into the native object model. // const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}"); IDispatch ptr; int hr = AccessibleObjectFromWindow(childWindow, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out ptr); if (hr >= 0) { myWordApp = (Word.Application)ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null); } } if (myWordApp != null) { Console.WriteLine( "Successfully attached to running instance of Word."); try { Word.Document myWordDocument = myWordApp.ActiveDocument; return(true); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine("No document open"); return(false); } } } catch (NullReferenceException e) { } catch (Exception e) { } return(false); }
static void Main(string[] args) { // Use the window class name ("XLMAIN") to retrieve a handle to Excel's main window. // Alternatively you can get the window handle via the process id: // int hwnd = (int)Process.GetProcessById(excelPid).MainWindowHandle; // int hwnd = (int)FindWindow("XLMAIN", null); if (hwnd != 0) { int hwndChild = 0; // Search the accessible child window (it has class name "EXCEL7") EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hwndChild); if (hwndChild != 0) { // We call AccessibleObjectFromWindow, passing the constant OBJID_NATIVEOM (defined in winuser.h) // and IID_IDispatch - we want an IDispatch pointer into the native object model. // const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}"); ExcelWindow ptr; int hr = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out ptr); if (hr >= 0) { // We successfully got a native OM IDispatch pointer, we can QI this for // an Excel Application using reflection (and using UILanguageHelper to // fix http://support.microsoft.com/default.aspx?scid=kb;en-us;320369) // using (UILanguageHelper fix = new UILanguageHelper()) { object xlApp = ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null); object version = xlApp.GetType().InvokeMember("Version", BindingFlags.GetField | BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, xlApp, null); Console.WriteLine(string.Format("Excel version is: {0}" + version)); } } } } }
static void Main() { // Use the window class name ("XLMAIN") to retrieve a handle to Excel's main window. // Alternatively you can get the window handle via the process id: // int hwnd = (int)Process.GetProcessById(excelPid).MainWindowHandle; // var p=Process.GetProcesses().FirstOrDefault(x => x.ProcessName=="WINWORD"); var hwnd = (int)FindWindow("OpusApp", null); if (hwnd == 0) { throw new Exception("Can't find Word"); } // Search the accessible child window (it has class name "_WwG") // http://msdn.microsoft.com/en-us/library/windows/desktop/dd317978%28v=vs.85%29.aspx var hwndChild = 0; var cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hwndChild); if (hwndChild == 0) { throw new Exception("Can't find Automation Child Window"); } // We call AccessibleObjectFromWindow, passing the constant OBJID_NATIVEOM (defined in winuser.h) // and IID_IDispatch - we want an IDispatch pointer into the native object model. const uint OBJID_NATIVEOM = 0xFFFFFFF0; var IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}"); IWordWindow ptr; var hr = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out ptr); if (hr < 0) { throw new Exception("Can't get Accessible Object"); } // We successfully got a native OM IDispatch pointer, we can QI this for // an Excel Application using reflection (and using UILanguageHelper to // fix http://support.microsoft.com/default.aspx?scid=kb;en-us;320369) using (new UiLanguageHelper()) { var wordApp = ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null); var version = wordApp.GetType().InvokeMember("Version", BindingFlags.GetField | BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, wordApp, null); Console.WriteLine("Word version is: {0}", version); dynamic wordAppd = ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null); Console.WriteLine("Version: " + wordAppd.Version); } }
public static void GetReferences(ref Microsoft.Office.Interop.Excel.Application _Application, ref Microsoft.Office.Interop.Excel.Workbook _Workbook) { EnumChildCallback cb; // First, get Excel's main window handle. int hwnd = (int)Process.GetCurrentProcess().MainWindowHandle; // We need to enumerate the child windows to find one that // supports accessibility. To do this, instantiate the // delegate and wrap the callback method in it, then call // EnumChildWindows, passing the delegate as the 2nd arg. if (hwnd != 0) { int hwndChild = 0; cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hwndChild); // If we found an accessible child window, call // AccessibleObjectFromWindow, passing the constant // OBJID_NATIVEOM (defined in winuser.h) and // IID_IDispatch - we want an IDispatch pointer // into the native object model. if (hwndChild != 0) { const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid( "{00020400-0000-0000-C000-000000000046}"); Microsoft.Office.Interop.Excel.Window ptr = null; int hr = AccessibleObjectFromWindow( hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref ptr); if (hr >= 0) { // If we successfully got a native OM // IDispatch pointer, we can QI this for // an Excel Application (using the implicit // cast operator supplied in the PIA). _Application = ptr.Application; _Workbook = _Application.ActiveWorkbook; } } } }
static void Main(string[] args) { // Use the window class name ("OpusApp") to retrieve a handle to Word's main window. // Alternatively you can get the window handle via the process id: // int hwnd = (int)Process.GetProcessById(wordPid).MainWindowHandle; // int hwnd = (int)FindWindow("OpusApp", null); if (hwnd != 0) { int hwndChild = 0; // Search the accessible child window (it has class name "_WwG") // as described in http://msdn.microsoft.com/en-us/library/dd317978%28VS.85%29.aspx // EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hwndChild); if (hwndChild != 0) { // We call AccessibleObjectFromWindow, passing the constant OBJID_NATIVEOM (defined in winuser.h) // and IID_IDispatch - we want an IDispatch pointer into the native object model. // const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}"); IDispatch ptr; int hr = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out ptr); if (hr >= 0) { object wordApp = ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null); object version = wordApp.GetType().InvokeMember("Version", BindingFlags.GetField | BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, wordApp, null); Console.WriteLine(string.Format("Word version is: {0}", version)); } } } }
private static object GetAccessibleObject(IntPtr hwnd) { if (hwnd != IntPtr.Zero) { int hWndChild = 0; EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hWndChild); if (hWndChild != 0) { object ptr = null; int hr = AccessibleObjectFromWindow(hWndChild, objid_NATIVEOM, _dispatch.ToByteArray(), ref ptr); if (hr >= 0) { return(NetOffice.Core.Default.Invoker.PropertyGet(ptr, "Application")); } } } return(null); }
public ExcelApplicationRetriever(int winHandle) { // We need to enumerate the child windows to find one that // supports accessibility. To do this, instantiate the // delegate and wrap the callback method in it, then call // EnumChildWindows, passing the delegate as the 2nd arg. if (winHandle != 0) { int hwndChild = 0; cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(winHandle, cb, ref hwndChild); // If we found an accessible child window, call // AccessibleObjectFromWindow, passing the constant // OBJID_NATIVEOM (defined in winuser.h) and // IID_IDispatch - we want an IDispatch pointer // into the native object model. if (hwndChild != 0) { const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid( "{00020400-0000-0000-C000-000000000046}"); Excel.Window ptr = null; int hr = AccessibleObjectFromWindow( hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref ptr); if (hr >= 0) { // If we successfully got a native OM // IDispatch pointer, we can QI this for // an Excel Application (using the implicit // cast operator supplied in the PIA). xl = ptr.Application; } } } }
static Word.Application GetWordApplicationObject(Process process) { Word.Application wordApp = null; if (process.MainWindowHandle != IntPtr.Zero) { IntPtr hwndChild = IntPtr.Zero; // Search the accessible child window (it has class name "_WwG") // as described in http://msdn.microsoft.com/en-us/library/dd317978%28VS.85%29.aspx // // adjust this class name inside EnumChildProc accordingly if you are // creating another COM server than Word // EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(process.MainWindowHandle, cb, ref hwndChild); if (hwndChild != IntPtr.Zero) { // We call AccessibleObjectFromWindow, passing the constant OBJID_NATIVEOM (defined in winuser.h) // and IID_IDispatch - we want an IDispatch pointer into the native object model. // const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}"); IDispatch ptr; int hr = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out ptr); if (hr >= 0) { // possibly adjust the name of the property containing the COM // object accordingly // wordApp = (Word.Application)ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null); } } } return(wordApp); }
public static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildCallback lpEnumFunc, ref IntPtr lParam);
private static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildCallback lpEnumFunc, ref int lParam);
private static extern Boolean EnumChildWindows( Int32 hWndParent, EnumChildCallback lpEnumFunc, ref Int32 lParam);
public static extern bool EnumChildWindows(int hWndParent, EnumChildCallback lpEnumFunc, ref int lParam);
public static extern int EnumChildWindows(IntPtr hwnd, EnumChildCallback Proc, int lParam);
private object GetAccessibleObject(IntPtr hwnd) { if (hwnd != IntPtr.Zero) { int hWndChild = 0; EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hWndChild); if (hWndChild != 0) { object ptr = null; int hr = AccessibleObjectFromWindow(hWndChild, objid_NATIVEOM, _dispatch.ToByteArray(), ref ptr); if (hr >= 0) return NetOffice.Core.Default.Invoker.PropertyGet(ptr, "Application"); } } return null; }
public static extern bool EnumChildWindows( int hWndParent, EnumChildCallback lpEnumFunc, ref int lParam);
/// <summary> /// Save all opened excel Workbooks. /// </summary> public void SaveOpenedFiles() { try { var allExcelProcesses = Process.GetProcessesByName("EXCEL"); for (int i = 0; i < allExcelProcesses.Length; i++) { int excelId = allExcelProcesses[i].Id; int hwnd = (int)Process.GetProcessById(excelId).MainWindowHandle; int hwndChild = 0; Console.WriteLine("Excel application found - process id: {0}", excelId); // Search the accessible child window (it has class name "EXCEL7") EnumChildCallback cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hwndChild); if (hwndChild != 0) { const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}"); ExcelWindow ptr; int hr = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out ptr); if (hr >= 0) { // We successfully got a native OM IDispatch pointer, we can QI this for // an Excel Application using reflection (and using UILanguageHelper to // fix http://support.microsoft.com/default.aspx?scid=kb;en-us;320369) // using (UILanguageHelper fix = new UILanguageHelper()) { bool success = false; while (success == false) { try { xlApp = (Application)ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null); success = true; } catch { SetForegroundWindow((IntPtr)hwnd); InputSimulator.SimulateKeyPress(VirtualKeyCode.EXECUTE); } } //object version = (Application)(xlApp.GetType().InvokeMember("Version", BindingFlags.GetField | BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, xlApp, null)); //Console.WriteLine(string.Format("Excel version is: {0}", version)); } //xlApp = (Application)Marshal.GetActiveObject("Excel.Application"); // It selects only 1st excell app! xlBooks = xlApp.Workbooks; var numBooks = xlBooks.Count; Console.WriteLine("Number of opened workbooks: {0}", numBooks); if (numBooks > 0) { xlBook = xlBooks[1]; } if (numBooks == 0) { allExcelProcesses[i].Kill(); // kills process if there is no active workbooks (ghost-process) Console.WriteLine("Empty process has been killed!"); break; } xlSheet = (Worksheet)xlBook.Worksheets[1]; string filePath = @"C:\ExcelSaver"; bool exists = System.IO.Directory.Exists(filePath); if (!exists) { System.IO.Directory.CreateDirectory(filePath); } BringExcelWindowToFront(xlApp); Console.WriteLine("Saving files: "); foreach (Workbook wb in xlApp.Workbooks) { string fileName = wb.Name; //string filePath = wb.Path; default path (same as original file) string fileNameAndPath = Path.Combine(filePath, fileName); openedFiles.Add(fileNameAndPath); Console.WriteLine("Saving {0}", fileName); bool edit = true; while (edit == true) { edit = IsEditMode(); if (edit == false) { xlApp.DisplayAlerts = false; wb.SaveAs(fileNameAndPath, wb.FileFormat, "", "", false, false, XlSaveAsAccessMode.xlExclusive, XlSaveConflictResolution.xlLocalSessionChanges); xlApp.DisplayAlerts = true; } else { InputSimulator.SimulateKeyPress(VirtualKeyCode.EXECUTE); } } } xlBooks.Close(); xlApp.Quit(); } } else { allExcelProcesses[i].Kill(); Console.WriteLine("Empty process has been killed!"); } } Console.WriteLine("All files have been saved."); RemoveResources(); } catch (Exception e) { Console.WriteLine("{0}", e.Message); Console.ReadKey(); } }