2009-04-22 12 views
17

estoy usando¿Cómo utilizar el enlace tardío para obtener la instancia de Excel?

[DllImport("Oleacc.dll")] 
static extern int AccessibleObjectFromWindow(
int hwnd, 
uint dwObjectID, 
byte[] riid, 
ref Excel.Window ptr);

para obtener una instancia de Excel usando el mango, que llego del proceso ID de la instancia de Excel.

Ésta es la forma en que parece cuando utilizo estas funciones

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); 

Object objApp = ptr.Application;

Esta paz de código funciona muy bien, pero el único problema es que he tenido que añadir una referencia a la Oficina 2003 ensamblados de interoperabilidad principal.

Como puede ver, el último parámetro en la función es la razón por la que necesitaba agregar la referencia a las Pias, entonces mi pregunta es si hay una forma de evitar el uso de Interop Assemblies, lo he intentado con vinculante tarde, pero tal vez he estado haciendo mal porque no he podido hacerlo funcionar.

Respuesta

25

Primero: el enlace tardío en C# es bastante molesto. Lo mejor es evitarlo. Segundo: la unión tardía en C# es un dolor. Usa el PIA!

Ok, una vez dicho esto, esto es lo que debe hacer para utilizar el enlace tardío: elimine la referencia a los PIA de Office 2003 y en su lugar agregue una importación COM de la interfaz requerida por AccessibleObjectFromWindow, es decir, la interfaz Excel.Window:

[Guid("00020893-0000-0000-C000-000000000046")] 
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
public interface ExcelWindow 
{ 
} 

puede recuperar esta interfaz utilizando una herramienta como Reflector (o simplemente pulsando F12 en el tipo Excel.Window mientras que la referencia al PIA de Excel se encuentra todavía en su proyecto)

Eso está haciendo que tendrá para modificar la firma de AccessibleObjectFromWindow para que coincida con la importada ExcelWindow interfaz:

[DllImport("Oleacc.dll")] 
static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, out ExcelWindow ptr); 

Por último, se debe utilizar la reflexión para obtener el objeto Excel.Application del ExcelWindow objeto:

object xlApp = ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null); 

Si el código se va a hacer un montón de llamadas en El OM de Excel podría ser más fácil de utilizar VB con Option Strict apagado (o esperar C# 4.0 ;-). O bien, si no desea cambiar de C#, podría ser una buena idea crear una clase contenedora para las últimas llamadas vinculantes.


muestra completa

Aquí está una muestra completamente funcional (basado en un article por Andrew Whitechapel):

using System; 
using System.Globalization; 
using System.Reflection; 
using System.Runtime.InteropServices; 
using System.Text; 

namespace ExcelLateBindingSample 
{ 
    /// <summary> 
    /// Interface definition for Excel.Window interface 
    /// </summary> 
    [Guid("00020893-0000-0000-C000-000000000046")] 
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
    public interface ExcelWindow 
    { 
    } 

    /// <summary> 
    /// This class is needed as a workaround to http://support.microsoft.com/default.aspx?scid=kb;en-us;320369 
    /// Excel automation will fail with the follwoing error on systems with non-English regional settings: 
    /// "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))" 
    /// </summary> 
    class UILanguageHelper : IDisposable 
    { 
     private CultureInfo _currentCulture; 

     public UILanguageHelper() 
     { 
      // save current culture and set culture to en-US 
      _currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture; 
      System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
     } 

     public void Dispose() 
     { 
      // reset to original culture 
      System.Threading.Thread.CurrentThread.CurrentCulture = _currentCulture; 
     } 
    } 

    class Program 
    { 
     [DllImport("user32.dll", SetLastError = true)] 
     static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

     [DllImport("Oleacc.dll")] 
     static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, out ExcelWindow ptr); 

     public delegate bool EnumChildCallback(int hwnd, ref int lParam); 

     [DllImport("User32.dll")] 
     public static extern bool EnumChildWindows(int hWndParent, EnumChildCallback lpEnumFunc, ref int lParam); 

     [DllImport("User32.dll")] 
     public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount); 

     public static bool EnumChildProc(int hwndChild, ref int lParam) 
     { 
      StringBuilder buf = new StringBuilder(128); 
      GetClassName(hwndChild, buf, 128); 
      if (buf.ToString() == "EXCEL7") 
      { 
       lParam = hwndChild; 
       return false; 
      } 
      return true; 
     } 

     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)); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

Y esta sería la misma solución sin los PIA en VB (Tenga en cuenta que las llamadas de OM son mucho más legibles, sin embargo, el código para obtener acceso al OM sería el mismo):

Option Strict Off 

Imports System.Globalization 
Imports System.Runtime.InteropServices 
Imports System.Text 

