First, let your first DLL project 1.File-> Close All-> file-> new ﹝ DLL
Code: // Automatically generates the following library project2; // This has a paragraph nonsense. Uses sysutils, classes; {$ r * .res} begin end.
2. Add a FUNC to come in:
Code: library project2; uss sysutils, classes; function mymax (x, y: integer): integer; stdcall; begin if x> y; result: = x else result: = y; end; // Remember: library name size It doesn't matter, but the case of DLL-FUNC is related. // written in DLL-FUNC-NAME to Mymax and Mymax is different. If you write a wrong, the immediate // The result is that your AP you call this DLL can't be found. // The case of the parameter is not related. There is no need to be the same name. If the prototype is (x, y: integer) but write // is written (A, B: Integer), it is nothing. // Remember: Add a stdcall again. In the book, if you write the DLL with Delphi, and hope not only to // delphi-ap, you want to use BCB / VC-AP, then you'd better add a stdcall; instruction // parameter type: Delphi has Many of its own variable types, of course, is not the DLL you like //, the Native language of Windows / DLL should be C. So if you want to pass the parameters of the DLL, we // use it as much as possible. These both, the latter will have a lot of trouble. If you are not familiar with C, it doesn't matter. Let's talk about it later. {$ R * .RES} begin end.
3. Send these shared FUNCs to make the outside world are your delphi-ap ﹞ ﹞用: Light, your AP can't use this, you have to add an exports.
Code: {$ r * .res} exports mymax; begin end.
4. Ok, you can compile according to Ctrl-F9. Do not press F9 at this time. DLL is not EXE ┌ can not be executed separately, if you press F9, there will be ErrorMSG. At this time, if the DLL has error, please correct it. Press CTRL-F9 again. At this time, there may be Warning, don't close, study it, see it. Press CTRL-F9 again, "DONE, COMPILED". There will be a * .dll with a directory. Congratulations, I have already gone. Second, test: open a new application: 1. Add a TButton
Code: ShowMessage (INTTOSTR (MyMax (30, 50)));
2. Inform EXE to catch a FUNC
Code: // In Form, Interface, Var plus function mymax (x, y: integer): integer; stdcall; external 'mytestdll.dll'; // mytestdll.dll for your DLL project name // DLL Name sensation does not matter. But remember to add an Extension .dll. In Win95 or NT, // is not an extension, but these two OS may be less and less. To add Extension Europe. Yes, simple. Is it very simple? Friends who are familiar with Delphi can see that the above code and the general Delphi program are basically the same, but there is more stdcall parameters after the TestDll function and declare the TestDll function with the exports statement. As long as the above code is compiled, you can get a dynamic link library called Delphi.dll. Now let's take a look at what you need to pay attention to. First, the function or procedure written in the DLL must add the stdcall call parameters. This call parameter is FAR in the Delphi 1 or Delphi 2 environment. This parameter is changed from Delphi 3 for stdcall, and the purpose is to replace the optimized Register parameter in order to use standard Win32 parameter transfer technology. Forgot to use the stdcall parameter is a common error. This error does not affect the compilation and generation of the DLL, but a very serious error occurs when calling this DLL, resulting in the deadlock of the operating system. The reason is that the register parameter is the default parameter of Delphi. Second, the written function and process should be declared using an Exports statement as an external function. As everyone see, the TestDLL function is declared as an external function. This can make this function to see the externally, the specific method is the "Quick View" feature to view the DLL file. (If there is no "quick view" option to be installed from the Windows CD.) The TestDll function appears in the Export Table column. Another very good reason is that if you don't declare this, the function we have written will not be called, this is everyone who is not willing to see. Third, when using a long string type, the variable is referenced to ShareMem. The String type in Delphi is very powerful. We know that the normal string length is 256 characters, but the String type in Delphi can reach 2G by default. (Yes, you don't have a mistake, it is indeed two megabytes.) At this time, if you insist on using the string type parameter, the variable is even logging information, it is to reference the ShareMem unit, and must be the first reference. Being the first referenced unit after the Uses statement. Such examples: Uses ShareMem, Sysutils, Classes; there is a little, in your project file (* .dpr) instead of the unit file (* .pas), do the same job, this point of Delphi comes with help files Did not clear, causing a lot of misunderstandings. If you don't do this, you are very likely to pay the cost of the crash. Avoid using String types is to declare the parameters, variables of the String type as PCHAR or shortstring (eg S: String [10]) type. The same problem occurs when you use a dynamic array, the method of solving is the same as above. Chapter 3 Static calling DLL TOP in Delphi Calling a DLL is easy to write a DLL. First, give you a static call method, will introduce the dynamic call method and make a comparison on both ways. Similarly, we first raised an example of static calls.
unit Unit1; interface uses Windows, Messages, SysUtils, 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 {$ r * .dfm} // The following code is the code for our true handwritten code FUNCTION TESTDLL (i: integer): integer; stdcall; external 'delphi. DLL '; procedure tform1.button1click (sender: TOBJECT); Begin Edit1.Text: = INTTOSTR (Testdll (1)); End; End. In the above example we placed a edit box (Edit) on the form (Edit) and one Button, and write very little code to test our Delphi.dll we have just written. Everyone can see that our unique work is to put the test portion of the TestDll function in the Implementation, and specify the location of Delphi.dll with the External statement. (In this example, the calling program and Delphi.dll are in the same directory.) Is exciting that the TestDLL function we wrote quickly was quickly recognized by Delphi. You can do such an experiment: Enter "TestDLL (", very fast Delphi will use the fly-by prompt to prompt what you should enter, just as simple as we are using other functions defined in Delphi. Notes have the following Some: 1. Call parameters use stdcall. As mentioned earlier, the STDCALL parameters should be used when referenced in the DLL, the cause is the same as mentioned earlier. Second, use the External statement to specify the called DLL file The path and name. As everyone saw, we specify the name of the DLL file to be called in the External statement. No write path is because the DLL file and the main program of calling it in the same directory. If the DLL file is C: /, we can write the above reference statement as External 'c: /delphi.dll'. Note that the suffix of the file must be written. Third, the global variable cannot be called from the DLL. If we declare in the DLL Some global variables, such as: Var s: Byte. This global variable in the DLL can be used normally, but s cannot be used by calling, both S cannot pass to the calling program as global variables. But in the calling program The variables in the declaration can be passed to the DLL. 4, the DLL called the call must exist. This is important, using a static call method requires the DLL file called the call and the function or process to be called, etc., if not If the path and file name exists or specified are incorrect, the system will prompt "Out" or "No * .dll file" or "No * .dll file" when running the main program. Chapter IV Dynamic call DLL TOP Dynamics in Delphi Calling DLL is relatively complicated, but very flexible. In order to fully explain this problem, this time we will call an example of a DLL written by C . First, compile the following DLL source program in C .
#include extern "c" _declspec (dllexport) int WinAPI TestC (INT i) {return i;} Generate a DLL file after compiling, here we call the file as cpp.dll, only one of the DLL returns an integer type function Testc. For convenience, we still refer to the above call program, just replace the statements in the original Button1Click process with the following code. procedure TForm1.Button1Click (Sender: TObject); type TIntFunc = function (i: integer): integer; stdcall; var Th: Thandle; Tf: TIntFunc; Tp: TFarProc; begin Th: = LoadLibrary ( 'Cpp.dll'); {Loading DLL} if TH> 0 THEN TRY TP: = GetProcaddress (th, pchar ('testc')); if TP <> nil dam tf: = TintFunc (TP); Edit1.Text: = INTOSTR (TF (1 )); {Call TESTC function} END ELSE ShowMessage ('TestC function is not found "; Finally Freelibrary (TH); {Release DLL} end else showMessage (' cpp.dll did not find '); END; everyone has seen, This dynamic calling technology is very complicated, but as long as the parameters are modified, if the DLL name in LoadLibrary ('cpp.dll') is 'delphi.dll', the called DLL can be dynamically changed. 1. Define the type of function or process to be called. In the above code we defined a TintFunc type, which corresponds to the function TESTC we want to call. Do the same definition work in other calls. And also add the stdcall call parameters. Second, release the called DLL. We use LoadLibrary dynamics to call a DLL, but remember to manually release the DLL with Freelibrary after use, otherwise the DLL will always use the memory until you exit Windows or shutdown. Now let's evaluate the advantages and disadvantages of two ways to call DLL. The static method is simple, easy to master and generally a little faster, but also more secure and reliable; however, the static method cannot flexibly loading the DLL required for running at runtime, but load the specified DLL until the main program starts running The DLL is released at the end of the program, and the method can be used only if the compiler and the system (such as Delphi) can be used. The dynamic method solves the deficiencies in the static method. It can easily access the functions and processes in the DLL, even newly added functions or processes in some old version DLLs; but the dynamic method is difficult to fully master, because of different Functions or processes must define a lot of complicated types and call methods. For beginners, the author recommends that you use a static method to use a dynamic call method after you are skilled. Chapter 5 uses DLL practical skills TOP one, write skills. 1. In order to ensure the correctness of the DLL, it can be written as part of the ordinary application. After the debug is correct, then from the main program, compile into a DLL. 2, in order to ensure the versatility of the DLL, the name of the visual control should be deducted in the DLL you have written, such as the Edit1 name in Edit1.Text; or custom type non-Windows definition, such as some record. 3, for easy debugging, each function and process should be as short as possible, and cooperate with specific detailed comments.