jumaris / delphi-detours-library

Automatically exported from code.google.com/p/delphi-detours-library
0 stars 1 forks source link

COM position #16

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello!

I see this example in the demo..

procedure TMain.BtnEnableHookClick(Sender: TObject);
begin
  if not Assigned(Trampoline_FileOpenDialog_Show) then
    @Trampoline_FileOpenDialog_Show := InterceptCreate(FileOpenDialog, 3, @FileOpenDialog_Show_Hook); // Index???
  Trampoline_FileOpenDialog_SetTitle := InterceptCreate(FileOpenDialog, 17, @FileOpenDialog_SetTitle_Hook); // Index???
end;

But how do you get an index of the procedure/function of interface? Is there a 
way to calculate this? 

Thanks.

Original issue reported on code.google.com by david.lo...@gmail.com on 15 Jan 2015 at 4:44

GoogleCodeExporter commented 9 years ago
Hi,
Here is a simple example:
{
IModalWindow is inherited from IInterface ==> We need to start counting from 
this one.
- Start count from the top to the bottom.
- Counting must be zero based.
}

type
  IInterface = interface
    ['{00000000-0000-0000-C000-000000000046}']
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;{==> Index 0 <<==}
    function _AddRef: Integer; stdcall;{==> Index 1 <<==}
    function _Release: Integer; stdcall;{==> Index 2 <<==}
  end;

  IModalWindow = interface(IInterface) 
    [SID_IModalWindow]
    function Show(hwndParent: HWND): HRESULT; stdcall;{==> Index 3 !!<<==}
  end;
Please refer to DDetours wikipage for more details about how to get the index!

Original comment by ismspi...@gmail.com on 15 Jan 2015 at 5:02

GoogleCodeExporter commented 9 years ago
Are you sure? Start has 3 index. But still not work!

unit Test;

interface

uses
  ComObj,
  ActiveX,
  DDetours,
  UrlMon;

const
  CLSID_HttpProtocol: TGUID = '{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}';

var
  FDefaultSink: IInternetProtocolRoot;
  IInternetProtocolRoot_Start: function(szUrl: LPCWSTR; OIProtSink: IInternetProtocolSink; OIBindInfo: IInternetBindInfo; grfPI, dwReserved: DWORD): HResult;

implementation

function TInternetProtocolRoot_Start(szUrl: LPCWSTR; OIProtSink: 
IInternetProtocolSink; OIBindInfo: IInternetBindInfo; grfPI, dwReserved: 
DWORD): HResult;
begin
 //
end;

initialization
  OleCheck(CoCreateInstance(CLSID_HttpProtocol, nil, CLSCTX_INPROC_SERVER, IUnknown, FDefaultSink));
  @IInternetProtocolRoot_Start := InterceptCreate(FDefaultSink, 3, @TInternetProtocolRoot_Start);
end.

Original comment by david.lo...@gmail.com on 15 Jan 2015 at 5:05

GoogleCodeExporter commented 9 years ago
I have no experience with IInternetProtocolRoot.
But it seem that you are hooing incorrectly!
First parameter of TInternetProtocolRoot_Start must be self!

Original comment by ismspi...@gmail.com on 15 Jan 2015 at 5:35

GoogleCodeExporter commented 9 years ago
I am trying to follow this article. 

http://web.archive.org/web/20130313164317/http://www.blackfishsoftware.com/blog/
don/passthroughapp_bho_toolbar_intercepting_requests_responses

var
  FDefaultSink: IInternetProtocol;
  FRoot: IInternetProtocolRoot;
  IInternetProtocolRoot_Start: function(Self: IInternetProtocolRoot; szUrl: LPCWSTR; OIProtSink: IInternetProtocolSink; OIBindInfo: IInternetBindInfo; grfPI, dwReserved: DWORD): HResult; stdcall;

implementation

procedure TForm1.Button1Click(Sender: TObject);
begin
  WebBrowser1.Navigate('www.google.com'); // this should fire Start()
end;

function Start(Self: IInternetProtocolRoot; szUrl: LPCWSTR; OIProtSink: 
IInternetProtocolSink; OIBindInfo: IInternetBindInfo; grfPI, dwReserved: 
DWORD): HResult; stdcall;
begin
  Result := INET_E_USE_DEFAULT_PROTOCOLHANDLER;
end;

initialization
  OleCheck(CoCreateInstance(CLSID_HttpProtocol, nil, CLSCTX_INPROC_SERVER, IUnknown, FDefaultSink));
  FRoot := FDefaultSink as IInternetProtocolRoot;
  @IInternetProtocolRoot_Start := InterceptCreate(FRoot, 3, @Start);
end.

Original comment by david.lo...@gmail.com on 15 Jan 2015 at 5:41

GoogleCodeExporter commented 9 years ago
This was the fix..

var
  FProtocol: IInternetProtocol;
  FRoot: IInternetProtocolEx;
  StartEx_Original: function(Self: IInternetProtocolRoot; Uri: IUri; OIProtSink: IInternetProtocolSink; OIBindInfo: IInternetBindInfo; grfPI, dwReserved: DWORD): HResult; stdcall;

implementation

function StartEx_Hook(Self: IInternetProtocolRoot; Uri: IUri; OIProtSink: 
IInternetProtocolSink; OIBindInfo: IInternetBindInfo; grfPI, dwReserved: 
DWORD): HResult; stdcall;
var
  Url: WideString;
begin
  Uri.GetDisplayUri(Url);
  Result := StartEx_Original(Self, Uri, OIProtSink, OIBindInfo, grfPI, dwReserved);
end;

initialization
  OleCheck(CoCreateInstance(CLSID_HttpProtocol, nil, CLSCTX_SERVER, IUnknown, FProtocol));
  FProtocol.QueryInterface(IInternetProtocolEx, FRoot);
  @StartEx_Original := InterceptCreate(FRoot, 13, @StartEx_Hook);
end.

Original comment by david.lo...@gmail.com on 15 Jan 2015 at 6:05

GoogleCodeExporter commented 9 years ago
Why you don't hook IWebBrowser.Navigate ?
//---------------------------------------
const
  CLSID_InternetExplorer: TGUID = '{8856F961-340A-11D0-A96B-00C04FD705A2}';

var
  FWebBrowser: IWebBrowser;
  TrampoNavigate: procedure(Self: PByte; const URL: WideString; const Flags: OleVariant; const TargetFrameName: OleVariant; const PostData: OleVariant;
    const Headers: OleVariant); safecall;

procedure NavigateHooked(Self: PByte; const URL: WideString; const Flags: 
OleVariant; const TargetFrameName: OleVariant; const PostData: OleVariant;
  const Headers: OleVariant); safecall;
begin
  ShowMessage('NavigateHooked');
  TrampoNavigate(Self, URL, Flags, TargetFrameName, PostData, Headers);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  WebBrowser1.Navigate('www.google.com');
end;

initialization

CoCreateInstance(CLSID_InternetExplorer, nil, CLSCTX_INPROC_SERVER, 
IID_IWebBrowser, FWebBrowser);
@TrampoNavigate := InterceptCreate(FWebBrowser, 11, @NavigateHooked);

Original comment by ismspi...@gmail.com on 15 Jan 2015 at 6:26

GoogleCodeExporter commented 9 years ago
Because this cannot get all the requests :)

images, scripts etc etc.

Original comment by david.lo...@gmail.com on 15 Jan 2015 at 6:49