Delphi EXPERT ToolServices.getCurrentFile failing - delphi

What is a new way to get current file that is being worked on in Experts for Delphi XE
Previously in Delphi 5-7 we used ToolServices.getCurrentFile

Perhaps the deprecated units ToolIntf, ExptIntf etc. are no longer working. You can use IOTAModuleServices.CurrentModule instead. Here's a quick example:
function GetCurrentEditorFileName: string;
var
Module: IOTAModule;
Editor: IOTAEditor;
begin
Result := '';
Module := (BorlandIDEServices as IOTAModuleServices).CurrentModule;
if Assigned(Module) then
begin
Editor := Module.CurrentEditor;
if Assigned(Editor) then
Result := Editor.FileName;
end;
end;

An alternate method is to pass the "name of file in the editor" to your tool as a parameter. $EDNAME

Related

How do I get Welcome Page browser navigate to some URI from within OTA package wizard?

What I'm trying to do is to create an ability to view (not to edit) the HTML pages included into project. The Welcome Page already has embedded web browser, so it appears to be a good candidate for that.
Curios why? Here is a question with background info.
Here is a solution I've made specifically for you...
Download the source from here, extract and load the package in Delphi (I made it in Delphi XE, but it'll load in any version! You will need to change the Unit Output path in Project Options on pre-XE versions, though)... install the package.
In the Help menu, find Create Browser and click it. This will then create and display a tab which navigates to my blog (for the purpose of example).
You can easily modify this to suit your needs! The Help Menu Item code is located in EditWizard.MenuItem.pas, and can be disregarded! Just note that it's making a call when clicked to (BorlandIDEServices as IOTAEditorViewServices).ShowEditorView(CreateTab('http://www.simonjstuart.com'));, which is what actually creates a browser tab instance!
All the code for the Browser Tab (including its Frame layout) is contained in EditorWizard.Frame.pas, which makes it really easy to modify to suit your needs!
The unit EditorWizard.Wizard.pas contains the small amount of code needed to register the custom browser tab into the IDE.
Some tweaking will of course be required on your part, but this should certainly serve as a very acceptable base for what you're trying to do.
Enjoy :)
In case you're willing to use a hack like this:
type
TOpenNewURLModule = procedure(const URL: string; EditorForm: TCustomForm);
procedure OpenURL(const URL: string);
var
EditWindow: INTAEditWindow;
Lib: HMODULE;
OpenNewURLModule: TOpenNewURLModule;
begin
EditWindow := (BorlandIDEServices as INTAEditorServices).TopEditWindow;
if not Assigned(EditWindow) or not Assigned(EditWindow.Form) then
Exit;
Lib := GetModuleHandle('startpageide150.bpl');
if Lib = 0 then
Exit;
OpenNewURLModule := GetProcAddress(Lib, '#Urlmodule#OpenNewURLModule$qqrx20System#UnicodeStringp22Editorform#TEditWindow');
if #OpenNewURLModule <> nil then
OpenNewURLModule(URL, EditWindow.Form);
end;
Cons:
it's a hack (startpageidexx.bpl is not exposed through the API or documented)
it's hard-coded for Delphi XE (you need a different file name for other versions, the method signature might be different, too - e.g. in older AnsiString versions)
it does nothing if there is no edit window (there has to be at least one open module)
it always opens a new browser view
EDIT: It seems to be possible to reuse an existing open Welcome page, as well as make this hack compatible with older versions of Delphi. The following shows two alternatives, Delphi XE and Delphi 2007 (both seem to be working):
type
IURLModule = interface(IOTAModuleData)
['{9D215B02-6073-45DC-B007-1A2DBCE2D693}']
function GetURL: string;
procedure SetURL(const URL: string);
property URL: string read GetURL write SetURL;
end;
TOpenNewURLModule = procedure(const URL: string; EditorForm: TCustomForm);
function FindURLModule: IURLModule;
var
I: Integer;
begin
Result := nil;
with BorlandIDEServices as IOTAModuleServices do
for I := 0 to ModuleCount - 1 do
if Supports(Modules[I], IURLModule, Result) then
Break;
end;
procedure OpenURL(const URL: string; ReuseExistingView: Boolean = True);
{$IFDEF VER220} // Delphi XE
const
SStartPageIDE = 'startpageide150.bpl';
SOpenNewURLModule = '#Urlmodule#OpenNewURLModule$qqrx20System#UnicodeStringp22Editorform#TEditWindow';
{$ENDIF}
{$IFDEF VER185} // Delphi 2007
const
SStartPageIDE = 'startpageide100.bpl';
SOpenNewURLModule = '#Urlmodule#OpenNewURLModule$qqrx17System#AnsiStringp22Editorform#TEditWindow';
{$ENDIF}
var
Module: IURLModule;
EditWindow: INTAEditWindow;
Lib: HMODULE;
OpenNewURLModule: TOpenNewURLModule;
begin
EditWindow := nil;
Module := nil;
if ReuseExistingView then
Module := FindURLModule;
if Assigned(Module) then
begin
Module.URL := URL;
(Module as IOTAModule).Show;
end
else
begin
{$IFDEF VER220}
EditWindow := (BorlandIDEServices as INTAEditorServices).TopEditWindow;
{$ENDIF}
{$IFDEF VER185}
if Assigned((BorlandIDEServices as IOTAEditorServices).TopView) then
EditWindow := (BorlandIDEServices as IOTAEditorServices).TopView.GetEditWindow;
{$ENDIF}
if not Assigned(EditWindow) or not Assigned(EditWindow.Form) then
Exit;
Lib := GetModuleHandle(SStartPageIDE);
if Lib = 0 then
Exit;
OpenNewURLModule := GetProcAddress(Lib, SOpenNewURLModule);
if #OpenNewURLModule <> nil then
OpenNewURLModule(URL, EditWindow.Form);
end;
end;
Remaining cons:
it's still a hack
it's still hard-coded, for Delphi XE (Unicode) and Delphi 2007 (ANSI)
it still does nothing if there is no edit window
Perhaps you can use this as a start if you need compatibility for other versions.
You are better off displaying your own TForm with a TWebBrowser component on it that you can load HTML into.

