2

Web ブラウザー コントロールでダウンロード ポップアップを閉じる必要があります (ユーザーがファイルをダウンロードできないようにします)。どうすればこれを達成できますか?

私はこれを見つけました: How to block downloads in .NET WebBrowser control?

そして、私は2番目の答えを使用しました。動作していますが、問題があります。作成されたオブジェクトの GetText を呼び出すと、スレッド全体がブロックされているようです。私はそれに対する解決策を持っていません。

private static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
    if (idObject == 0 && idChild == 0)
    {
        if(eventType == EVENT_OBJECT_CREATE)
        {
            string text = GetText(hwnd);
            if (text.Contains("File Download"))
                SendMessage(hwnd, 0x0010, IntPtr.Zero, IntPtr.Zero); //close window
        }
    }
}

public static string GetText(IntPtr hWnd)
{
    int length = GetWindowTextLength(hWnd); //my app is freezing here - i think it's because i'm calling it from message loop.
    StringBuilder sb = new StringBuilder(length + 1);
    GetWindowText(hWnd, sb, sb.Capacity);
    return sb.ToString();
}

//編集

わかりました、提案してくれた@sgorozcoに感謝します。今私は SetWindowsHookEx と WH_CBT を使用しています。次に、メッセージ ループで HCBT_CREATEWND イベントをキャッチしています。しかし、lparm から CBT_CREATEWND を取得する際に問題があります。「マネージ デバッグ アシスタント 'FatalExecutionEngineError'」例外が発生します。

これが私の現在のコードです:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct CREATESTRUCT
        {
            public IntPtr lpCreateParams;
            public IntPtr hInstance;
            public IntPtr hMenu;
            public IntPtr hwndParent;
            public int cy;
            public int cx;
            public int y;
            public int x;
            public int style;
            public string lpszName;
            public string lpszClass;
            public int dwExStyle;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct CBT_CREATEWND
        {
            public IntPtr lpcs;
            public IntPtr hwndInsertAfter;

        }

private static IntPtr MessageLoopFuctnion(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code < 0)
            {
                return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
            }

            if(code == 3)
            {
                CBT_CREATEWND info;
                info = (CBT_CREATEWND)Marshal.PtrToStructure(lParam, typeof(CBT_CREATEWND));

                CREATESTRUCT info1;
                info1 = (CREATESTRUCT)Marshal.PtrToStructure(info.lpcs, typeof(CREATESTRUCT)); //here exception is throwing

                if (info1.lpszName != null && info1.lpszName.Contains("File Download")))
                    SendMessage(wParam, 0x0010, IntPtr.Zero, IntPtr.Zero); //close popup

                //Marshal.FreeHGlobal(info.lpcs); //error, why?
                //Marshal.FreeHGlobal((IntPtr)lParam.ToUInt64());
            }


            return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
        }

//set hook
            IntPtr hWinEventHook = SetWindowsHookEx(5, myCallbackDelegate, user32DLL, 0);

//編集2

ここに私の定義があります:

 private delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
        private static HookProc myCallbackDelegate = new HookProc(MessageLoopFuctnion);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetWindowsHookEx(int hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll")]
        static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

私の myCallbackDelegate は静的フィールドであるため、確実に収集される GC ではありません。一時的に、500ms ごとにすべてのウィンドウを列挙し、「ファイルのダウンロード」というテキストを含むダイアログを探します。しかし、それは醜い解決策です。

4

1 に答える 1