0

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.

ArielB
  • 1,184
  • 2
  • 11
  • 36

1 Answers1

1
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;
Denis Anisimov
  • 3,297
  • 1
  • 10
  • 18
  • Isnt IWebBrowser2 is linked to Internet Explorer? i need windows explorer – ArielB Feb 06 '18 at 14:19
  • 1
    IWebBrowser2 is a common interface. Windows explorer uses it too. I tested this code before posting. – Denis Anisimov Feb 06 '18 at 15:32
  • allright, i've converted your code and indeed got the searchbox, but as before - i get 0x80004002 when calling "GetText" (E_NOINTERFACE).. are you able to retrieve the text? – ArielB Feb 07 '18 at 11:13
  • I have the same error when input field is empty. But when input field contains any text - I have correct result. – Denis Anisimov Feb 07 '18 at 11:30