简体   繁体   English

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

[英]Find handle of a ActiveX user control inside IE

How can I programatically find the handle of a user control in a webpage running on IE? 如何以编程方式在IE上运行的网页中找到用户控件的句柄?

I'm able to find it using Spy++ but since the handle keeps changing I'm stuck. 我能够使用Spy ++找到它,但由于手柄不断变化,我陷入困境。

I've been trying using FindWindow() but no luck :( I also wonder if I am doing something wrong or it simply only work for Windows... 我一直在尝试使用FindWindow(),但没有运气:(我也想知道我做错了什么,或者它只是适用于Windows ...

Thanks in advance, 提前致谢,

Zubrowka 朱波罗卡

I had a similar problem finding a PDF ActiveX Control inside a IE control in WPF. 我在WPF中的IE控件中找到PDF ActiveX控件时遇到了类似的问题。

To overcome the problem I used the EnumChildWindows API to find the correct child window and thus get its handle. 为了解决这个问题,我使用了EnumChildWindows API来查找正确的子窗口,从而得到它的句柄。

I'll include as much code as I can. 我将尽可能多地包含代码。

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

transcribed in a rush so I hope it is both helpful and accurate. 匆忙转录,所以我希望它既有用又准确。

If the ActiveX control is windowed, then you can query its IOleWindow interface to get the window handle. 如果ActiveX控件是窗口化的,那么您可以查询其IOleWindow接口以获取窗口句柄。

Before you query interfaces from the ActiveX , you need to review the page's HTML to find a way to identify the activex in the document, such as element id. 从ActiveX查询接口之前,需要查看页面的HTML以找到识别文档中activex的方法,例如元素id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM