Delphi DDE Open url in active tab - delphi

I want to open url in already existing, active opera/IE/FF tab using delphi.
I tried:
ShellExecute(hw,'open',pchar(url),nil,nil,SW_SHOWNORMAL);
where hw is handle of web browser and url is string variable with url I want to open, but it opens new tab instead of using active tab.
I also tried:
procedure SetURL(Browser, URL: String);
var
Client_DDE: TDDEClientConv;
begin
Client_DDE := TDdeClientConv.Create(nil);
with Client_DDE do
begin
SetLink( Browser, 'WWW_Activate' );
RequestData('0xFFFFFFFF');
SetLink( Browser, 'WWW_OpenURL' );
RequestData(URL);
CloseLink;
end;
Client_DDE.Free;
end;
And SetURL('Opera', url); in buttonclick procedure, but it also opens url in new tab. When I use RequestData(URL + ',-1'); in SetURL procedure then it opens url in new window. Any ideas how to open url in already existing browser tab?
I have Delphi 7.

Unfortunately, that isn't possible. Take a look at similar question: Open link in same browser tab

Related

using EmbeddedWB and :OnBeforeNavigate2 to open URL in another browser

I need to open another browser sometimes if users click a link.
I am using CHtmlView::OnBeforeNavigate2 to cancel the integrated web browser from opening the specific URL; and instead I will open it in another firefox.
However this does not seem to be a good place to do this.
For some reason OnBeforeNavigate is also called, when user does not click a link at all.
The OnBeforeNavigate2 event is the best place to cancel the ongoing navigation. Just, if you want to detect if this event was triggered by a hyperlink click (since it fires e.g. for frames when they are loaded), you can determine if the Flags parameter contains the navHyperlink value, like this e.g.:
procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject;
const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
begin
if ((Flags and navHyperlink) <> 0) and (URL = 'http://example.com') then
begin
Cancel := True;
// the user clicked a link to http://example.com and the navigation is
// going to be cancelled; now do whatever else you need here
end;
end;
I cannot tell you which versions of Internet Explorer passes this value, but I can tell you for sure, that the MSDN documentation for web browser is almost completely outdated.

TWebBrowser and URL

I'm working on a software to verify the problem of porting it from D5 to XE5. In D5, TWebBrowser.BeforeNavigate2 was call each time the user click on the submit button of a displayed form. In XE5, it's not the case. I figured out it is because the URL for the submit contain http:/aDirectory/ExecToBeCall.exe. If I add an extra / after: the event is fire.
Under D5 the URL is change for:
http ://localhost/aDirectory/ExecToBeCall.exe (space added to break the link in the post)
That behavior of TWebBrowser under D5 to fire anyway and change the URL is important for the software and I cannot change the HTML (about 2000 files) to contain 2. It allowed us to know if the submit was made inside Delphi or from a outside Browser. I tried other and newer events of TWebBrowser and none are fire.
How can I be informed of a problematic URL, check it and change it into a localhost URL? A small and clean method would be preferable.
Thanks for your help and suggestions
TWebBrowser is just a thin wrapper around Internet Explorer's ActiveX object, so it is IE itself, not TWebBrowser, that is behaving differently.
http:/aDirectory/ExecToBeCall.exe is actually a valid URL. Since the : is not followed by //, there is no authority portion in the URL, and thus no explicit hostname. localhost is used as an implicit hostname, and the path is /aDirectory/ExecToBeCall.exe. That is what the URL is being changed to in D5, which is correct behavior. Changing the URL to http://aDirectory/ExecToBeCall.exe is incorrect, as that creates an authority portion of the URL and thus the hostname is explicitly set to aDirectory and the path is set to /ExecToBeCall.exe, which is not what you want.
Why the URL is not changing in XE5, I have no clue. Sounds like a bug in whatever version of IE is being used in that version of TWebBrowser.
In any case, it is IE that is triggering the event, so if it is not triggering for a URL it does not like, there is nothing you can do about that, short of using the browser's DOM interfaces to handle the onsubmit event of the HTML webform directly.
If you want to redirect unexpected url instead of navigating to it, you can start with TEmbeddedWB project or you can DIY by extending TWebBrowser class with IDocHostUIHandler, which has an interesting method TranslateURL.
function TAdvWebBrowser.TranslateURL(const dwTranslate: DWORD; const pchURLIn: POLESTR; var ppchURLOut: POLESTR): HRESULT;
var
Url: string;
BufferSize: Integer;
begin
Url := PChar(pchURLIn);
if GetSafeUrlFor(Url) then
begin
ppchURLOut := CoTaskMemAlloc(BufferSize);
CopyMemory(ppchURLOut, PChar(Url), BufferSize);
// redirects to new location
Result := S_OK;
end
else
// no redirection
Result := S_FALSE;
end;
// You can change the function to add more complex redirection rules
function GetSafeUrlFor(var Url: string): Boolean;
begin
Result := Url.EndsWithText('.exe');
if Result then
Url := 'http://localhost/';
end;

