To export a class from a DLL, you must first consider using a BPL package. This method is not good, that is, the user must be aware of the classes contained in the package, that is, the class name must be known-this is a limitation in a certain sense, imagine a situation, the user defines an abstract class and then defines many concrete classes. Therefore, for the user, he hopes to use these classes without knowing which classes are specific. It seems a bit mysterious, but the actual situation is true, when defining abstract classes, you cannot predict how many specific classes will be created in the future. What technology is required to implement such a requirement?
In fact, the implementation technology is not very difficult-the author here will dedicate his practical experience to everyone, as a reference, hope to see other better methods!
The following describes the basic knowledge involved in this method, and then uses an example to describe the specific implementation.
I. Basic Concepts
Meta class is also called class-reference type. It can be regarded as a type and the value of the variable declared for this type represents a class. For example, Type
Tclass = Class of tobject;
In this way, the type of a metadata class is declared. Then you can have such a variable declaration:
VaR
Aclass: tclass;
You can use this method:
Aclass: = tobject;
Or:
Aclass: = tbutton;
Or:
Aclass: = tform;
And so on.
Because tclass is a tobject type Meta class, while tbutton and tform are derived from tobject, values such as tbutton and tform are acceptable for Aclass.
Then, we can use the polymorphism idea to flexibly use Aclass variables. This is also the basic knowledge of the specific implementation below.
II. Specific implementation
Step 1: Create an abstract class:
Using such a simple declaration, this abstract class only provides one abstract method, but it does not affect our description:
Tmybaseform = Class (tform)
Protected
Function gettitle: pchar; virtual; abstract;
End;
Mybaseformclass = Class of tmybaseform;
We will not discuss how many practical methods and interfaces such an abstract class provides, because we will discuss the technical feasibility. Assume that the original intention of defining this interface is to obtain any number of changed titles, and the specific return value of gettitle must be implemented by subclass. In addition, the author also hopes that the sub-class code can be implemented in the DLL and separated from the main program. This method has some plug-ins, maybe some features of Plug and Play can be implemented. Is it quite attractive? So what should I do next?
First, the main program and the DLL program should include the units declared above. Then, the main program is responsible for implementing a driver-dynamic loading of DLL and dynamic loading of classes; while the DLL is responsible for implementing sub-classes.
Let's talk about DLL first. What should DLL do?
Step 2: Export subclass in dll:
We have designed the following two export functions:
1. Function getclasscount: integer; stdcall;
Tell the caller that there are several subclasses in this DLL;
2. Function getclasstypebyindex (const iindex: integer;
VaR classtype: mybaseformclass): wordbool; stdcall;
Obtain the specific subclass by indexing. Note that the classtype type here is mybaseformclass, which indicates that its value will be a class inherited from tmybaseform.
One of their possible implementations is as follows:
Function getclasscount: integer;
Begin
Result: = 3; // indicates that three classes are exported in the DLL.
End;
Function getclasstypebyindex (const iindex: integer;
VaR classtype: mybaseformclass): wordbool;
Begin
Result: = true;
Case iindex
0: classtype: = tfrmtest1;
1: classtype: = tfrmtest2;
2: classtype: = tfrmtest3;
Else
Result: = false;
End;
End;
Of course, tfrmtest1, tfrmtest2, and tfrmtest3 should be included in the Use List of the unit. The implementation of tfrmtest1 can be as follows:
Tfrmtest1 = Class (tmybaseform)
Protected
Function gettitle: pchar; override;
End;
Function tfrmtest1.gettitle: pchar;
Begin
Result: = 'Hello from tfrmtest1 ';
End;
Finally, do not forget to add getclasscount and getclassbyindex to the exports list. When building the DLL project, check the "use runtime package" in the project option-package. The specific reasons are described later.
So far, the DLL work has come to an end.
Step 3: implement the main program driver engine:
This step is relatively easy-it is nothing more than dynamic loading of DLL, then calling the getclasscount function, and then calling getclassbyindex. Key code:
VaR Aclass: tmybaseclass;
AForm: tmybaseform;
I, icount: integer;
Blresult: Boolean;
Begin
// Load the dynamic library slightly. Assume that fpgetclassproc points to the getclasscount function, and fpgetclassbyindexproc points to getclassbyindex. Then:
Icount: = fpgetclassproc;
For I: = 0 to icount-1 do
Begin
Aclass: = fpgetclassbyindex (I, blresult );
If blresult then
Begin
AForm: = Aclass. Create (application );
AForm. Caption: = AForm. gettitle;
AForm. show;
End;
End;
//...
End;
Note that, similar to DLL, you also need to select the runtime package when creating the output file. This is because if the runtime package is not used, multiple copies of the same class will exist in the memory, and false will be returned if the is operator is used for them.