Get web page in Delphi [duplicate] - delphi

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Downloading a file in Delphi
Delphi File Downloader Component
Hello everyone,
I'm trying to develop a Delphi 7 app which would serve as a currency database or something close to it (don't ask me why, I'm doing it just for money:) ). So current task is to download a web page containing currency list from a server and extract this list to the database.
Is it possible and what tools should one need to accimplish it? I know php, but writing php gui seems something crazy)

To add to Andreas comments, you can also check out Delphi.About.com, there is an example there to download a file and show a progressbar: http://delphi.about.com/cs/adptips2003/a/bltip0903_2.htm
"If you need to save the contents of a specified URL to a file - and be able to track the download progress, use the TDownLoadURL Delphi action
While TDownLoadURL is writing to the specified file, it periodically generates an OnDownloadProgress event, so that you can provide users with feedback about the process.
Here's an example - note that you must include the unit ExtActns in the uses clause."
uses ExtActns, ...
type
TfrMain = class(TForm)
...
private
procedure URL_OnDownloadProgress
(Sender: TDownLoadURL;
Progress, ProgressMax: Cardinal;
StatusCode: TURLDownloadStatus;
StatusText: String; var Cancel: Boolean) ;
...
implementation
...
procedure TfrMain.URL_OnDownloadProgress;
begin
ProgressBar1.Max:= ProgressMax;
ProgressBar1.Position:= Progress;
end;
function DoDownload;
begin
with TDownloadURL.Create(self) do
try
URL:='http://0.tqn.com/6/g/delphi/b/index.xml';
FileName := 'c:\ADPHealines.xml';
OnDownloadProgress := URL_OnDownloadProgress;
ExecuteTarget(nil) ;
finally
Free;
end;
end;

Related

How to close PDF document that is used in Acrobat Reader ActiveX component from Delphi?

I have importated Acrobate Reader ActiveX component in my Delphi application and loaded PDF document with code:
var AA: TAcroPDF;
function GetFilePath(AFileName: String): String;
begin
Result:=IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+AFileName;
end;
procedure TMainForm.Open2BtnClick(Sender: TObject);
var FPath: String;
begin
FPath:=GetFilePath('test.pdf');
if not FileExists(FPath) then raise Exception.Create('No file');
AA.LoadFile(FPath);
end;
But I can not unload PDF file from Acrobat ActiveX component. My use case requires that component unloads the file and shows gray rectangle upon the request from user. I have tried several code lines:
AA.Src:='';
AA.LoadFile('');
But file stays loaded all the time. I can successfully call the load of another file, but I can not find the way to unload the file.
I am using Delphi 2009, but I guess that my question applies to any version.
Question update and suggestion of hack as a solution:
I managed to adapt the code from https://stackoverflow.com/a/37889375/1375882 as for the close:
procedure TMainForm.Close1BtnClick(Sender: TObject);
var
Ref : Integer;
begin
if Assigned(AA) then begin
Ref:=AA.ControlInterface._AddRef;
AA.Src:='';
AA.Free;
AA:=Nil;
end;
end;
And accordingly I am checking if Assigned(AA) in FormClose as well. This works perfectly for me as far as I can see. But this seems to be no clean solution that was intended by Acrobat Reader?
Bad consequences of the suggested hack/solution:
No dark-gray rectangle remains on the form after onloading Acrobat Reader component in the suggested way and the subsequent call of anothes LoadFile(...) fails with AV, of course.

Fault in Ole Automation Server

I set up an OLE Automation Server and an OLE Client in Delphi XE2. My two Methods are the following:
function TMyCom2.Get_Text() : IStrings;
begin
GetOleStrings(unit1.Form1.Memo1.Lines, Result);
end;
and:
procedure TMyCom2.Set_Text(const value: IStrings);
begin
SetOleStrings(unit1.Form1.Memo1.Lines, value);
end;
Now I tried to call both by the client.
The second Method(Set_Text) was working perfectly fine.
But the first one(Get_Text) wich should gather the content of the memo of the server and write it into the memo of the client, caused this exception:
Exception error of the server!
To get the Ole information I wrote this on the client side:
procedure TForm1.Button1Click(Sender: TObject);
var
aStrings : IStrings;
begin
aStrings.Add(Server.Get_Text);
SetOleStrings(Memo1.Lines, aStrings);
end;
I have no idea what could be wrong and I was incredibly grateful if somebody could take a look at this code and tell me what the hell is going wrong;D
PS: I already tested it with Integers and it worked so the Problem has to do something with Strings
I uploaded the two projects on Dropbox so feel free to download them:
Client: here
Server: here

Directory path manipulation in Delphi?

