在IE中查找ActiveX用户控件的句柄

时间:2011-10-27 16:08:17

标签: c# winapi activex handle user32

如何以编程方式在IE上运行的网页中找到用户控件的句柄?

我能够使用Spy ++找到它,但由于手柄不断变化,我陷入困境。

我一直在尝试使用FindWindow(),但没有运气:(我也想知道我做错了什么,或者它只是适用于Windows ...

提前致谢,

朱波罗卡

2 个答案:

答案 0 :(得分:1)

我在WPF中的IE控件中找到PDF ActiveX控件时遇到了类似的问题。

为了克服这个问题,我使用EnumChildWindows API来查找正确的子窗口,从而得到它的句柄。

我将尽可能多地包含代码。

private static IntPtr FindPdfControlWindow(IntPtr parentHandle)
{
    IntPtr result = IntPtr.Zero;
    IntPtr matchPointer = IntPtr.Zero;
    try
    {
        //allocate unmanaged memory for the result of the callback delegate
        matchPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
        Marshal.WriteIntPtr(matchPointer, IntPtr.Zero)

        //instantiate the delegate and pass to the API
        NativeMethods.EnumWindowProc windowChecker = CheckForPdfControlWindow;
        if (!NativeMethods.EnumChildWindows(parentHandle, 
                                                windowChecker, 
                                                matchPointer))
    }
    finally
    {
        if (matchPointer != IntPtr.Zero) Marshal.FreeHGlobal(matchPointer);
    }
    return result;
}

private static bool CheckForPdfControlWindow(IntPtr handle,
                                                IntPtr matchPointer)
{
    int captionLength = NativeMehtods.GetWindowTextLength(handle);
    if (captionLength > 0)
    {
        StringBuilder buffer = new StringBuilder(captionLength + 1);
        NativeMethods.GetWindowText(handle, buffer, buffer.Capacity);
        if (buffer.ToString().Contains("Adobe"))
        {
            Marhsal.WriteIntPtr(matchPointer, handle)
            return false;
        }
    }
    return true;
}

private static class NativeMethods
{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool EnumChildWindows(IntPtr window,
                                                    EnumWindowProc callback,
                                                    IntPtr i);

    internal delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSer.Auto)]
    internal static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    internal static extern int GetWindowText(IntPtr hWnd,
                                               StringBuilder lpString,
                                               int nMaxCount);
}

匆忙转录,所以我希望它既有用又准确。

答案 1 :(得分:1)

如果ActiveX控件被窗口化,那么您可以查询其IOleWindow接口以获取窗口句柄。

query interfaces from the ActiveX之前,您需要查看页面的HTML以找到识别文档中activex的方法,例如元素ID。