Delphi writes and calls the DLL (DELPHI7 test pass)

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_4dbbf76f01000anz.html

1 Delphi Writing DLLs2The following is a simple DLL written in Delphi, where there is only one Max function, which returns a large number of 2 numbers (Delphi5.0)3 1, new->dll; named Dll_0001, write the code:4 LibraryDll_0001;5 uses6 Sysutils,7 Classes;8 {$R *. RES}9 functionMax (X,y:integer): integer;stdcall;Ten begin One      if(x>y) Then AMax: =x -       Else -Max: =y; the End; - exportsMax; - begin - End. + The red part is written for itself, this is the same as the normal Delphi function, just take a stdcall parameter in the return value, and then use exports to export the function -================================================================================ + DELPHL calling DLLs A Call DLL in dynamic call and static call 2, dynamic call write simple, secure point, dynamic call complex many, but very flexible; atNow write a program to invoke the Dll_ written above0001the Max function in a. dll file -First, new A application, put 2 tedit in the form,1a Tlabek; - Second, - 1, static invocation - UnitUnit1; - Interface in uses - Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, to Stdctrls; + type -TForm1 =class(Tform) the Edit1:tedit; * Edit2:tedit; $ Label1:tlabel;Panax Notoginseng      procedureEdit2keydown (Sender:tobject;varKey:word; - shift:tshiftstate); the    Private +      {Private Declarations} A     Public the      {Public Declarations} +    End; - var $ Form1:tform1; $ Implementation - {$R *. DFM} - functionMax (X,y:integer): integer;stdcall; the External 'Dll_0001.dll'; - procedureTform1.edit2keydown (Sender:tobject;varKey:word;Wuyi shift:tshiftstate); the begin - ifKey =vk_return Then WuLabel1. Caption: =IntToStr (Max (Strtoint (Edit1.text), Strtoint (Edit2.text))); - End; About End. $ The Red code adds itself, where the dll_name in external "dll_name" can be the absolute path to the DLL, and if the DLL file is in your search path, you can write the file name directly, but the. dll cannot write less - 2, dynamic invocation, code as follows; - UnitUnit1; - Interface A uses + Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, the Stdctrls; - type $TForm1 =class(Tform) the Edit1:tedit; the Edit2:tedit; the Label1:tlabel; the      procedureEdit2keydown (Sender:tobject;varKey:word; - shift:tshiftstate); in    Private the      {Private Declarations} the     Public About      {Public Declarations} the    End; the var the Form1:tform1; + Implementation - {$R *. DFM} the procedureTform1.edit2keydown (Sender:tobject;varKey:word;Bayi shift:tshiftstate); the type theTfunc =function(x,y:integer): integer;stdcall; - var - Th:thandle; the Tf:tfunc; the Tp:tfarproc; the begin the ifKey =vk_return Then - begin theTh: =loadlibrary ('Dll_0001.dll');{Load DLL} the if(Th >0) Then the Try94Tp: =getprocaddress (Th,pchar ('Max')); the if(Tp <>Nil) Then the begin         {begin 1} theTf: =Tfunc (Tp);98Label1.Caption: =IntToStr ( About Tf (Strtoint (Edit1.text), Strtoint (Edit2.text))); - End   {End 1}101 Else102ShowMessage ('function Max not found.');103 finally104 FreeLibrary (Th); the End106 Else107ShowMessage ('Dll_0001.dll not exsit.');108 109 End; the End;111 End.

Delphi writes and calls the DLL (DELPHI7 test pass)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.