I was trying to understand how to use units in my main 'modules test unit'. they are 'module1.pas', and 'module2.pas'.
This is a console program and I would like for both units to be displayed and used in my main unit modules_test:
program modules_test;
uses
SysUtils, module1, module2;
procedure modules_display;
begin
module1;
module2;
end;
end.
here's unit module1:
unit module1;
interface
uses
Classes, SysUtils;
implementation
begin
writeln('this is module 1....');
end.
And module2:
unit module2;
interface
uses
Classes, SysUtils;
implementation
begin
writeln('this is module 2....');
end.
As I'm fairly certain that I'm missing a few things, as well as the errors I get, what would I need to use for this to execute properly?
program modules_test;
{$APPTYPE CONSOLE}
uses
SysUtils, module1, module2;
procedure modules_display;
begin
module1.Test; // Fully qualify the name of the procedure
module2.Test;
end;
begin
modules_display;
ReadLn;
end.
unit module1;
interface
// Declare a procedure that can be called from outside of this unit
procedure Test;
implementation
uses
// Unit references that are exclusively used in the implementation section
Classes, SysUtils;
// This is the implementation of the procedure
procedure Test;
begin
writeln('this is module 1....');
end;
end.
unit module2;
interface
// Declare a procedure that can be called from outside of this unit
procedure Test;
implementation
uses
// Unit references that are exclusively used in the implementation section
Classes, SysUtils;
// This is the implementation of the procedure
procedure Test;
begin
writeln('this is module 2....');
end;
end.
See some documentation, Programs and Units.
Related
I have a simple project in Delphi:
program Project1;
uses
Forms,
Unit2 in 'Unit2.pas',
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Unit1:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
function encodeData(var Data:array of Byte; var Size: Integer): Integer;
var
i: Intger
begin
...
for i := 1 to Size do
begin
Data[i] := Data[i] + unit2.SomeArray[i]
end;
...
Result := 0;
Exit;
end
...
The second unit:
unit Unit2;
interface
implementation
const
SomeArray:Array [0..65000] of LongWord = (
...
);
end.
When I'm trying to build this project I get errors like this:
[Error] Unit1.pas(41): Undeclared identifier: 'SomeArray'
What's wrong with this code? I checked Delphi wiki and other questions and didn't find solution for this issue...
You need to define SomeArray in the interface section of the unit. Currently, you have it in the implementation section, which is purposely hidden from other units. Only things defined/declared in the interface are visible to other units.
In the documentation you linked, it's described:
The implementation section of a unit begins with the reserved word implementation and continues until the beginning of the initialization section or, if there is no initialization section, until the end of the unit. The implementation section defines procedures and functions that are declared in the interface section. Within the implementation section, these procedures and functions may be defined and called in any order. You can omit parameter lists from public procedure and function headings when you define them in the implementation section; but if you include a parameter list, it must match the declaration in the interface section exactly.
In addition to definitions of public procedures and functions, the implementation section can declare constants, types (including classes), variables, procedures, and functions that are private to the unit. That is, unlike the interface section, entities declared in the implementation section are inaccessible to other units.
(Emphasis mine)
I'm trying to automate an application (Windows 8, Delphi XE.) For my testing I'm doing the following:
Created a small test application, consisting of a form and a memo (Form1)
Added a new ActiveX Object, CoClass name TestOLE, Threading mode Apartment, Instancing Multiple (as per this article.)
Added one method Method1 which only adds some text to the memo control in Form1
I then start the application and double click on a file named test.vbs which contains the following code:
dim obj
set obj = GetObject("", "Project1.TestOLE")
obj.AddSomeText "Hola mundo"
When the application is running, I see that a new form is created, the text is added and then it exits.
What I want to accomplish is that the opened application should have its memo text changed.
I've repeated creating new projects with both MultipleInstance and SingleInstance, and in an outburst of heuristic anger, I even changed the threading model to single, to no avail.
I see two flags in the type library editor: "Replaceable" and "Aggregatable." However, selecting "Replaceable" ends up in an error in the generated RIDL file.
I've been reading a lot about GetObject. It appears that its documentation is even wrong (it says you can omit the first parameter but I've found that doesn't work).
Is this the right way to write an automation server in Delphi that can be reused?
Well, I got it working (I hope.)
Reading more of the same article cited above, found the following:
Know how to implement servers that support GetActiveObject.
Adding a global object, and registering in the Running Object Table (ROT) accomplishes the desired task of having the COM call passed to the running application:
Project file:
program TestOLEProject3;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
TestOLEProject3_TLB in 'TestOLEProject3_TLB.pas',
Unit2 in 'Unit2.pas' {TestOLE: CoClass},
Unit3 in 'Unit3.pas';
{$R *.TLB}
{$R *.res}
begin
Application.Initialize;
RegisterGlobalTestOLE;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Unit2.pas:
unit Unit2;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, TestOLEProject3_TLB, StdVcl;
type
TTestOLE = class(TAutoObject, ITestOLE)
protected
procedure Method1; safecall;
procedure Quit; safecall;
end;
implementation
uses ComServ, Unit1, Unit3;
procedure TTestOLE.Method1;
begin
Form1.Memo1.Lines.Add('Wheeee');
end;
procedure TTestOLE.Quit;
begin
RevokeGlobalTestOLE;
end;
initialization
TAutoObjectFactory.Create(ComServer, TTestOLE, CLASS_TestOLE, ciMultiInstance,
tmApartment);
end.
Unit3.pas (functions to register and unregister the global object):
unit Unit3;
interface
procedure RegisterGlobalTestOLE;
procedure RevokeGlobalTestOLE;
implementation
uses TestOLEProject3_TLB, ComObj, ActiveX;
var
GlobalTestOLEHandle: longint = 0;
procedure RegisterGlobalTestOLE;
var
GlobalTestOLE: ITestOLE;
begin
GlobalTestOLE := CoTestOLE.Create;
OleCheck(RegisterActiveObject(GlobalTestOLE, CLASS_TestOLE,
ACTIVEOBJECT_STRONG, GlobalTestOLEHandle));
end;
procedure RevokeGlobalTestOLE;
begin
if (GlobalTestOLEHandle <> 0) then
begin
OleCheck(RevokeActiveObject(GlobalTestOLEHandle, nil));
GlobalTestOLEHandle := 0;
end;
end;
end.
For example, I have a couple of functions written for my form. Now, I need the exact same functions in another form. So, how can I share them between the two forms? Please, provide a simple example if possible.
Don't put them in your form. Separate them and put them in a common unit, and add that unit to the uses clause where you need access to them.
Here's a quick example, but you can see many of the Delphi RTL units (for instance, SysUtils) that do this. (You should learn to use the VCL/RTL source and the demo apps that are included in Delphi; they could answer many of the questions you've posted more quickly than waiting for an answer here.)
SharedFunctions.pas:
unit
SharedFunctions;
interface
uses
SysUtils; // Add other units as needed
function DoSomething: string;
implementation
function DoSomething: string;
begin
Result := 'Something done';
end;
end.
UnitA.pas
unit
YourMainForm;
uses
SysUtils;
interface
type
TMainForm = class(TForm)
procedure FormShow(Sender: TObject);
// other stuff
end;
implementation
uses
SharedFunctions;
procedure TMainForm.FormShow(Sender: TObject);
begin
ShowMessage(DoSomething());
end;
end.
In more recent versions of Delphi than Delphi 7, you can create the functions/methods in a record instead:
unit
SharedFunctions;
interface
uses
SysUtils;
type
TSharedFunctions = record
public
class function DoSomething: string;
end;
implementation
function TSharedFunctions.DoSomething: string;
begin
Result := 'Something done';
end;
end;
UnitB.pas
unit
YourMainForm;
uses
SysUtils;
interface
type
TMainForm = class(TForm)
procedure FormShow(Sender: TObject);
// other stuff
end;
implementation
uses
SharedFunctions;
procedure TMainForm.FormShow(Sender: TObject);
begin
ShowMessage(TSharedFunctions.DoSomething());
end;
end.
If you need forms. You could use inherited forms. Creating a form that inherit the functions of a parent form.
The most interesting. Any changes in the parent form is reflected a change in inherited forms. You even can inherit form controls (tbutton, tlabel, etc...).
In GUI Delphi7. Option "new form", option "inherited from a existing form".
Example:
//MainForm.pas
type
TMainForm = class(TForm)
procedure MiFunction();
.
.
end;
//ChilForm.pas
type
TChildForm = class(TMainForm)
.
.
end;
I am having a strange problem of using interface in different versions of Delphi. The following minimized code compiles and runs as expected in Delphi XE and higher but not in Delphi 7. Specificaly, it seems when compiling in Delphi 7, the function TForm1.Load: IMoleculeSubject; does not returns the correct result, i.e., the correct reference to the newly created instance. Could you help to comment about the reason and possible workaround? Many thanks!
uInterface.pas
unit uInterface;
interface
type
IMoleculeSubject = interface
['{BEB4425A-186C-45DF-9DCE-C7175DB0CA90}']
end;
TMoleculeSubject = class(TInterfacedObject, IMoleculeSubject)
end;
implementation
end.
uBusiness.pas
unit uBusiness;
interface
uses
uInterface;
type
TMoleculeDecorator = class(TMoleculeSubject)
private
FID: Integer;
public
property ID: Integer read FID;
constructor Create;
end;
implementation
{ TMoleculeDecorator }
constructor TMoleculeDecorator.Create;
begin
inherited Create;
FID := Random(100);
end;
end.
Unit1.pas
unit Unit1;
interface
uses
uInterface, uBusiness,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
function Load: IMoleculeSubject;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
MolSubject: IMoleculeSubject;
begin
MolSubject := Load;
// The down-cast is to show the returned result is wrong in Delphi 7!
Caption := IntToStr(TMoleculeDecorator(MolSubject).ID);
end;
function TForm1.Load: IMoleculeSubject;
var
MolSubject: IMoleculeSubject;
begin
MolSubject := TMoleculeDecorator.Create;
Result := MolSubject;
end;
end.
The Load function works perfectly well in all versions of Delphi. The problem is your cast, which is what is known as an unsafe typecast. An unsafe typecast from an interface reference to an object has ill-defined behaviour in older versions of Delphi. However, the behaviour is well-defined in modern Delphi. The documentation says more.
So, the basic problem is that your expectations for the behaviour are not compatible with the Delphi 7 version of the language.
If you get the interface to return the ID you will find that the interface you are creating is as expected.
program InterfaceDemo;
{$APPTYPE CONSOLE}
uses
Classes;
type
IMyIntf = interface
function GetID: Integer;
end;
TImplementingObject = class(TInterfacedObject, IMyIntf)
private
FID: Integer;
function GetID: Integer;
public
constructor Create;
end;
{ TImplementingObject }
constructor TImplementingObject.Create;
begin
FID := Random(100);
Writeln(FID);
end;
function TImplementingObject.GetID: Integer;
begin
Result := FID;
end;
var
MyIntf: IMyIntf;
begin
Randomize;
MyIntf := TImplementingObject.Create;
Writeln(MyIntf.GetID);
Readln;
end.
It's rather unusual to ask for the implementing object from an interface. To do so suggests that there is a problem with your design. Should you really need to do so there are a few options:
In modern Delphi you can use the type-safe case with the as operator.
In older Delphi versions there are hacks that retrieve the implementing object: Casting a delphi interface to its implementation class without modifying the interface
You could add a function to the interface that returns the implementing object.
The latter option works in all versions of Delphi and does so without resorting to subterfuge.
Casting interfaces to objects is available since Delphi 2010. Where are workarounds for older Delphi versions, see for example How to cast a Interface to a Object in Delphi
I'm trying to call a function returning an interface from another unit; for instance consider the following:
program intf_sb1;
{$APPTYPE CONSOLE}
uses
myunit in 'myunit.pas';
var
MyBL: ISomeInterface;
begin
MyBL := GetInterface;
end.
where the content of myunit.pas is as follows:
unit myunit;
interface
type
ISomeInterface = interface
['{D25A26ED-7665-4091-9B0F-24DF37545E2A}']
end;
implementation
function GetInterface : ISomeInterface;
begin
end;
end.
My problem is that I get the error "E2003 Undecleared identifier GetInterface" when I try to run this program. Why isn't this allowed? Thanks in advance!
Declare the GetInterface function in the interface section as well. If you don't it is "private" to the unit.
IE:
type
ISomeInterface
...
end;
function GetInterface: ISomeInterface;
implementation
function GetInterface: ISomeInterface;
begin
...
end;