Module ExcelLateBindingSample 

    ''' <summary> 
    ''' Interface definition for Excel.Window interface 
    ''' </summary> 
    <Guid("00020893-0000-0000-C000-000000000046"), _ 
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _ 
    Public Interface ExcelWindow 
    End Interface 

    ''' <summary> 
    ''' This class is needed as a workaround to http://support.microsoft.com/default.aspx?scid=kb;en-us;320369 
    ''' Excel automation will fail with the follwoing error on systems with non-English regional settings: 
    ''' "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))" 
    ''' </summary> 
    Class UILanguageHelper 
     Implements IDisposable 

     Private _currentCulture As CultureInfo 

     Public Sub New() 
      ' save current culture and set culture to en-US 
      _currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture 
      System.Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US") 
     End Sub 

     Public Sub Dispose() Implements System.IDisposable.Dispose 
      'reset to original culture 
      System.Threading.Thread.CurrentThread.CurrentCulture = _currentCulture 
     End Sub 

    End Class 

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
    Private Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr 
    End Function 

    <DllImport("Oleacc.dll")> _ 
    Private Function AccessibleObjectFromWindow(ByVal hwnd As Integer, ByVal dwObjectID As UInt32, ByVal riid() As Byte, ByRef ptr As ExcelWindow) As Integer 
    End Function 

    Public Delegate Function EnumChildCallback(ByVal hwnd As Integer, ByRef lParam As Integer) As Boolean 

    <DllImport("User32.dll")> _ 
    Public Function EnumChildWindows(ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumChildCallback, ByRef lParam As Integer) As Boolean 
    End Function 

    <DllImport("User32.dll")> _ 
    Public Function GetClassName(ByVal hWnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer 
    End Function 

    Public Function EnumChildProc(ByVal hwndChild As Integer, ByRef lParam As Integer) As Boolean 
     Dim buf As New StringBuilder(128) 
     GetClassName(hwndChild, buf, 128) 
     If buf.ToString() = "EXCEL7" Then 
      lParam = hwndChild 
      Return False 
     End If 
     Return True 
    End Function 

    Sub 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: 
     ' Dim hwnd As Integer = CInt(Process.GetProcessById(excelPid).MainWindowHandle); 
     ' 
     Dim hwnd As Integer = CInt(FindWindow("XLMAIN", Nothing)) 

     If hwnd <> 0 Then 
      Dim hwndChild As Integer = 0 

      ' Search the accessible child window (it has class name "EXCEL7") 
      Dim cb As New EnumChildCallback(AddressOf EnumChildProc) 
      EnumChildWindows(hwnd, cb, hwndChild) 

      If hwndChild <> 0 Then 
       ' 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 OBJID_NATIVEOM As UInteger = &HFFFFFFF0& 
       Dim IID_IDispatch As New Guid("{00020400-0000-0000-C000-000000000046}") 
       Dim ptr As ExcelWindow 

       Dim hr As Integer = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ptr) 

       If hr >= 0 Then 
        ' 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 fixCrash As New UILanguageHelper 
         Console.WriteLine(String.Format("Excel version is: {0}", ptr.Application.Version)) 
        End Using 
       End If 
      End If 
     End If 

    End Sub 

End Module 
+5

Eso me daña la cabeza solo mirándolo. –

+7

+1 Trabajo realmente impresionante, divo. ¿Y se muestra en VB y en C#? Lamento que estemos limitados a votar solo una vez. –

+2

Muchas gracias, ya que Mike dice que es un trabajo impresionante, es exactamente lo que necesitaba. – Vic

0

No.

Sé que suena trillado, pero VB es muchas veces más fácil de usar que C# cuando se trabaja con Excel. Incluso si usa PIA en lugar de la vinculación tardía total, aún así es mejor usar VB.

(Nota:. Todas estas observaciones se convertirá instantáneamente en mal cuando C# 4 se libera)

5

Utilice esta definición de AccessibleObjectFromWindow lugar:

[DllImport("Oleacc.dll")] 
    private static extern int AccessibleObjectFromWindow(
     int hwnd, uint dwObjectID, 
     byte[] riid, 
     [MarshalAs(UnmanagedType.IUnknown)]ref object ptr); 
+2

+1, funciona muy bien para obtener una instancia 'Access.Application' sin utilizar PIA o definir una interfaz ficticia. – Heinzi

3

El código en la primera respuesta funcionó de maravilla. Esto es lo mismo para Word, más un poco de acción dinámica de .NET 4.0 en la parte inferior.

// http://stackoverflow.com/questions/779363/how-to-use-use-late-binding-to-get-excel-instance 
// ReSharper disable InconsistentNaming 

using System; 
using System.Runtime.InteropServices; 
using System.Globalization; 
using System.Reflection; 
using System.Text; 

namespace LateBindingWord { 
    /// <summary> Interface definition for Word.Window interface </summary> 
    [Guid("00020962-0000-0000-C000-000000000046")] 
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
    public interface IWordWindow { 
    } 

    /// <summary> 
    /// This class is needed as a workaround to http://support.microsoft.com/default.aspx?scid=kb;en-us;320369 
    /// Excel automation will fail with the follwoing error on systems with non-English regional settings: 
    /// "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))" 
    /// </summary> 
    class UiLanguageHelper : IDisposable { 
     private readonly CultureInfo _currentCulture; 

     public UiLanguageHelper() { 
      // save current culture and set culture to en-US 
      _currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture; 
      System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
     } 

     public void Dispose() { 
      // reset to original culture 
      System.Threading.Thread.CurrentThread.CurrentCulture = _currentCulture; 
     } 
    } 

    class Program { 
     [DllImport("user32.dll", SetLastError = true)] 
     static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

     [DllImport("Oleacc.dll")] 
     static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, out IWordWindow ptr); 

     public delegate bool EnumChildCallback(int hwnd, ref int lParam); 

     [DllImport("User32.dll")] 
     public static extern bool EnumChildWindows(int hWndParent, EnumChildCallback lpEnumFunc, ref int lParam); 

     [DllImport("User32.dll")] 
     public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount); 

     public static bool EnumChildProc(int hwndChild, ref int lParam) { 
      var buf = new StringBuilder(128); 
      GetClassName(hwndChild, buf, 128); 
      Console.WriteLine(buf.ToString()); 

      if (buf.ToString() == "_WwG") { 
       lParam = hwndChild; 
       return false; 
      } 
      return true; 
     } 

     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); 
      } 
     } 
    } 
} 
+0

Sé que es muy antiguo, pero me está dando ** hr = -2147467259 **, ¿sabes por qué podría ser así? –

Cuestiones relacionadas