How to get the main source file name from an IOTAProject?

Since at least D2007 a project file can have a main source file with differing base name. The DevExpress demos make use of this: E.g. there is a single dpr file UnboundListDemo.dpr which serves as the main source for both UnboundListDemoD11.dproj and UnboundListDemoD12.dproj.
Now if I have a Project: IOTAProject then Project.FileName returns the dproj file name. I couldn't find an "official" way to get the dpr's file name. Is there any? One can get it from parsing the dproj file (see here) but I'd prefer a ToolsAPI method.
Edit: I came up with this code based on Jon's answer:
function IsProjectSource(const FileName: string): Boolean;
begin
Result := IsDpr(FileName) or IsBpr(FileName) or IsPackage(FileName);
end;
function GxOtaGetProjectFileName2(Project: IOTAProject; NormalizeBdsProj: Boolean = False): string;
var
i: Integer;
Module: IOTAModule;
Editor: IOTAEditor;
begin
Result := '';
if Assigned(Project) then begin
Result := Project.FileName;
if NormalizeBdsProj and IsBdsprojOrDproj(Result) then begin
Module := Project as IOTAModule;
for i := 0 to Module.ModuleFileCount - 1 do
begin
Editor := Module.ModuleFileEditors[i];
if IsProjectSource(Editor.FileName) then begin
Result := Editor.FileName;
Exit;
end;
end;
end;
end;
end;
where the Is... routines are from GX_GenericUtils.
Edit 2: How to create one of these situations:
Create new VCL application.
Save as MyProject.dproj.
Close project in IDE.
In Windows explorer, rename MyProject.dproj to MyProjectD11.dproj.
From now on be sure to open MyProjectD11.dproj, not MyProject.dpr!
If you iterate the editors on the IOTAProject instance, you'll probably find the dpr.
var
Module: IOTAModule;
Project: IOTAProject;
Editor: IOTAEditor;
begin
// Set Project Here....
Module := Project as IOTAModule;
for I := 0 to Module.ModuleFileCount - 1 do
begin
Editor := Module.ModuleFileEditors[I];
end;
end;

How can my program tell if Delphi is running?