I have the full path name of a given folder for e.g.
c:\foo\bar
Now I would like to reference a file inside c:\foo named baz.txt,
c:\foo\bar\..\baz.txt
I am currently using the .. path operator to go down one level and get the file that I need.
Is there a function that can do path manipulations, for e.g. UpOneLevel(str) -> str ? I know I can write one by splitting the string and removing the last token, but I would rather it be a built-in / library function so I don't get into trouble later if there are for e.g. escaped backslashes.
Use the ExpandFileName function:
var
S: string;
begin
S := 'c:\foo\bar\..';
S := ExpandFileName(S);
ShowMessage(S);
end;
The message from the above example will show the c:\foo path.
Look at ExtractFilePath() and ExtractFileDir(). These are available in just about all Delphi versions, particularly those that do not have TDirectory, IOUtils, etc.
And before anyone says it, these work just fine whether the path ends with a filename or not. ForceDirectories() uses them internally to walk backwards through a hierarchy of parent folders, for example.
This answer is valid for Delphi XE +
Use the TDirectory class of the IOutils unit, which have the method GetParent, like this::
uses IOUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
s := 'c:\foo\bar';
ShowMessage(TDirectory.GetParent(s));
end;
In older versions
Look at the other answers.
You can take a look at TPathBuilder record in SvClasses unit from delphi-oop library. This unit does not support Delphi 2007 but TPathBuilder implementation is compatible with this Delphi version. Example usage:
var
LFullPath: string;
begin
LFullPath := TPathBuilder.InitCustomPath('c:\foo\bar').GoUpFolder.AddFile('baz.txt').ToString;
//LFullPath = c:\foo\baz.txt

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.

Delphi IDE custom menu items, how to Add them?

I am working on a project using Delphi 7 and Delphi 2006, i am developing a component that will get certain system information.
Now the requirement is that after the component is installed on the system, there should be a menu item on the IDE , like this
and for delphi 7 like this
i have searched on the net about adding a menu item but i have not got anything to add an item to the IDE like the one EurekaLog has.
can any body tell me how to add the item like EurekaLog or mysql ?
is it some where in the registry?
To add a menu to the Delphi IDE you must use the Delphi Open Tools API. from here you can access the Main menu of the delphi IDE using a code like this.
LMainMenu:=(BorlandIDEServices as INTAServices).MainMenu;
or
LMainMenu:=(BorlandIDEServices as INTAServices).GetMainMenu;
And then add the menu items which you want.
Check these links for additional samples
Open Tools API Chapter 15: IDE Main Menus
Introduction to the Delphi Open Tools API
How can I add a menu item to the IDE’s main menu?
If you want to specifically add a menu item to the HELP menu, and also make it get removed when your package unloads, and handle enable/disable of the items, then this wizard code might be helpful. I took the sample wizard code that GExperts documentation shows as a starter project, and posted it up here as a slightly nicer start project. You can get started very quickly if you grab this code and just extend it:
https://bitbucket.org/wpostma/helloworldwizard/
What they mean by "Wizard" is a "simple IDE Expert", that is, something with a menu added to the IDE, that implements IOTAWizard and IOTAMenuWizard. This approach has many benefits and is the way that the GExperts wizards are written.
The core of the code is this starter wizard, which needs to be put into a package (DPK) and installed, and registered with the IDE:
// "Hello World!" for the OpenTools API (IDE versions 4 or greater)
// By Erik Berry: http://www.gexperts.org/, eberry#gexperts.org
unit HelloWizardUnit;
interface
uses ToolsAPI;
type
// TNotifierObject has stub implementations for the necessary but
// unused IOTANotifer methods
THelloWizard = class(TNotifierObject, IOTAMenuWizard, IOTAWizard)
public
// IOTAWizard interface methods(required for all wizards/experts)
function GetIDString: string;
function GetName: string;
function GetState: TWizardState;
procedure Execute;
// IOTAMenuWizard (creates a simple menu item on the help menu)
function GetMenuText: string;
end;
implementation
uses Dialogs;
procedure THelloWizard.Execute;
begin
ShowMessage('Hello World!');
end;
function THelloWizard.GetIDString: string;
begin
Result := 'EB.HelloWizard';
end;
function THelloWizard.GetMenuText: string;
begin
Result := '&Hello Wizard';
end;
function THelloWizard.GetName: string;
begin
Result := 'Hello Wizard';
end;
function THelloWizard.GetState: TWizardState;
begin
Result := [wsEnabled];
end;
end.
The registration code is not shown above, but is included in its own "Reg" (registration) unit if you download this from the link above. A tutorial link is on EDN here.

Resources