unit uKiller;<br><br>{$WARN SYMBOL_PLATFORM OFF}<br><br>interface<br><br>uses<br> Windows, ActiveX, Classes, ComObj, Shdocvw, Dialogs, Variants;<br><br>type<br> TAdKillerBHO = class(TComObject, IObjectWithSite, IDispatch)<br> private<br> FIESite: IUnknown;<br> FIE: IWebBrowser2;<br> FCPC: IConnectionPointContainer;<br> FCP: IConnectionPoint;<br> FCookie: Integer;<br> protected<br> function SetSite(const pUnkSite: IUnknown): HResult; stdcall;<br> function GetSite(const riid: TIID; out site: IUnknown): HResult; stdcall;<br> function GetTypeInfoCount(out Count: Integer): HResult; stdcall;<br> function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult;<br> stdcall;<br> function GetIDsOfNames(const IID: TGUID; Names: Pointer;<br> NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;<br> function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;<br> Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;<br> stdcall;<br> procedure DoBeforeNavigate2(const pDisp: IDispatch; var URL: OleVariant; var Flags: OleVariant;<br> var TargetFrameName: OleVariant; var PostData: OleVariant;<br> var Headers: OleVariant; var Cancel: WordBool);<br> end;<br><br>const<br> AdKillerBHO: TGUID = '{A692062A-11A1-461B-BE98-B520F01F96FC}';<br><br>implementation<br><br>uses ComServ, Sysutils, ComConst;<br><br>var<br> WM_ADKILLER: Cardinal;<br><br>{ TAdKillerBHO }<br><br>procedure TAdKillerBHO.DoBeforeNavigate2(const pDisp: IDispatch; var URL,<br> Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool);<br>var<br> hOwner: THandle;<br>begin<br> if FIE.ToolBar = 0 then<br> begin<br> hOwner := FindWindow('TfrmAdKiller', PAnsiChar('秋风网页广告拦截器1.2'));<br> if hOwner <> 0 then<br> begin<br> FIE.Quit;<br> PostMessage(hOwner, WM_ADKILLER, 0, GlobalAddAtom(PAnsiChar(VarToStrDef(URL, ''))));<br> end;<br> end;<br>end;<br><br>function TAdKillerBHO.GetIDsOfNames(const IID: TGUID; Names: Pointer;<br> NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;<br>begin<br> Result := E_NOTIMPL;<br>end;<br><br>function TAdKillerBHO.GetSite(const riid: TIID;<br> out site: IInterface): HResult;<br>begin<br> if Supports(FIESite, riid, site) then<br> Result := S_OK<br> else<br> Result := E_NOINTERFACE;<br>end;<br><br>function TAdKillerBHO.GetTypeInfo(Index, LocaleID: Integer;<br> out TypeInfo): HResult;<br>begin<br> Result := E_NOTIMPL;<br> pointer(TypeInfo) := nil;<br>end;<br><br>function TAdKillerBHO.GetTypeInfoCount(out Count: Integer): HResult;<br>begin<br> Result := E_NOTIMPL;<br> Count := 0;<br>end;<br><br>procedure BuildPositionalDispIds(pDispIds: PDispIdList; const dps: TDispParams);<br>var<br> i: integer;<br>begin<br> Assert(pDispIds <> nil);<br> for i := 0 to dps.cArgs - 1 do<br> pDispIds^ := dps.cArgs - 1 - i;<br> if (dps.cNamedArgs <= 0) then<br> Exit;<br> for i := 0 to dps.cNamedArgs - 1 do<br> pDispIds^[dps.rgdispidNamedArgs^] := i;<br>end;<br><br>function TAdKillerBHO.Invoke(DispID: Integer; const IID: TGUID;<br> LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,<br> ArgErr: Pointer): HResult;<br>var<br> dps: TDispParams absolute Params;<br> bHasParams: boolean;<br> pDispIds: PDispIdList;<br> iDispIdsSize: integer;<br>begin<br> pDispIds := nil;<br> iDispIdsSize := 0;<br> bHasParams := (dps.cArgs > 0);<br> if (bHasParams) then<br> begin<br> iDispIdsSize := dps.cArgs * SizeOf(TDispId);<br> GetMem(pDispIds, iDispIdsSize);<br> end;<br> try<br> if (bHasParams) then<br> BuildPositionalDispIds(pDispIds, dps);<br> Result := S_OK;<br> case DispId of<br> 250:<br> DoBeforeNavigate2(IDispatch(dps.rgvarg^[pDispIds^[0]].dispval),<br> POleVariant(dps.rgvarg^[pDispIds^[1]].pvarval)^,<br> POleVariant(dps.rgvarg^[pDispIds^[2]].pvarval)^,<br> POleVariant(dps.rgvarg^[pDispIds^[3]].pvarval)^,<br> POleVariant(dps.rgvarg^[pDispIds^[4]].pvarval)^,<br> POleVariant(dps.rgvarg^[pDispIds^[5]].pvarval)^,<br> dps.rgvarg^[pDispIds^[6]].pbool^);<br> 253:<br> FCP.Unadvise(FCookie);<br> else<br> Result := DISP_E_MEMBERNOTFOUND;<br> end;<br> finally<br> if (bHasParams) then FreeMem(pDispIds, iDispIdsSize);<br> end;<br>end;<br><br>function TAdKillerBHO.SetSite(const pUnkSite: IInterface): HResult;<br>begin<br> Result := E_FAIL;<br> FIESite := pUnkSite;<br> if not Supports(FIESite, IWebBrowser2, FIE) then Exit;<br> if not Supports(FIE, IConnectionPointContainer, FCPC) then Exit;<br> FCPC.FindConnectionPoint(DWebBrowserEvents2, FCP);<br> FCP.Advise(Self, FCookie);<br> Result := S_OK;<br>end;<br><br>procedure DeleteRegKeyValue(Root: DWORD; Key: string; ValueName: string = '');<br>var<br> KeyHandle: HKEY;<br>begin<br> if ValueName = '' then<br> RegDeleteKey(Root, PChar(Key));<br> if RegOpenKey(Root, PChar(Key), KeyHandle) = ERROR_SUCCESS then<br> try<br> RegDeleteValue(KeyHandle, PChar(ValueName));<br> finally<br> RegCloseKey(KeyHandle);<br> end;<br>end;<br><br>procedure CreateRegKeyValue(Root: DWORD; const Key, ValueName, Value: string);<br>var<br> Handle: HKey;<br> Status, Disposition: Integer;<br>begin<br> Status := RegCreateKeyEx(ROOT, PChar(Key), 0, '',<br> REG_OPTION_NON_VOLATILE, KEY_READ or KEY_WRITE, nil, Handle,<br> @Disposition);<br> if Status = 0 then<br> begin<br> Status := RegSetValueEx(Handle, PChar(ValueName), 0, REG_SZ,<br> PChar(Value), Length(Value) + 1);<br> RegCloseKey(Handle);<br> end;<br> if Status <> 0 then<br> raise EOleRegistrationError.CreateRes(@SCreateRegKeyError);<br>end;<br><br>type<br> TIEAdvBHOFactory = class(TComObjectFactory)<br> public<br> procedure UpdateRegistry(Register: Boolean); override;<br> end;<br><br>{ TIEAdvBHOFactory }<br><br>procedure TIEAdvBHOFactory.UpdateRegistry(Register: Boolean);<br>begin<br> inherited;<br> if Register then<br> CreateRegKeyValue(HKEY_LOCAL_MACHINE, 'Software/Microsoft/Windows/CurrentVersion/explorer/Browser Helper Objects/' + GuidToString(ClassID), '', '')<br> else<br> DeleteRegKeyValue(HKEY_LOCAL_MACHINE, 'Software/Microsoft/Windows/CurrentVersion/explorer/Browser Helper Objects/' + GuidToString(ClassID), '');<br>end;<br><br>initialization<br> TIEAdvBHOFactory.Create(ComServer, TAdKillerBHO, AdKillerBHO,<br> 'TAdKillerBHO', '', ciMultiInstance, tmApartment);<br> WM_ADKILLER := RegisterWindowMessage('AdKiller');<br>end.