Delphi Dll Dynamic Invocation Example (1)

Source: Internet
Author: User

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

First, write the DLL

Library TestDllByD2007;

Uses
Sysutils,
Classes;
function test (const ASTR:PCHAR): Boolean;stdcall;
Begin
Result:=true;
End
{$R *.res}
Exports test;
Begin
End.

Note: 1. You cannot use the string type, otherwise it involves the issue of resource release, using Pchar instead of string.

String cannot be returned directly in 2.Dll, unless the SHAREMEM unit is referenced, the publisher needs to include BORLNDMM.DLL

Second, write the test form, just a button. In the Code of the button, the dynamic loading and releasing of the DLL is implemented.

Unit Unit1;

Interface

Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;

Type
Tftest=function (const Astr:pchar): Boolean;
TForm1 = Class (Tform)
Btn1:tbutton;
Procedure Btn1click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End

Var
Form1:tform1;

Implementation

{$R *.DFM}

Procedure Tform1.btn1click (Sender:tobject);
Var
sdll:string;
Tfttt:tftest;
Handle:thandle;
sstr:string;//declares the variable in order to avoid an exception.
Begin
sdll:= ' TestDllByD2007.dll ';
Caption:= ";
Handle:=loadlibrary (Pansichar (Sdll));
If Handle <> 0 Then
Begin
@tfTTT: = GetProcAddress (Handle, ' test ');
If @tfTTT <> Nil Then
Begin
sstr:= ' testsss ';
If TFTTT (PChar (SSTR)) =true Then
Begin
Caption:= ' true ';
End
Else
Begin
Caption:= ' false ';
End
End
Else
Begin
Caption:= ' can not find the test function ';
End
FreeLibrary (Handle);
End
Else
Begin
Caption:= ' Can not load library ' + Sdll;
End
End

End.

Delphi Dll Dynamic Invocation Example (1)

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.