Delphi 2007: save only the breakpoint options in the DSK file? - delphi

Is it possible in Delphi to just save the breakpointss in the .DSK file for a project and no other Desktop settings?
Most of the .DSK gets in the way, but not being able to save debug breakpoints is a real pain (especially when they are conditionally or actions are attached).

I've never come across an IDE facility to save only the breakpoint-related settings in the .Dsk file.
For amusement, I thought I'd try and implement something via an IDE add-in using OTA notifications. The code below runs fine installed into a package installed in D7, and the IDE seems quite happy to re-open a project whose .Dsk file has been processed by it (and the breakpoints get set!).
As you can see, it catches an OTA notifier's FileNotification event when called with a NotifyCode of ofnProjectDesktopSave, which happens just after the IDE has saved the .Dsk file (initially with the extension '.$$$', which I faile to notice when first writing this). It then reads the saved file file, and and prepares an updated version from which all except a specified list of sections are removed. The user then has the option to save the thinned-out file back to disk. I've used a TMemIniFile to do most of the processing simply to minimize the amount of code needed.
I had zero experience of writing an OTA notifier when I read your q, but the GE Experts FAQ referenced below was immensely helpful, esp the example notifier code.
Normally, deleting a project's .Dsk file is harmless, but use this code with caution as it has not been stress-tested.
Update: I noticed that the filename received by TIdeNotifier.FileNotification event actually has an extension of '.$$$'. I'm not quite sure why that should be, but seemingly the event is called before the file is renamed to xxx.Dsk. I thought that would require a change to how
to save the thinned-out version, but evidently not.
Update#2: Having used a folder-monitoring utility to see what actually happens, it turns out that the desktop-save notification the code receives is only the first of a number of operations related to the .Dsk file. These include renaming any existing version of the .Dsk file as a .~Dsk file and finally saving the .$$$ file as the new .Dsk file.
unit DskFilesu;
interface
{$define ForDPK} // undefine to test in regular app
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, StdCtrls, IniFiles, TypInfo
{$ifdef ForDPK}
, ToolsApi
{$endif}
;
{$ifdef ForDPK}
{
Code for OTA TIdeNotifier adapted from, and courtesy of, the link on http://www.gexperts.org/open-tools-api-faq/#idenotifier
}
type
TIdeNotifier = class(TNotifierObject, IOTANotifier, IOTAIDENotifier)
protected
procedure AfterCompile(Succeeded: Boolean);
procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
procedure FileNotification(NotifyCode: TOTAFileNotification;
const FileName: string; var Cancel: Boolean);
end;
{$endif}
type
TDskForm = class(TForm)
edDskFileName: TEdit;
SpeedButton1: TSpeedButton;
OpenDialog1: TOpenDialog;
lbSectionsToKeep: TListBox;
lbDskSections: TListBox;
moDskFile: TMemo;
btnSave: TButton;
procedure btnSaveClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
private
procedure GetSectionsToKeep;
function GetDskFileName: String;
procedure SetDskFileName(const Value: String);
function GetDskFile: Boolean;
protected
public
DskIni : TMemIniFile;
property DskFileName : String read GetDskFileName write SetDskFileName;
end;
var
NotifierIndex: Integer;
DskForm: TDskForm;
{$ifdef ForDPK}
procedure Register;
{$endif}
implementation
{$R *.DFM}
{$ifdef ForDPK}
procedure Register;
var
Services: IOTAServices;
begin
Services := BorlandIDEServices as IOTAServices;
Assert(Assigned(Services), 'IOTAServices not available');
NotifierIndex := Services.AddNotifier(TIdeNotifier.Create);
end;
{$endif}
procedure DskPopUp(FileName : String);
var
F : TDskForm;
begin
F := TDskForm.Create(Application);
try
F.DskFileName := FileName;
F.ShowModal;
finally
F.Free;
end;
end;
function TDskForm.GetDskFileName: String;
begin
Result := edDskFileName.Text;
end;
procedure TDskForm.SetDskFileName(const Value: String);
begin
edDskFileName.Text := Value;
if Assigned(DskIni) then
FreeAndNil(DskIni);
btnSave.Enabled := False;
DskIni := TMemIniFile.Create(DskFileName);
DskIni.ReadSections(lbDskSections.Items);
GetSectionsToKeep;
end;
procedure TDskForm.btnSaveClick(Sender: TObject);
begin
DskIni.UpdateFile;
end;
procedure TDskForm.FormCreate(Sender: TObject);
begin
lbSectionsToKeep.Items.Add('watches');
lbSectionsToKeep.Items.Add('breakpoints');
lbSectionsToKeep.Items.Add('addressbreakpoints');
if not IsLibrary then
DskFileName := ChangeFileExt(Application.ExeName, '.Dsk');
end;
procedure TDskForm.GetSectionsToKeep;
var
i,
Index : Integer;
SectionName : String;
begin
moDskFile.Lines.Clear;
for i := lbDskSections.Items.Count - 1 downto 0 do begin
SectionName := lbDskSections.Items[i];
Index := lbSectionsToKeep.Items.IndexOf(SectionName);
if Index < 0 then
DskIni.EraseSection(SectionName);
end;
DskIni.GetStrings(moDskFile.Lines);
btnSave.Enabled := True;
end;
function TDskForm.GetDskFile: Boolean;
begin
OpenDialog1.FileName := DskFileName;
Result := OpenDialog1.Execute;
if Result then
DskFileName := OpenDialog1.FileName;
end;
procedure TDskForm.SpeedButton1Click(Sender: TObject);
begin
GetDskFile;
end;
{$ifdef ForDPK}
procedure RemoveNotifier;
var
Services: IOTAServices;
begin
if NotifierIndex <> -1 then
begin
Services := BorlandIDEServices as IOTAServices;
Assert(Assigned(Services), 'IOTAServices not available');
Services.RemoveNotifier(NotifierIndex);
end;
end;
function MsgServices: IOTAMessageServices;
begin
Result := (BorlandIDEServices as IOTAMessageServices);
Assert(Result <> nil, 'IOTAMessageServices not available');
end;
procedure TIdeNotifier.AfterCompile(Succeeded: Boolean);
begin
end;
procedure TIdeNotifier.BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
begin
Cancel := False;
end;
procedure TIdeNotifier.FileNotification(NotifyCode: TOTAFileNotification;
const FileName: string; var Cancel: Boolean);
begin
Cancel := False;
// Note: The FileName passed below has an extension of '.$$$'
if NotifyCode = ofnProjectDesktopSave then
DskPopup(FileName);
end;
initialization
finalization
RemoveNotifier;
{$endif}
end.