twebbrowser download in popupwindow

Looking to get my delphi app to log into a website, navigate to a page, and automatically download certain files, the solution at How do I keep an embedded browser from prompting where to save a downloaded file?, helped a great deal with the file download.
The final problem is the last step on navigating opens in a popup window, there are plenty of solutions out there to capture popup windows by implementing TWebBrowser.NewWindow2 but none of these events seem to work with the above code, something to do with how twebbrowser.invokeevent in the above code works maybe?
If I use invokeveent and the dispID of 273(newwindow3) to call a function I can twebbwowser.navigate() a second webbrowser to the url of the popupwindow.
My problem is the popup window has basicly one line of javascript "document.print(parent.parent.opener.thefunction())" the second twebbrowser has no reference to its parent so this fails.
I can see two possible solutions, get the TWebBrowser.NewWindow2 or 3 to trigger, fix the code sample bellow, LVarArray[0] {const IDispatch}, is null for some reason.
procedure TWebBrowser.InvokeEvent(ADispID: TDispID; var AParams: TDispParams);
// DispID 250 is the BeforeNavigate2 dispinterface and to the FFileSource here
// is stored the URL parameter (for cases, when the IDownloadManager::Download
// won't redirect the URL and pass empty string to the pszRedir)
//showmessage('test');
var
ArgCount : Integer;
LVarArray : Array of OleVariant;
LIndex : Integer;
begin
inherited;
ArgCount := AParams.cArgs;
SetLength(LVarArray, ArgCount);
for LIndex := Low(LVarArray) to High(LVarArray) do
LVarArray[High(LVarArray)-LIndex] := OleVariant(TDispParams(AParams).rgvarg^[LIndex]);
case ADispID of
250: FFileSource := OleVariant(AParams.rgvarg^[5]);
273: DoNewWindow3(Self,
LVarArray[0] {const IDispatch},
WordBool((TVarData(LVarArray[1]).VPointer)^) {var WordBool},
LVarArray[2] {const OleVariant},
LVarArray[3] {const OleVariant},
LVarArray[4] {const OleVariant});
end;
end;
I'm not going to answer your question directly because I think you've asked the wrong question. You are trying to download files over the internet without any GUI being shown to the user. As such, an embedded browser is simply the wrong solution.
Rather than trying to suppress popup dialogs, use a tool that never shows popup dialogs. What I believe you should be doing is downloading the files using direct HTTP download. There are many different ways to achieve that. For example, an extremely convenient method, available out of the box with Delphi, is to use Indy. I believe that the component you need is TIdHttp.

Open new pages also in TWebBrowser

I have created an app with a twebBrowser in it. Problem is when i click on some link, in say gmail, it opens in a new window of my default browser( which is IE). how do i make it work like firefox or chrome etc. which opens the clicked links in their windows. The url's should open in the TWebBrowser's window. Must i create a new Form at runtime with TWebBrowser in it at runtime for that? Code not needed as such, ideas will do
Thanks in Advance.
P.S. My org blocks Gmail, Facebook etc. , However through my TWebBrowser, i can open them. Can my QA ppl see that in their log? My guess will be no, since then they would block it. What is your comment on this
TWebBrowser has an OnNewWindow2 event. Assuming the form holding the TWebBrowser is named Form1 and the web-control itself is named WebBrowser1, write a handler like this:
procedure TForm1.WebBrowser1NewWindow2(ASender: TObject; var ppDisp: IDispatch; var Cancel: WordBool);
var NF: TForm1;
begin
NF := TForm1.Create(Application);
NF.Visible := True;
NF.WebBrowser1.RegisterAsBrowser;
ppDisp := NF.WebBrowser1.DefaultInterface;
end;
This will create a new window, with a new TWebBrowser when the "click" is supposed to lead to a new window.

How do I open Word modally from my Delphi app?

I have a Word document on disk. I want my application to open Word modally, with this file loaded (and resume running when Word is closed)
Option A:
You can open Word using TOleContainer on the form and you show the Form as Modal
Option B:
something like:
EnableWindow(Application.MainForm.Handle, True);
application.Minimize;
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
Application.Restore;
EnableWindow(Application.MainForm.Handle, False);
Application.BringToFront;
c this for more information.

Resources