I have an application that uses TEmbeddedWb to automate data scrapping tasks.
Some web-sites show messages / popup boxes when my app navigates to it, and these makes the process slower.
I want to block any messagebox that TWebbrowser could show.
I'm already setting the 'silent' property to true, and also setting the onshowmessage method as follow, but still the messageboxes are show. Any hints ?
function TForm1.webShowMessage(Sender: TObject; HWND: Cardinal; lpstrText,
lpstrCaption: PWideChar; dwType: Integer; lpstrHelpFile: PWideChar; dwHelpContext: Integer;
var plResult: Integer): HRESULT;
begin
plresult := S_OK;
end;
I could achieve these task by making some changes on TEmbeddedWb source, specifically on the functionbelow :
procedure TEmbeddedWB.HandleDialogBoxes(var AMsg: Messages.TMessage);
Here is the change :
DlgClss := GetWinClass(PopHandle);
WinClss := GetWinClass(Windows.GetParent(PopHandle));
DlgCaption := GetWinText(PopHandle);
if (DlgClss = 'Internet Explorer_TridentDlgFrame') or ((DlgClss = '#32770'))
// comment here to make all windows be evaluated
{and (WinClss <> 'TApplication') and
(FindControl(Windows.GetParent(PopHandle)) = nil))}
then
begin
if (WinClss = 'TApplication') or (FindControl(Windows.GetParent(PopHandle)) <> nil) then
begin
if pos('web browser',lowercase(DlgCaption)) = 0 then
exit;
end;
I just add this code Result := S_OK; on ShowMessage Event and no more popup message:
function TForm1.webShowMessage(Sender: TObject; HWND: Cardinal; lpstrText,
lpstrCaption: PWideChar; dwType: Integer; lpstrHelpFile: PWideChar; dwHelpContext: Integer;
var plResult: Integer): HRESULT;
begin
Result := S_OK;
end;
Related
How to detect that Taskbar Thumbnail Preview is being drawn (because user is moving mouse over taskbar button) or is currently appearing on screen?
Why the code below does not make log entries when WM_DWMSENDICONICTHUMBNAIL msg should be arriving to form?
procedure TStartInfoForm.FormCreate(Sender: TObject);
begin
SetWindowAttribute(DWMWA_HAS_ICONIC_BITMAP);
end;
function TStartInfoForm.SetWindowAttribute(dwAttr: DWORD): HRESULT;
var
Attr: function(hwnd: hwnd; dwAttribute: DWORD; pvAttribute: Pointer;
cbAttribute: DWORD): HRESULT; stdcall;
hndDLLHandle : THandle;
EnableAttribute : DWORD;
begin
EnableAttribute := DWORD(true);
try
hndDLLHandle := loadLibrary('dwmapi.dll');
if hndDLLHandle <> 0 then
begin
Attr := getProcAddress(hndDLLHandle, 'DwmSetWindowAttribute');
if addr(Attr) <> nil then
Result := Attr(Handle, dwAttr, #EnableAttribute, SizeOf(EnableAttribute));
end;
finally
freeLibrary(hndDLLHandle);
end;
end;
procedure TStartInfoForm.WndProc(var Msg : TMessage);
begin
DebugNote(0,'wndproc '+inttohex(msg.Msg,3)+' w '+inttohex(Msg.wparam,3)+' l '+inttohex(Msg.lparam,3));
case Msg.Msg of
WM_DWMSENDICONICTHUMBNAIL :
begin
DebugNote(0,' iconiclive');
end;
WM_DWMSENDICONICLIVEPREVIEWBITMAP:
begin
DebugNote(0,' iconiclive2 ');
end;
WM_WINDOWPOSCHANGING:
begin
// MessageBeep(1);
end ;
WM_WINDOWPOSCHANGED:
begin
// MessageBeep(1);
end
end;
inherited;// call this for default behaveour
end;
I'm using a TWebBrowser to display a WYSIWYG HTML editor and I've added some handlers to catch keyboard and mouse events so I can integrate this editor into my application flow. This browser is integrated in a custom TPanel, TPanelEditorHTML.
This is the way I'm doing it, following some tips from this answer:
//Create the procedure type to assign the event
THTMLProcEvent = procedure(Sender: TObject; Event: IHTMLEventObj) of object;
//Create a new class for manage the event from the twebbrowser
THTMLBrowserEventLink = class(TInterfacedObject, IDispatch)
private
FOnEvent: THTMLProcEvent;
private
constructor Create(Handler: THTMLProcEvent);
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
public
property OnEvent: THTMLProcEvent read FOnEvent write FOnEvent;
end;
On my container for the TWebBrowser I have this:
FOnKeyDownConnector: IDispatch;
FOnClickConnector: IDispatch;
FOnKeyDownConnectorIFrame: IDispatch;
FOnClickConnectorIFrame: IDispatch;
procedure BrowserIHTMLDocument2OnKeyDown(Sender: TObject; Event: IHTMLEventObj);
procedure BrowserIHTMLDocument2OnClick(Sender: TObject; Event: IHTMLEventObj);
procedure IframeIHTMLDocument2OnKeyDown(Sender: TObject; Event: IHTMLEventObj);
procedure IframeIHTMLDocument2OnClick(Sender: TObject; Event: IHTMLEventObj);
Where BrowserIHTMLDocument2OnKeyDown, etc are the procedures where I do all the work integrating the HTML editor data into my application
I create the handlers on startup
constructor TPanelEditorHTML.Create(AOwner: TComponent);
begin
inherited;
// ...
FNavegador := TGENBrowser.Create(self);
FOnKeyDownConnector := THTMLBrowserEventLink.Create(BrowserIHTMLDocument2OnKeyDown);
FOnClickConnector := THTMLBrowserEventLink.Create(BrowserIHTMLDocument2OnClick);
FOnKeyDownConnectorIFrame := THTMLBrowserEventLink.Create(IFrameIHTMLDocument2OnKeyDown);
FOnClickConnectorIFrame := THTMLBrowserEventLink.Create(IFrameIHTMLDocument2OnClick);
end;
And when I load the HTML editor I assign this handlers to a couple of elements in the DOM tree:
procedure TPanelEditorHTML.AsignarManejadores;
var
HTMLDocument2_A, HTMLDocument2_B: IHTMLDocument2;
begin
HTMLDocument2_A := ExtraerIframeEditor;
HTMLDocument2_B := (FNavegador.Document AS IHTMLDocument2);
if (HTMLDocument2_A = nil) or (HTMLDocument2_B = nil) then
Exit;
if (FOnKeyDownConnectorIFrame <> nil) then
HTMLDocument2_A.onkeydown := FOnKeyDownConnectorIFrame;
if (FOnClickConnectorIFrame <> nil) then
HTMLDocument2_A.onclick := FOnClickConnectorIFrame;
if (FOnKeyDownConnector <> nil) then
HTMLDocument2_B.onkeydown := FOnKeyDownConnector;
if (FOnClickConnector <> nil) then
HTMLDocument2_B.onclick := FOnClickConnector;
end;
When the user ends the editing I remove this handlers
procedure TPanelEditorHTML.DesconectarManejadores;
var
HTMLDocument2 : IHTMLDocument2;
begin
HTMLDocument2 := ExtraerIframeEditor;
if (HTMLDocument2 <> nil) then
begin
HTMLDocument2.onkeydown := Unassigned; //assign the event handler
HTMLDocument2.onclick := Unassigned; //assign the event handler
end;
HTMLDocument2:=(FNavegador.Document AS IHTMLDocument2);
if (HTMLDocument2 <> nil) then
begin
HTMLDocument2.onkeydown := Unassigned; //assign the event handler
HTMLDocument2.onclick := Unassigned; //assign the event handler
end;
end;
My problem is on the TPanelEditorHTML destructor. This leads to a memory leak of the four THTMLBrowserEventLink. If I try to FreeAndNil the handlers I get a runtime error.
destructor TPanelEditorHTML.Destroy;
begin
FDataLink.Free;
FOnKeyDownConnector := Unassigned;
FOnClickConnector := Unassigned;
FOnKeyDownConnectorIFrame := Unassigned;
FOnClickConnectorIFrame := Unassigned;
inherited;
end;
I found this article about memory leaks and I tried to replace both methods that were making copies to no avail.
Am I missing something?
As #DalijaPrasnikar said there is a superfluous _AddRef in THTMLEventLink.Create, wich I copied from the solution proposed in this answer.
Changing the constructor of THTMLBrowserEventLink to this:
constructor THTMLBrowserEventLink.Create(Handler: THTMLProcEvent);
begin
inherited Create;
FOnEvent := Handler;
end;
Avoids the memory leak.
I've been trying to use the technique shown in the answer to this q
Detect when the active element in a TWebBrowser document changes
to implement a DIY version of MS Word's Automation events.
A fuller extract from my app is below, from which you'll be able to see the
declaration of the variables in these methods:
procedure TForm1.StartWord;
var
IU : IUnknown;
begin
IU := CreateComObject(Class_WordApplication);
App := IU as WordApplication;
App.Visible := True;
IEvt := TEventObject.Create(DocumentOpen);
end;
procedure TForm1.OpenDocument;
var
CPC : IConnectionPointContainer;
CP : IConnectionPoint;
Res : Integer;
MSWord : OleVariant;
begin
Cookie := -1;
CPC := App as IConnectionPointContainer;
Res := CPC.FindConnectionPoint(DIID_ApplicationEvents2, CP);
Res := CP.Advise(IEvt, Cookie);
MSWord := App;
WordDoc:= MSWord.Documents.Open('C:\Docs\Test.Docx');
end;
The StartWord routine works fine. The problem is in OpenDocument. The
value of Res returned by Res := CP.Advise(IEvt, Cookie); is $80040200
This isn't present amongst the HResult status codes in Windows.Pas and googling "ole error 80040200"
returns a few hits involving setting up Ado events from Delphi, but nothing
apparently relevant.
Anyway, the upshot of this is that the Invoke method of the EventObject is never
called, so I don't receive notifications of the WordApplication's events.
So, my question is what does this error $80040200 signify and/or how do I avoid it?
Fwiw, I've also tried connecting to the ApplicationEvents2 interface using this code
procedure TForm1.OpenDocument2;
var
MSWord : OleVariant;
II : IInterface;
begin
II := APP as IInterface;
InterfaceConnect(II, IEvt.EventIID, IEvt as IUnknown, Cookie);
MSWord := App;
WordDoc:= MSWord.Documents.Open('C:\Docs\Test.Docx');
end;
That executes without complaint, but again the EventObject's Invoke method is never
called.
If I drop a TWordApplication onto the blank form of a new application, the events
like OnDocumentOpen work fine. I'm mentioning that because it seems to confirm
that Delphi and MS Word (2007) are correctly set up on my machine.
Code:
uses
... Word2000 ...
TForm1 = class(TForm)
btnStart: TButton;
btnOpenDoc: TButton;
procedure FormCreate(Sender: TObject);
procedure btnOpenDocClick(Sender: TObject);
procedure btnStartClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure WordApplication1DocumentOpen(ASender: TObject; const Doc: _Document);
private
procedure DocumentOpen(Sender : TObject; DispID : Integer; var Params);
procedure StartWord; // see above for implementation
procedure OpenDocument; // --"--
procedure OpenDocument2; // --"--
public
WordDoc: OleVariant;
IEvt : TEventObject; // see linked question
Cookie : Integer;
App : WordApplication;
[...]
procedure TForm1.WordApplication1DocumentOpen(ASender: TObject; const Doc:
_Document);
begin
//
end;
I could post an MCVE instead, but it would mostly be just the code from the earlier answer.
This had me scratching my head for a while, I can tell you. Anyway, eventually the penny dropped
that the answer must lie in the difference between the way TEventObject is implemented
and TServerEventDispatch in OleServer.Pas.
The key is that TServerEventDispatch implements a custom QueryInterface
function TServerEventDispatch.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
begin
Result := S_OK;
Exit;
end;
if IsEqualIID(IID, FServer.FServerData^.EventIID) then
begin
GetInterface(IDispatch, Obj);
Result := S_OK;
Exit;
end;
Result := E_NOINTERFACE;
end;
whereas TEventObject does not. Once I'd spotted that, it was straightforward to extend
TEventObject to do likewise, and voila! the error returned by "CP.Advise" went away.
For completeness, I've included the complete source
of the updated TEventObject below. It is the
if IsEquallIID then ...
which makes the difference between
Res := CP.Advise(IEvt, Cookie);
returning the $800040200 error and zero for success. With the "if IsEquallIID then ..."
commented out, the RefCount on IEvt is 48 (!) after "CP.Advise ..." returns, by which time
TEventObject.QueryInterface has been called no less than 21 times.
I hadn't realised
previously (because TEventObject didn't previously have its own version to observe)
that when "CP.Advise ..." is executed, the COM system calls "TEventObject.QueryInterface"
with a succession of different IIDs until it returns S_Ok on one of them. When I have some free time, maybe I'll try to look up what these other IIDs are: as it is, the IID for IDispatch is quite a long way down the list of IIDs that are queried for, which seems strangely sub-optimal seeing as I'd have though that would be the one that IConnectionPoint.Advise would be trying to get.
Code for updated TEventObject is below. It includes a rather rough'n ready customization
of its Invoke() which is specific to handling Word's DocumentOpen event.
type
TInvokeEvent = procedure(Sender : TObject; const Doc : _Document) of object;
TEventObject = class(TInterfacedObject, IUnknown, IDispatch)
private
FOnEvent: TInvokeEvent;
FEventIID: TGuid;
protected
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
public
constructor Create(const AnEvent : TInvokeEvent);
property OnEvent: TInvokeEvent read FOnEvent write FOnEvent;
property EventIID : TGuid read FEventIID;
end;
constructor TEventObject.Create(const AnEvent: TInvokeEvent);
begin
inherited Create;
FEventIID := DIID_ApplicationEvents2;
FOnEvent := AnEvent;
end;
function TEventObject.GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
begin
Result := E_NOTIMPL;
end;
function TEventObject.GetTypeInfo(Index, LocaleID: Integer;
out TypeInfo): HResult;
begin
Pointer(TypeInfo) := nil;
Result := E_NOTIMPL;
end;
function TEventObject.GetTypeInfoCount(out Count: Integer): HResult;
begin
Count := 0;
Result := E_NOTIMPL;
end;
function TEventObject.Invoke(DispID: Integer; const IID: TGUID;
LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
ArgErr: Pointer): HResult;
var
vPDispParams: PDispParams;
tagV : TagVariant;
V : OleVariant;
Doc : _Document;
begin
vPDispParams := PDispParams(#Params);
if (vPDispParams <> Nil) and (vPDispParams^.rgvarg <> Nil) then begin
tagV := vPDispParams^.rgvarg^[0];
V := OleVariant(tagV);
Doc := IDispatch(V) as _Document;
// the DispID for DocumentOpen of Word's ApplicationEvents2 interface is 4
if (DispID = 4) and Assigned(FOnEvent) then
FOnEvent(Self, Doc);
end;
Result := S_OK;
end;
function TEventObject.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
begin
Result := S_OK;
Exit;
end;
if IsEqualIID(IID, EventIID) then
begin
GetInterface(IDispatch, Obj);
Result := S_OK;
Exit;
end;
Result := E_NOINTERFACE;
end;
Currently i check if a HWND is a console by EnumWindows and checking the ClassName.
function EnumWindows(AHandle: HWND; AParam: LPARAM): BOOL; stdcall;
var
classname: array[0.. 255] of Char;
begin
GetClassName(AHandle, classname, 255);
if classname = 'ConsoleWindowClass' then
begin
// do something
Result := False;
end
else
Result := True;
end;
I am wondering if there is a better way to accomplish something like this?
Would checking the Style (or/and ExStyle) be "better"?
You can use AttachConsole and FreeConsole do detect if other processes provide a console. One other thing to mind: there are processes with no console windows which allo AttachConsole - here GetConsoleWindow returns 0. There is a very good explanation of this behaviour in this github repository.
Declarations:
function AttachConsole(dwProcessID: Integer): Boolean; stdcall; external 'kernel32.dll';
function FreeConsole(): Boolean; stdcall; external 'kernel32.dll';
function GetConsoleWindow: HWND; stdcall; external kernel32;
Enumerate Processes:
procedure TForm2.FindConsoleWindows(AList: TListBox);
var
LProcHandle: THandle;
LResult, LNext: Boolean;
LProc: TProcessEntry32;
begin
aList.Items.Clear;
LProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
LResult := LProcHandle <> INVALID_HANDLE_VALUE;
if LResult then
try
LProc.dwSize := SizeOf(LProc);
LNext := Process32First(LProcHandle, LProc);
while LNext do begin
if AttachConsole(LProc.th32ProcessID) then
try
AList.Items.Add(IntToStr(LProc.th32ProcessID) + ' has a console ' + IntToStr(GetConsoleWindow()))
finally
FreeConsole();
end;
LNext := Process32Next(LProcHandle, LPRoc);
end;
finally
CloseHandle(LProcHandle);
end;
Credits:
JclSysInfo.pas to enumerate the processes
I am using Delphi 2007. I can successfully Post data to a web site using WebBrowser.Navigate, but afterwards, when that site returns a PDF, while it appears on the screen of the Browser, I cannot figure out how to acquire the PDF programmatically. I can see some text and HTML using Document.Body.InnerHTML, but not the PDF. Can someone demonstrate how to acquire the PDF which appears after the POST?
Thank yoU!
To get the text out of a PDF in the web browser, I found a solution using an open source unit called PushKeys to send keys to the web browser to select all the text (Control+A), copy it to the clipboard (Control+C) and then paste it to a TMemo or other control using PasteFromClipBoard. Tested in D2007.
WebBrowser.SetFocus; // set the focus to the TWebBrowser control
Sleep(1000); // 1 second delay to be sure webbrowser actually has focus
Application.ProcessMessages;
PushKeys('^a'); //send ctrl-a to select all text
Application.ProcessMessages;
WebBrowser.SetFocus;
PushKeys('^c'); //send ctrl-c to copy the text to clipboard
Sleep(1000); // 1 second delay to make sure clipboard finishes processing
Application.ProcessMessages;
Memo1.PasteFromClipBoard; // Paste the clipboard to a memo field.
// You could also use the clipbrd unit to handle the data.
//for Multi-page PDF's, you can send a PageDn key to get to the next page:
PushFnKey('PAGEDOWN');
You could use an IE4+ option for capturing all internet traffic using your own protocol. You can even hook the protocol http (IIRC) and when you need to load the data use the WIndows functions and/or Indy components.
This is a unit to do so:
{
This component allows you to dynamically create your own internet protocols for
Microsoft Internet Explorer 4+. Simply place the component on your form, set the protocol
property to something useful and set the Active property.
For example, when the Protocol is set to 'private', you can trap requests to
'private:anythingyoulike'.
}
unit UnitInternetProtocol;
// Developed by: R.A. Hornstra
// (C) 2001 ContinuIT BV
interface
uses
SysUtils, Windows, Classes, Messages;
type
TInternetProtocol = class;
{
When a request is made, the data must be returned in a TStream descendant.
The request is present in Request. The result should be saved in Stream.
When no data can be linked to the request, leave Stream equal to nil.
See #link(TInternetProtocol.OnRequestStream) and #link(TInternetProtocol.OnReleaseStream).
}
TProtocolRequest = procedure(Sender: TInternetProtocol; const Request: string;
var Stream: TStream) of object;
{
When a request is done by the Microsoft Internet Explorer it is done via an URL.
This URL starts with a protocol, than a colon and than a protocol specific resource identifier.
New protocols can be added dynamically and privately for each session.
This component will register / deregister new protocols to the Microsoft Internet Explorer.
You should set the name of the protocol with #link(Protocol), activate / deactivate the
protocol with #link(Active). The implementation of the protocol can be done with the
events #link(OnRequestStream) and #link(OnReleaseStream).
}
TInternetProtocol = class(TComponent)
private
FHandle: HWnd;
FActive: Boolean;
FProtocol: string;
FRequest: TProtocolRequest;
FRelease: TProtocolRequest;
procedure SetActive(const Value: Boolean);
procedure SetProtocol(const Value: string);
protected
procedure Loaded; override;
procedure Activate;
procedure Deactivate;
procedure WndProc(var Message: TMessage);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{
Setting this property will activate or deactivate the internet
}
property Active: Boolean read FActive write SetActive;
{
The protocol name must be specified. default, this is 'private'.
You should fill it here without the trailing colon (that's part of the URL notation).
Protocol names should be valid identifiers.
}
property Protocol: string read FProtocol write SetProtocol;
{
When a request is made on the selected protocol, this event is fired.
It should return a TStream, based upon the given Request.
The default behaviour of TInternetProtocol is freeing the stream.
To override or monitor this behaviour, use #link(OnRequestStream).
}
property OnRequestStream: TProtocolRequest read FRequest write FRequest;
{
When a stream is about to be released by TInternetProtocol, you can override the
default behaviour. By Setting the Stream variable to nil in the OnReleaseStream handler,
the stream will not be released by TInternetProtocol.
This is handy when you're implementing a caching system, or for some reason need control on
the creation and deletion to the streams.
The default behaviour of TInternetProtocol is freeing the stream.
}
property OnReleaseStream: TProtocolRequest read FRelease write FRelease;
end;
{
All exceptions raised by #link(TInternetProtocol) are of type EInternetException.
}
EInternetException = class(Exception);
procedure Register;
implementation
uses
ComObj, ActiveX, UrlMon, Forms;
resourcestring
strNotAValidProtocol = 'The Internet Protocol selected is not a valid protocol identifier';
// todo: move registration to separate file
procedure Register;
begin
Classes.RegisterComponents('Internet',[TInternetProtocol]);
end;
// forward declarations
procedure RegisterProtocol(Protocol: string; Handler: TInternetProtocol); forward;
procedure UnregisterProtocol(Protocol: string); forward;
const
IID_TInternetProtocolHandler: TGUID = '{B74826E0-1107-11D5-B166-0010D7090486}';
WM_STREAMNEEDED = WM_USER;
{ TInternetProtocol }
constructor TInternetProtocol.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FActive := False;
FProtocol := 'private';
FRequest := nil;
FRelease := nil;
FHandle := Forms.AllocateHWnd(WndProc);
end;
destructor TInternetProtocol.Destroy;
begin
Active := False;
Forms.DeallocateHWnd(FHandle);
inherited Destroy;
end;
procedure TInternetProtocol.Loaded;
begin
inherited Loaded;
if FActive then Activate;
end;
procedure TInternetProtocol.SetActive(const Value: Boolean);
begin
if Value = FActive then Exit;
if Value then begin
if not (csLoading in ComponentState) then Activate;
end else begin
Deactivate;
end;
FActive := Value;
end;
procedure TInternetProtocol.Activate;
begin
if csDesigning in ComponentState then Exit;
RegisterProtocol(FProtocol,Self);
end;
procedure TInternetProtocol.Deactivate;
begin
if csDesigning in ComponentState then Exit;
UnregisterProtocol(FProtocol);
end;
procedure TInternetProtocol.SetProtocol(const Value: string);
var AActive: Boolean;
begin
if not SysUtils.IsValidIdent(Value) then raise EInternetException.Create(strNotAValidProtocol);
AActive := FActive;
try
Active := False;
FProtocol := Value;
finally
Active := AActive;
end;
end;
procedure TInternetProtocol.WndProc(var Message: TMessage);
var
Msg: packed record
Msg: Longword;
Request: PChar;
Stream: ^TStream;
end;
begin
if Message.Msg = WM_STREAMNEEDED then begin
System.Move(Message,Msg,SizeOf(Msg));
if Assigned(FRequest) then FRequest(Self,string(Msg.Request),Msg.Stream^);
end else Message.Result := Windows.DefWindowProc(FHandle,Message.Msg,Message.WParam,Message.LParam);
end;
var
Session: IInternetSession; // The current Internet Session
Factory: IClassFactory; // Factory of our IInternetProtocol implementation
Lock: TRTLCriticalSection; // The lock for thread safety
List: TStrings; // The list of active protocol handlers
type
TInternetProtocolHandler = class(TInterfacedObject, IInternetProtocol)
private
ProtSink: IInternetProtocolSink; // Protocol Sink that needs the data
Stream: TStream; // Stream containing the data
StreamPosition: Integer; // Current Position in the stream
StreamSize: Integer; // Current size of the stream
LockCount: Integer; // Lock count for releasing data
procedure ReleaseStream;
public
{ IInternetProtocol }
function Start(szUrl: PWideChar; OIProtSink: IInternetProtocolSink;
OIBindInfo: IInternetBindInfo; grfPI, dwReserved: DWORD): HResult; stdcall;
function Continue(const ProtocolData: TProtocolData): HResult; stdcall;
function Abort(hrReason: HResult; dwOptions: DWORD): HResult; stdcall;
function Terminate(dwOptions: DWORD): HResult; stdcall;
function Suspend: HResult; stdcall;
function Resume: HResult; stdcall;
function Read(pv: Pointer; cb: ULONG; out cbRead: ULONG): HResult; stdcall;
function Seek(dlibMove: LARGE_INTEGER; dwOrigin: DWORD;
out libNewPosition: ULARGE_INTEGER): HResult; stdcall;
function LockRequest(dwOptions: DWORD): HResult; stdcall;
function UnlockRequest: HResult; stdcall;
end;
TInternetProtocolHandlerFactory = class(TInterfacedObject, IClassFactory)
public
{ IClassFactory }
function CreateInstance(const unkOuter: IUnknown; const iid: TIID; out obj): HResult; stdcall;
function LockServer(fLock: BOOL): HResult; stdcall;
end;
procedure RegisterProtocol(Protocol: string; Handler: TInternetProtocol);
var
i: Integer;
Proto: WideString;
begin
Windows.EnterCriticalSection(Lock);
try
// if we have a previous handler, delete that from the list.
i := List.IndexOf(Protocol);
if i >=0 then TInternetProtocol(List.Objects[i]).Active := False;
// If this is the first time, create the Factory and get the Internet Session object
if List.Count = 0 then begin
Factory := TInternetProtocolHandlerFactory.Create;
CoInternetGetSession(0, Session, 0);
end;
// Append ourselves to the list
List.AddObject(Protocol,Handler);
// Register the protocol with the Internet session
Proto := Protocol;
Session.RegisterNameSpace(Factory, IInternetProtocol{ IID_TInternetProtocolHandler}, PWideChar(Proto), 0, nil, 0);
finally
Windows.LeaveCriticalSection(Lock);
end;
end;
procedure UnregisterProtocol(Protocol: string);
var i: Integer;
Proto: WideString;
begin
Windows.EnterCriticalSection(Lock);
try
i := List.IndexOf(Protocol);
if i < 0 then Exit; // oops, protocol was somehow already freed... this should not happen
// unregister our namespace handler
Proto := Protocol; // to widestring
Session.UnregisterNameSpace(Factory, PWideChar(Proto));
// and free from list
List.Delete(i);
// see if we need to cleanup?
if List.Count = 0 then begin
// release the COM server
Session := nil;
Factory := nil;
end;
finally
Windows.LeaveCriticalSection(Lock);
end;
end;
{ TInternetProtocolHandler }
function TInternetProtocolHandler.Abort(hrReason: HResult; dwOptions: DWORD): HResult;
begin
Result := E_NOTIMPL;
end;
function TInternetProtocolHandler.Continue(const ProtocolData: TProtocolData): HResult;
begin
Result := S_OK;
end;
function TInternetProtocolHandler.LockRequest(dwOptions: DWORD): HResult;
begin
Inc(LockCount);
Result := S_OK;
end;
function TInternetProtocolHandler.Read(pv: Pointer; cb: ULONG; out cbRead: ULONG): HResult;
const Results: array [Boolean] of Longword = ( E_PENDING, S_FALSE );
begin
if Assigned(Stream) then cbRead := Stream.Read(pv^,cb) else cbRead := 0;
Inc(StreamPosition, cbread);
Result := Results[StreamPosition = StreamSize];
end;
procedure TInternetProtocolHandler.ReleaseStream;
begin
// see if we can release the Stream...
if Assigned(Stream) then FreeAndNil(Stream);
Protsink := nil;
end;
function TInternetProtocolHandler.Resume: HResult;
begin
Result := E_NOTIMPL;
end;
function TInternetProtocolHandler.Seek(dlibMove: LARGE_INTEGER;
dwOrigin: DWORD; out libNewPosition: ULARGE_INTEGER): HResult;
begin
Result := E_NOTIMPL;
end;
function TInternetProtocolHandler.Start(szUrl: PWideChar; OIProtSink: IInternetProtocolSink;
OIBindInfo: IInternetBindInfo; grfPI,dwReserved: DWORD): HResult;
var URL, Proto: string;
i: Integer;
Handler: TInternetProtocol;
begin
// Sanity check.
Assert(Assigned(OIProtSink));
Assert(Assigned(szUrl));
Assert(Assigned(OIBindInfo));
URL := szUrl;
Stream := nil; // just to make sure...
// Clip the protocol name from the URL & change the URL to the proto specific part
i := Pos(':',URL);
if i > 0 then begin
Proto := Copy(URL,1,i-1);
URL := Copy(URL,i+1,MaxInt);
end;
Windows.EnterCriticalSection(Lock);
try
i := List.IndexOf(Proto);
if i >= 0 then begin
// we've found our protocol
Handler := TInternetProtocol(List.Objects[i]);
// And query. Use a Windows message for thread synchronization
Windows.SendMessage(Handler.FHandle,WM_STREAMNEEDED,WParam(PChar(URL)),LParam(#Stream));
end;
finally
Windows.LeaveCriticalSection(Lock);
end;
if not Assigned(Stream) then begin
Result := INET_E_USE_DEFAULT_PROTOCOLHANDLER;
Exit;
end;
// Setup all data
StreamSize := Stream.Size;
Stream.Position := 0;
StreamPosition := 0;
LockCount := 1;
// Get the protocol sink & start the 'downloading' process
ProtSink := OIProtSink;
ProtSink.ReportData(BSCF_FIRSTDATANOTIFICATION or BSCF_LASTDATANOTIFICATION or
BSCF_DATAFULLYAVAILABLE, StreamSize, StreamSize);
ProtSink.ReportResult(S_OK, S_OK, nil);
Result := S_OK;
end;
function TInternetProtocolHandler.Suspend: HResult;
begin
Result := E_NOTIMPL;
end;
function TInternetProtocolHandler.Terminate(dwOptions: DWORD): HResult;
begin
Dec(LockCount);
if LockCount = 0 then ReleaseStream;
Result := S_OK;
end;
function TInternetProtocolHandler.UnlockRequest: HResult;
begin
Dec(LockCount);
if LockCount = 0 then ReleaseStream;
Result := S_OK;
end;
{ TInternetProtocolHandlerFactory }
function TInternetProtocolHandlerFactory.CreateInstance(const unkOuter: IInterface;
const iid: TIID; out obj): HResult;
begin
if IsEqualGUID(iid, IInternetProtocol) then begin
IInternetProtocol(obj) := TInternetProtocolHandler.Create as IInternetProtocol;
Result := S_OK;
end else if IsEqualGUID(iid, IInterface) then begin
IInterface(obj) := TInternetProtocolHandler.Create as IInterface;
Result := S_OK;
end else begin
Result := E_NOINTERFACE;
end;
end;
function TInternetProtocolHandlerFactory.LockServer(fLock: BOOL): HResult;
begin
if fLock then _AddRef else _Release;
Result := S_OK;
end;
initialization
begin
// Get a critical section for thread synchro
Windows.InitializeCriticalSection(Lock);
// The list of protocol handlers
List := TStringList.Create;
end;
finalization
begin
// deactivate all handlers (should only happen when memory leaks are present...)
while List.Count > 0 do TInternetProtocol(List.Objects[0]).Active := False;
List.Free;
// and delete the critical section
Windows.DeleteCriticalSection(Lock);
end;
end.