Related

How to create a default project under Delphi XE7?

Related to this question: Should "Library path" point to the source files of packages?
Fabricio Araujo suggested that is not necessary to set the 'search path' for each new project by creating a 'Default Project Option'. How can this be done in Delphi XE7?
Prompted by your q, and more for amusement than anything else, I decided to try
writing an IDE-plugin that would provide a way to store some preferred project
settings somewhere and allow you to apply them to the current project.
To use, prepare and save a sample .Ini file containing your preferred settings in the format shown below (it's important to get the project option names right, see below for how to do find them out), then compile the unit below into a new package and install it in the IDE. Its gui will pop up when you subsequently open a project.
The settings in the .Ini are loaded into a ValueList editor and pressing the
[Return] key in one of the values will apply it to the project.
Interestingly, the names the IDE uses for the Project seetings are the same in XE7
as they are in D7. Iow, the XE7 IDE uses these internally rather than the names
which appear in the .DProj XML file. You can get a full list of them by clicking the GetOptions button.
As usual when working with the IDE OTA services, the code has to include
a fair amount of "baggage".
Tested in D7 and XE7.
Sample Ini File:
[settings]
OutputDir=Somewhere
UnitOutputDir=Somewhere else
UnitDir=$(DELPHI)
ObjDir=$(DELPHI)
SrcDir=$(DELPHI)
ResDir=$(DELPHI)
Code:
unit ProjectOptionsXE7u;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ToolsAPI, Grids, ValEdit, IniFiles;
type
TProjectOptionsForm = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
ValueListEditor1: TValueListEditor;
btnGetOptions: TButton;
procedure btnGetOptionsClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ValueListEditor1KeyPress(Sender: TObject; var Key: Char);
private
function GetCurrentProject: IOTAProject;
procedure GetOptionsFromIni;
procedure UpdateOptionValue;
public
Services: IOTAServices;
ProjectGroup : IOTAProjectGroup;
Project: IOTAProject;
Options : IOTAProjectOptions;
ModServices: IOTAModuleServices;
Module: IOTAModule;
NotifierIndex: Integer;
Ini : TMemIniFile;
IsSetUp : Boolean;
SetUpCount : Integer;
procedure GetProjectOptions;
procedure SetUp;
end;
var
ProjectOptionsForm: TProjectOptionsForm;
procedure Register;
implementation
{$R *.dfm}
type
TIdeNotifier = class(TNotifierObject, IOTANotifier, IOTAIDENotifier)
protected
procedure AfterCompile(Succeeded: Boolean);
procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
procedure FileNotification(NotifyCode: TOTAFileNotification;
const FileName: string; var Cancel: Boolean);
end;
procedure Register;
begin
ProjectOptionsForm:= TProjectOptionsForm.Create(Nil);
ProjectOptionsForm.Services := BorlandIDEServices as IOTAServices;
ProjectOptionsForm.NotifierIndex := ProjectOptionsForm.Services.AddNotifier(TIdeNotifier.Create);
end;
procedure CloseDown;
begin
ProjectOptionsForm.Services.RemoveNotifier(ProjectOptionsForm.NotifierIndex);
ProjectOptionsForm.Close;
ProjectOptionsForm.Free;
end;
procedure TIdeNotifier.AfterCompile(Succeeded: Boolean);
begin
end;
procedure TIdeNotifier.BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
begin
end;
procedure TIdeNotifier.FileNotification(NotifyCode: TOTAFileNotification;
const FileName: string; var Cancel: Boolean);
begin
if NotifyCode in [ofnProjectDesktopLoad, ofnActiveProjectChanged] then begin
ProjectOptionsForm.Show;
// ProjectOptionsForm.Memo1.Lines.Add('Got notification');
ProjectOptionsForm.SetUp;
// ProjectOptionsForm.Memo1.Lines.Add('after GetProjectOptions');
end;
end;
procedure TProjectOptionsForm.btnGetOptionsClick(Sender: TObject);
var
KeyName,
Value,
S : String;
V : Variant;
i : Integer;
begin
GetProjectOptions;
ValueListEditor1.Strings.Clear;
for i := Low(Options.GetOptionNames) to High(Options.GetOptionNames) do begin
try
KeyName := Options.GetOptionNames[i].Name;
if CompareText(KeyName, 'HeapSize') = 0 then
NoOp;
V := Options.Values[KeyName];
if not VarIsEmpty(V) then
Value := VarToStr(V)
else
Value := '';
ValueListEditor1.InsertRow(KeyName, Value, True);
except
// Reading some CPP-related settings cause exceptions
S := '***Error ' + KeyName; // + ': ' + IntToStr(Options.Values[KeyName].Kind);
Memo1.Lines.Add(S);
end;
end;
end;
procedure TProjectOptionsForm.FormDestroy(Sender: TObject);
begin
Ini.Free;
end;
procedure TProjectOptionsForm.GetOptionsFromIni;
var
i : Integer;
KeyName : String;
TL : TStringList;
begin
ValueListEditor1.Strings.Clear;
TL := TStringList.Create;
try
Ini.ReadSection('Settings', TL);
Assert(TL.Count > 0);
for i := 0 to TL.Count - 1 do begin
KeyName := TL[i];
ValueListEditor1.InsertRow(KeyName, Ini.ReadString('Settings', KeyName, ''), True);
end;
finally
TL.Free;
end;
end;
procedure TProjectOptionsForm.FormCreate(Sender: TObject);
var
IniFileName : String;
begin
IniFileName := 'd:\aaad7\ota\ProjectOptions.Ini'; // <beware of hard-code path
Ini := TMemIniFile.Create(IniFileName);
GetOptionsFromIni;
end;
function TProjectOptionsForm.GetCurrentProject: IOTAProject;
var
i: Integer;
begin
Result := nil;
ModServices := BorlandIDEServices as IOTAModuleServices;
for i := 0 to ModServices.ModuleCount - 1 do
begin
Module := ModServices.Modules[i];
if Supports(Module, IOTAProjectGroup, ProjectGroup) then begin
Result := ProjectGroup.ActiveProject;
Options := Result.ProjectOptions;
Exit;
end
else if Supports(Module, IOTAProject, Project) then
begin // In the case of unbound packages, return the 1st
if Result = nil then begin
Result := Project;
Options := Result.ProjectOptions;
end;
end;
end;
end;
procedure TProjectOptionsForm.GetProjectOptions;
begin
Assert(Project <> Nil, 'Project');
Options := Project.ProjectOptions;
end;
procedure TProjectOptionsForm.SetUp;
begin
Project := GetCurrentProject;
GetProjectOptions;
Inc(SetUpCount);
Caption := 'Setup done ' + IntToStr(SetUpCount);
IsSetUp := True;
end;
procedure TProjectOptionsForm.UpdateOptionValue;
var
Rect : TGridRect;
S : String;
KeyName,
Value : String;
Row,
Col : Integer;
begin
if Options = Nil then
Exit;
Rect := ValueListEditor1.Selection;
// S := 'left: %d top: %d right: %d, bottom: %d';
// S := Format(S, [Rect.Left, Rect.Top, Rect.Right, Rect.Bottom]);
// Memo1.Lines.Add(S);
Row := Rect.Bottom;
Col := Rect.Left - 1;
KeyName := ValueListEditor1.Cells[Col, Row];
Value := ValueListEditor1.Values[KeyName];
Options.SetOptionValue(KeyName, Value);
Options.ModifiedState := True;
Module.Save(False, False);
end;
procedure TProjectOptionsForm.ValueListEditor1KeyPress(Sender: TObject; var
Key: Char);
begin
if Key = #13 then
UpdateOptionValue;
end;
initialization
finalization
CloseDown;
end.
How this can be done in Delphi XE7?
It cannot. This functionality was removed I'm not sure exactly when, but it has not been present for some considerable time.
What you can do is:
Create a new project.
Change its settings however you please.
Save this modified project template to some central location.
Whenever you make a new project, do so by copying this project template.
You can integrate this process into the IDE by saving your modified project template into the Object Repository. Add a project to the repository with Project | Add to Repository.

Drag and Drop files to Delphi form not working

I've tried to accept files that are dragged and dropped to a Form from the File Explorer but it doesn't work. My WM_DROPFILES handler is never called. I'm running Windows 8 if that makes any difference.
Here's a simple example of what I do (I just have a TMemo on the form):
type
TForm1 = class(TForm)
Memo1: TMemo;
private
{ Private declarations }
procedure WMDROPFILES(var msg : TWMDropFiles) ; message WM_DROPFILES;
procedure CreateWnd; override;
procedure DestroyWnd; override;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.CreateWnd;
begin
inherited;
DragAcceptFiles(Handle, True);
end;
procedure TForm1.DestroyWnd;
begin
inherited;
DragAcceptFiles(Handle, false);
end;
procedure TForm1.WMDROPFILES(var msg: TWMDropFiles);
var
i, fileCount: integer;
fileName: array[0..MAX_PATH] of char;
begin
fileCount:=DragQueryFile(msg.Drop, $FFFFFFFF, fileName, MAX_PATH);
for i := 0 to fileCount - 1 do
begin
DragQueryFile(msg.Drop, i, fileName, MAX_PATH);
Memo1.Lines.Add(fileName);
end;
DragFinish(msg.Drop);
end;
Most likely you are running your application elevated. Probably because you are running Delphi elevated. In Vista and later, low privilege processes cannot send messages to higher privilege processes. This MSDN blog explains more.
If you are running your Delphi IDE elevated, I urge you to stop doing so. There's very seldom a need to do so for standard desktop application development.
As Remy points out, your DestroyWnd is incorrect. You are destroying the window handle before calling DragAcceptFiles. Simply reverse the order. Personally I'd use WindowHandle in both CreateWnd and DestroyWnd. The Handle property creates the window handle if it is not assigned and so masks such errors.
procedure TForm1.CreateWnd;
begin
inherited;
DragAcceptFiles(WindowHandle, True);
end;
procedure TForm1.DestroyWnd;
begin
DragAcceptFiles(WindowHandle, false);
inherited;
end;

Delphi IDE Menu integration - INTAServices

I successfully installed my menu item inside Delphi using INTAServices40 but the problem is - menu is missing the next time Delphi starts?! Actually, two menu items are installed; One under Help menu which is ALWAYS shown (IOTAWizardMenu), but the one under Tools menu (TEST menu item) is missing the next time Delphi starts. How to fix this?
unit TESTMENU;
interface
uses
ToolsAPI, Classes, Windows, vcl.Menus, vcl.dialogs;
type
TCustomMenuItem = class(TNotifierObject, IOTAWizard, IOTAMenuWizard)
function GetIDString: string;
function GetName: string;
function GetState: TWizardState;
// Launch the AddIn
procedure Execute;
function GetMenuText: string;
end;
TCustomMenuHandler = class(TObject)
// Handle custom menu
procedure HandleClick(Sender: TObject);
end;
procedure Register;
implementation
var
mnuitem: TMenuItem;
CustomMenuHandler: TCustomMenuHandler;
procedure TCustomMenuItem.Execute;
begin
ShowMessage('IOTAWizardMenu based menu item');
end;
function TCustomMenuItem.GetIDString: string;
begin
Result := 'TMS.MenuSample';
end;
function TCustomMenuItem.GetMenuText: string;
begin
Result := 'IOTAWizardMenu';
end;
function TCustomMenuItem.GetName: string;
begin
Result := 'TMSMenuSample';
end;
function TCustomMenuItem.GetState: TWizardState;
begin
Result := [wsEnabled];
end;
procedure TCustomMenuHandler.HandleClick(Sender: TObject);
begin
ShowMessage('INTAServices40.MainMenu based menu item');
end;
procedure AddIDEMenu;
var
NTAServices: INTAServices40;
begin
NTAServices := BorlandIDEServices as INTAServices40;
// avoid inserting twice
if NTAServices.MainMenu.Items[9].Find('TEST') = nil then
begin
CustomMenuHandler := TCustomMenuHandler.Create;
mnuitem := TMenuItem.Create(nil);
mnuitem.Caption := 'TEST';
mnuitem.OnClick := CustomMenuHandler.HandleClick;
NTAServices.MainMenu.Items[9].Add(mnuitem)
end;
end;
procedure RemoveIDEMenu;
var
NTAServices: INTAServices40;
begin
if Assigned(mnuitem) then
begin
NTAServices := BorlandIDEServices as INTAServices40;
NTAServices.MainMenu.Items[9].Remove(mnuitem);
mnuitem.Free;
if Assigned(CustomMenuHandler) then
CustomMenuHandler.Free;
end;
end;
procedure Register;
begin
AddIDEMenu;
RegisterPackageWizard(TCustomMenuItem.Create);
end;
initialization
mnuitem := nil;
CustomMenuHandler := nil;
finalization
RemoveIDEMenu;
end.
So, my first problem is how to get menu item TEST shown each time Delphi starts.. Also, I would like to add icon next to the menu item TEST. Any directions?
Thank you
EDIT:
I just found out my package is delayed loading. Reading the Internet people say ForceDemandLoadState(dlDisable) should be called. But, this is not helping me also....
NTAServices.MainMenu.Items[9] may return different things at different times as the IDE is loading its packages, also there are menu items whose sub-items are managed by the IDE at runtime (e.g. the Window menu).
You could look up the Help menu item component by name:
procedure AddIDEMenu;
var
HelpMenu: TComponent;
begin
HelpMenu := Application.MainForm.FindComponent('HelpMenu');
if (HelpMenu is TMenuItem) and (TMenuItem(HelpMenu).Find('TEST') = nil) then
begin
CustomMenuHandler := TCustomMenuHandler.Create;
mnuitem := TMenuItem.Create(nil);
mnuitem.Caption := 'TEST';
mnuitem.OnClick := CustomMenuHandler.HandleClick;
TMenuItem(HelpMenu).Add(mnuitem);
end;
end;
procedure RemoveIDEMenu;
var
HelpMenu: TComponent;
begin
if Assigned(mnuitem) then
begin
HelpMenu := Application.MainForm.FindComponent('HelpMenu');
if HelpMenu is TMenuItem then
TMenuItem(HelpMenu).Remove(mnuitem);
mnuitem.Free;
CustomMenuHandler.Free;
end;
end;

How enumerate the forms of the IDE from a delphi IDE expert

I am working in an delphi IDE expert , and I need enumerate all the forms displayed by the Delphi IDE, currently i am using the Screen.Forms property , but i am wondering if exist another way to do this using the OTA. because using the Screen.Forms only works when my expert is a BPL but now i am migrating to a dll expert.
Screen.Forms should still work from a DLL. Just make sure you compile your DLL with the "use runtime packages" linker option selected. That way, your DLL will use the same VCL instance as the IDE, and you'll have access to all the same global variables, including Screen.
This is perfectly possible with the OpenToolsAPI.
To extract a list of all opened forms in the IDE, you could use something like this:
procedure GetOpenForms(List: TStrings);
var
Services: IOTAModuleServices;
I: Integer;
Module: IOTAModule;
J: Integer;
Editor: IOTAEditor;
FormEditor: IOTAFormEditor;
begin
if (BorlandIDEServices <> nil) and (List <> nil) then
begin
Services := BorlandIDEServices as IOTAModuleServices;
for I := 0 to Services.ModuleCount - 1 do
begin
Module := Services.Modules[I];
for J := 0 to Module.ModuleFileCount - 1 do
begin
Editor := Module.ModuleFileEditors[J];
if Assigned(Editor) then
if Supports(Editor, IOTAFormEditor, FormEditor) then
List.AddObject(FormEditor.FileName,
(Pointer(FormEditor.GetRootComponent)));
end;
end;
end;
end;
Note that the pointer in that StringList is an IOTAComponent. To resolve this to the TForm instance you must dig deeper. To be continued.
It's also possible to keep track of all forms being opened in the IDE by adding a notifier of type IOTAIDENotifier to the IOTAServices, as follows:
type
TFormNotifier = class(TNotifierObject, IOTAIDENotifier)
public
procedure AfterCompile(Succeeded: Boolean);
procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
procedure FileNotification(NotifyCode: TOTAFileNotification;
const FileName: String; var Cancel: Boolean);
end;
procedure Register;
implementation
var
IdeNotifierIndex: Integer = -1;
procedure Register;
var
Services: IOTAServices;
begin
if BorlandIDEServices <> nil then
begin
Services := BorlandIDEServices as IOTAServices;
IdeNotifierIndex := Services.AddNotifier(TFormNotifier.Create);
end;
end;
procedure RemoveIdeNotifier;
var
Services: IOTAServices;
begin
if IdeNotifierIndex <> -1 then
begin
Services := BorlandIDEServices as IOTAServices;
Services.RemoveNotifier(IdeNotifierIndex);
end;
end;
{ TFormNotifier }
procedure TFormNotifier.AfterCompile(Succeeded: Boolean);
begin
// Do nothing
end;
procedure TFormNotifier.BeforeCompile(const Project: IOTAProject;
var Cancel: Boolean);
begin
// Do nothing
end;
procedure TFormNotifier.FileNotification(NotifyCode: TOTAFileNotification;
const FileName: String; var Cancel: Boolean);
begin
if BorlandIDEServices <> nil then
if (NotifyCode = ofnFileOpening) then
begin
//...
end;
end;
initialization
finalization
RemoveIdeNotifier;
end.

How to get the url from Chrome using delphi

How can I get the url from a running instance of Chrome using Delphi?
I'm trying to do a Delphi application that gets the url of the active tab of the browser (IE, Mozilla, etc.). I'm using this code that works for IE:
procedure TForm1.GetCurrentURL (var URL, Title : string);
var
DDEClient : TDDEClientConv;
s : string;
begin
s := '';
try
DDEClient := TDDEClientConv.Create(self);
with DDEClient do
begin
if SetLink('IExplore','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Netscape','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Mosaic','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Netscp6','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Mozilla','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Firefox','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle');
end;
if s <> '' then
begin
delete(s,1,1);
URL := copy(s,1,pos('","',s)-1);
delete(s,1,pos('","',s)+2);
Title := copy(s,1,pos('"',s) - 1);
end;
exit;
except
MessageDlg('URL attempt failed!',mtError,[mbOK],0);
end;
end;
But this code doesn't work with Chrome.
Thanks.
Here is how I have done it before for retrieving the URL from the active tab. You could probably extend this to include all of Chrome's tabs.
One other note, as you can see this grabs the first handle to chrome.exe that it finds. To have this accommodate multiple instances of Chrome running, you would need to adjust this to get a handle to each Chrome instance.
I put this together pretty quick, so don't consider this "production" quality. Just create a new vcl application and drop a TMemo and a TButton on the form and assign the Button1Click to TButton's OnClick event.
unit main;
interface
uses
Windows,
Messages,
SysUtils,
Classes,
Controls,
Forms,
StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function GetActivePageUrlFromChrome(Handle: HWnd; Param: LParam): Bool; stdcall;
var
Form1 : TForm1;
implementation
{$R *.dfm}
function GetActivePageUrlFromChrome(Handle: HWnd; Param: LParam): Bool; stdcall;
var
List: TStrings;
hWndChrome, hWndChromeChild: HWND;
Buffer : array[0..255] of Char;
begin
List := TStrings(Param);
//get the window caption
SendMessage(Handle, WM_GETTEXT, Length(Buffer), integer(#Buffer[0]));
//look for the chrome window with "Buffer" caption
hWndChrome := FindWindow('Chrome_WidgetWin_0', Buffer);
if hWndChrome <> 0 then
begin
hWndChromeChild := FindWindowEx(hWndChrome, 0, 'Chrome_AutocompleteEditView', nil);
if hWndChromeChild <> 0 then
begin
SendMessage(hWndChromeChild, WM_GETTEXT, Length(Buffer), integer(#Buffer));
List.Add(Buffer);
end;
end;
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
slChromeUrl : TStringList;
begin
slChromeUrl := TStringList.Create;
try
EnumWindows(GetActivePageUrlFromChrome, LParam(slChromeUrl));
Memo1.Lines.AddStrings(slChromeUrl);
finally
FreeAndNil(slChromeUrl);
end;
end;
end.
Error:
try
EnumWindows(GetActivePageUrlFromChrome, LParam(slChromeUrl));
Memo1.Lines.AddStrings(slChromeUrl);
Works:
try
EnumWindows(#GetActivePageUrlFromChrome, LParam(slChromeUrl));
Memo1.Lines.AddStrings(slChromeUrl);
Apparently there's an issue open to request DDE support by chrome/chromium, keep a look-out there if a future version would provide it:
http://code.google.com/p/chromium/issues/detail?id=70184

Resources