I've heard that some custom component authors use an RTL routine that checks to see if Delphi is running in order to set up shareware restrictions. Does anyone know what this routine is? Checking obvious names like "DelphiRunning" or "IsDelphiRunning" doesn't turn up anything useful.
There are 2 different ideas here:
- Delphi is up and running
- The application is running under the debugger
The common way to test if Delphi is running is to check the presence of known IDE Windows which have a specific classname like TAppBuilder or TPropertyInspector.
Those 2 works in all version of Delphi IIRC.
If you want to know if your application is running under the debugger, i.e. launched normally from the IDE with "Run" (F9) or attached to the debugger while already running, you just have to test the DebugHook global variable.
Note that "Detach from program" does not remove the DebugHook value, but "Attach to process" sets it.
function IsDelphiRunning: Boolean;
begin
Result := (FindWindow('TAppBuilder', nil) > 0) and
(FindWindow('TPropertyInspector', 'Object Inspector') > 0);
end;
function IsOrWasUnderDebugger: Boolean;
begin
Result := DebugHook <> 0;
end;
If the goal is to restrict the use of a trial version of your component to when the application is being developped, both have flaws:
- Hidden windows with the proper Classname/Title can be included in the application
- DebugHook can be manually set in the code
You can use DebugHook <> 0 from your component code. DebugHook is a global variable (IIRC, it's in the Systems unit) that's set by the Delphi/RAD Studio IDE, and couldn't be set from anywhere else.
There are other techniques (FindWindow() for TAppBuilder, for instance), but DebugHook takes all of the work out of it.
This is a code snippet from www.delphitricks.com/source-code/misc/check_if_delphi_is_running.html.
function WindowExists(AppWindowName, AppClassName: string): Boolean;
var
hwd: LongWord;
begin
hwd := 0;
hwd := FindWindow(PChar(AppWindowName), PChar(AppClassName));
Result := False;
if not (Hwd = 0) then {window was found if not nil}
Result := True;
end;
function DelphiLoaded: Boolean;
begin
DelphiLoaded := False;
if WindowExists('TPropertyInspector', 'Object Inspector') then
if WindowExists('TMenuBuilder', 'Menu Designer') then
if WindowExists('TAppBuilder', '(AnyName)') then
if WindowExists('TApplication', 'Delphi') then
if WindowExists('TAlignPalette', 'Align') then
DelphiLoaded := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if DelphiLoaded then
begin
ShowMessage('Delphi is running');
end;
end;
function DelphiIsRunning: Boolean;
begin
Result := DebugHook <> 0;
end;

Basic code to Delphi

I'm struggling with the following:
The goal is to parametrize an automation server for openoffice and I'm programming in Delphi.
The piece of basic code I want to translate into Delphi code is:
Dim aProps(1) As New com.sun.star.beans.PropertyValue
aProps(0).Name = "FilterName"
aProps(0).Value = "Text - txt - csv (StarCalc)"
aProps(1).Name = "FilterOptions"
aProps(1).Value = sFilterOptions
My attempt in Delphi looks like
type TPrmRecord = packed Record
Name : String;
Value : String;
End;
Var
ooParams:Variant;
MyData : TPrmRecord;
Begin
ooParams:= VarArrayCreate([0, 1], varVariant);
MyData.Name := 'FilterName';
MyData.Value := 'Text - txt - csv (StarCalc)';
ooParams[0] := MyData;
MyData.Name := 'FilterOptions';
MyData.Value := '59/44,34,ANSI,1,';
ooParams[1] := MyData;
End;
This is not working does anyone have a suggestion how to tackle this?
Your TPrmRecord type is not what OO.org expects. You should not try to write your own types, but use those that OO.org exposes.
There is an LPGL-licensed toolbox for Delphi: Delphi OOo. In it you will find a unit OOoTools.pas, which exports a function CreateUnoStruct(). Use this and pass 'com.sun.star.beans.PropertyValue' as the name of the struct. You will get a Variant (or an array of those, depending on the other parameter value) back that you can use instead of TPrmRecord (something like the following, untested):
var
Params: Variant;
begin
Params := CreateUnoStruct('com.sun.star.beans.PropertyValue', 1);
Params[0].Name := 'FilterName';
Params[0].Value := 'Text - txt - csv (StarCalc)';
Params[1].Name := 'FilterOptions';
Params[1].Value := '59/44,34,ANSI,1,';
end;
It looks as though you're missing the creation of the COM class, which would be the equivalent of the New com.sun.star.beans.PropertyValue line in your code.
I suspect you need to import the type library into Delphi which would give you the objects, properties and methods you need to emulate the Basic behaviour.
Here is straight Delphi code without using Delphi OOo:
uses comobj;
var
OO_ServiceManager: OleVariant;
FileParams: OleVariant;
begin
OO_ServiceManager := CreateOleObject ('com.sun.star.ServiceManager');
FileParams := VarArrayCreate([0, 1], varVariant);
FileParams[0] := OO_ServiceManager.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
FileParams[0].Name := 'FilterName';
FileParams[0].Value := 'Text - txt - csv (StarCalc)';
FileParams[1] := OO_ServiceManager.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
FileParams[1].Name := 'FilterOptions';
FileParams[1].Value := '59/44,34,ANSI,1,';
end;
Have a look at this Thread in the german Delphi-PRAXiS forums. There is a whole delphi Unit posted doing some OOo automation.
use
var
ooParams:array[0..1] of TPrmRecord;
delphi uses strict type-casting, so this is causing an assignment error.

File MD5 checksum

In this question is mentioned the wcrypt2.
What I need is simply calculate the MD5 of a file. It would be perfect if I could calculate it without having to save it because it is a downloaded file in stream format.
I would like to have the most straightforward way to do that.
Thanks!
Here is a working code for Indy 10:
function MD5File(const FileName: string): string;
var
IdMD5: TIdHashMessageDigest5;
FS: TFileStream;
begin
IdMD5 := TIdHashMessageDigest5.Create;
FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Result := IdMD5.HashStreamAsHex(FS)
finally
FS.Free;
IdMD5.Free;
end;
end;
Regards,
OscaR1
Based on #dummzeuch answere I wrote this function:
function getMD5checksum(s: TStream): string;
var
md5: TIdHashMessageDigest5;
hash : T4x4LongWordRecord;
begin
md5 := TIdHashMessageDigest5.Create;
s.Seek(0,0);
hash := md5.HashValue(s);
result := IntToHex(Integer(hash[0]), 4) +
IntToHex(Integer(hash[1]), 4) +
IntToHex(Integer(hash[2]), 4) +
IntToHex(Integer(hash[3]), 4);
end;
Indy comes with functions for calculating several hashes, MD5 is one of them. Indy is included in all versions of Delphi since at least Delphi 2006 and available as a free download for older versions.
What about:
function GetFileMD5(const Stream: TStream): String; overload;
var MD5: TIdHashMessageDigest5;
begin
MD5 := TIdHashMessageDigest5.Create;
try
Result := MD5.HashStreamAsHex(Stream);
finally
MD5.Free;
end;
end;
function GetFileMD5(const Filename: String): String; overload;
var FileStream: TFileStream;
begin
FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Result := GetFileMD5(FileStream);
finally
FileStream.Free;
end;
end;
As you mentioned, the post you linked to talks about wcrypt2, which is a library of cryptographic routines, including MD5. The post you linked to also seems to indicate that it is available for Delphi 7 since the asker includes output labeled "Delphi 7." You have tagged this question delphi7, so I assume that's the version you're using, too. So what's stopping you from using wcrypt2?
The question links to a copy of wcrypt2.pas, and the copyright dates in that file appear to indicate that the unit was available by the time Delphi 7 was released. Check your installation; you might already have it. If not, then the unit also says that it was obtained via Project Jedi, so you could try looking there for the unit as well.
The answers to your referenced question include example Delphi code and the names of units that come with Delphi for doing MD5. They come with Delphi 2009, so you should check whether they're also available for your version.
Take a look at this implementation of MD5SUM in Delphi. It requires a string for input, but I imagine you can easily make it work with a stream.
MessageDigest_5 would work for this as well.
I use the following function in Delphi 7 with Indy 10.1.5
uses IdHashMessageDigest, idHash, Classes;
...
function cc_MD5File(const p_fileName : string) : string;
//returns MD5 has for a file
var
v_idmd5 : TIdHashMessageDigest5;
v_fs : TFileStream;
v_hash : T4x4LongWordRecord;
begin
v_idmd5 := TIdHashMessageDigest5.Create;
v_fs := TFileStream.Create(p_fileName, fmOpenRead OR fmShareDenyWrite) ;
try
v_hash := v_idmd5.HashValue(v_fs);
result := v_idmd5.AsHex(v_hash);
finally
v_fs.Free;
v_idmd5.Free;
end;
end;
If you use Overbyte http://www.overbyte.eu/frame_index.html just add unit and call function FileMD5 with name of file
uses OverbyteIcsMd5;
....
function GetMd5File:String;
begin
Result := FileMD5(FileName);
end;

Resources