DLL and call Method

Source: Internet
Author: User

1. implicit call.

Library project1;
Uses
Sysutils,
Classes;

{$ R *. Res}
Function sum (A, B: integer): integer; stdcall;
Begin
Result: = A + B;
End;

Function linkstr (str1, str2: pchar): pchar; stdcall;
Begin
Result: = pchar (strpas (str1) + strpas (str2 ));
End;

Function ifcheck (int1, int2: integer): Boolean; stdcall;
Begin
If int1> int2 then
Result: = true
Else
Result: = false;
End;
Exports
Sum, linkstr, ifcheck;

Begin

End.

========================

VaR
Form1: tform1;

Implementation
Function sum (A, B: integer): integer; stdcall; External 'project1. dll ';
Function linkstr (str1, str2: pchar): pchar; stdcall; External 'project1. dll ';
Function ifcheck (int1, int2: integer): Boolean; stdcall; External 'project1. dll ';

{$ R *. DFM}

Procedure tform1.button1click (Sender: tobject );
Begin
Self. Caption: = inttostr (sum (strtoint (edt1.text), strtoint (edt2.text )));
End;

Procedure tform1.button2click (Sender: tobject );
Begin
Self. Caption: = strpas (linkstr (pchar (edt1.text), pchar (edt2.text )));
End;

Procedure tform1.button3click (Sender: tobject );
Begin
If ifcheck (strtoint (edt1.text), strtoint (edt2.text) then
Self. Caption: = 'count 1> count 2'
Else
Self. Caption: = 'Number two is greater than number one ';
End;

End.

// Configure //------------------------------------------------------------------------------------

2. Explicit not called.

Procedure tform1.button1click (Sender: tobject );
VaR
Hdll: hmodule;
Sum: function (A, B: integer): integer; stdcall;
Begin
Hdll: = loadlibrary ('project1. dll ');
If hdll <> 0 then
Begin
@ Sum: = getprocaddress (hdll, 'sum ');
If @ sum <> nil then
Self. Caption: = inttostr (sum (strtoint (edt1.text), strtoint (edt2.text )))
Else
Showmessage ('this function does not exist in dll ');
Freelibrary (hdll );
End
Else
Showmessage ('dll cannot be loaded ');
End;

 

// End;

Procedure tform1.button2click (Sender: tobject );
VaR
Hdll: hmodule;
Linkstr: function (str1, str2: pchar): pchar; stdcall;
Begin
Hdll: = loadlibrary ('project1. dll ');
If hdll <> 0 then
Begin
@ Linkstr: = getprocaddress (hdll, 'linkstr ');
If @ linkstr <> nil then
Self. Caption: = strpas (linkstr (pchar (edt1.text), pchar (edt2.text )))
Else
Showmessage ('module function loading failed in dll ');
Freelibrary (hdll );
End
Else
Showmessage ('dll loading failed ');

End;

Procedure tform1.button3click (Sender: tobject );
VaR
Hdll: hmodule;
Ifcheck: function (A, B: integer): Boolean; stdcall;
Begin
Hdll: = loadlibrary ('project1. dll ');
If hdll <> 0 then
Begin
@ Ifcheck: = getprocaddress (hdll, 'ifcheck ');
If @ ifcheck <> nil then
If ifcheck (strtoint (edt1.text), strtoint (edt2.text) then
Self. Caption: = 'count 1> count 2'
Else
Self. Caption: = 'Number two is greater than number one'
Else
Showmessage ('error occurred during function loading ');
Freelibrary (hdll );
End
Else
Showmessage ('errors occurred when loading dll ');

End;

End.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.