c++explorershell-namespace-extension

How to retrieve the ISearchBoxInfo interface?


i would like to capture the Windows' search box, for that i found out i can use the ISearchBoxInfo interface:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd562062(v=vs.85).aspx

I have the handle of the windows explorer - but i'm not really sure how to get that interface.. Any assistance would be appreciated.


Solution

  • function FindSearchBoxInfo(AWnd: HWND): ISearchBoxInfo;
    var
      ShellWindows: IShellWindows;
      ExplorerIndex: Integer;
      Dispatch: IDispatch;
      WebBrowser2: IWebBrowser2;
      ServiceProvider: IServiceProvider;
    begin
      Result := nil;
      if Succeeded(CoCreateInstance(CLASS_ShellWindows, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IShellWindows, ShellWindows)) then
        begin
          for ExplorerIndex := ShellWindows.Count - 1 downto 0 do
            begin
              Dispatch := ShellWindows.Item(ExplorerIndex);
              if Assigned(Dispatch) then
                begin
                  if Succeeded(Dispatch.QueryInterface(IWebBrowser2, WebBrowser2)) then
                    begin
                      if WebBrowser2.HWND = AWnd then
                        begin
                          if Succeeded(Dispatch.QueryInterface(IServiceProvider, ServiceProvider)) then
                            begin
                              ServiceProvider.QueryService(SID_SSearchBoxInfo, ISearchBoxInfo, Result);
                              ServiceProvider := nil;
                            end;
                          WebBrowser2 := nil;
                          Dispatch := nil;
                          ShellWindows := nil;
                          Exit;
                        end;
                      WebBrowser2 := nil;
                    end;
                  Dispatch := nil;
                end;
            end;
          ShellWindows := nil;
        end;
    end;