在DLL中调用组件的小例子

来源:互联网 发布:python数据挖掘案例 编辑:程序博客网 时间:2024/05/24 04:16
 前几天CSDN中的一个朋友询问在DLL中访问窗体中的组件的问题,偶对DLL也不熟,也没有搞懂,昨天他对我说问题解决了,我借其小结一下,让自己印像深刻:
在DLL中访问组件时要类似这样进行判断:
      if panl.Controls[i].ClassNameIS('TEDIT') then
        TEdit(panl.Controls[i]).Clear;
而不能用这样进行判断:
      if panl.Controls[i] is TEDIT then
        TEdit(panl.Controls[i]).Clear;
对于DLL的知识回顾如下:
DLL在win16与win32中有很大区别,在win16中DLL中是一个全局即只他配一次地址当两个DLL访问同一个变量会有冲突,而在win32中则对于每个实例分配一个独立的地址空间,这类似于COM了,呵,待我回家看看书再好好组织一下语言,现在把DLL创建的过程写一下:
主框架:
library DLL名称;
uses  相关的单元

library Project1;

定义过程或函数
procedure   过程名;stdcall;
begin
end;

exports
  过程名   ---------这是让其它程序可以调用的关键
begin
end;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  ExtCtrls,
  StdCtrls,
  Dialogs,
  Controls;

procedure ClearPanelText(panl:Tpanel);stdcall;
var i:integer;
begin
  showmessage(inttostr(panl.ControlCount));
  for i:=0 to panl.ControlCount-1 do
  begin
      if panl.Controls[i].ClassNameIS('TEDIT') then
        TEdit(panl.Controls[i]).Clear;
      if panl.Controls[i].ClassNameIS('TCombobox') then
        TCombobox(panl.Controls[i]).Text:='';

  end;
end;

function getsum(i: integer): integer;stdcall;
begin
  result := i;
end;
{$R *.res}

exports
  ClearPanelText,getsum;
begin
end.


调用的例子:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    Edit1: TEdit;
    Edit2: TEdit;
    ComboBox1: TComboBox;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    procedure ClearText(panl:Tpanel);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

procedure ClearPanelText(panl:Tpanel);stdcall;external'Project1.dll';
function getsum(i: integer):integer;stdcall;external'Project1.dll';
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  i := 200;
  ClearPanelText(panel1);
  //showmessage(inttostr(getsum(i)));
end;

procedure TForm1.ClearText(panl:Tpanel);
var i:integer;
begin
  for i:=0 to panl.ControlCount-1 do
  begin
    if  panl.Controls[i] is TEdit then
        TEdit(panl.Controls[i]).Clear
    else   if panl.Controls[i]is TCombobox then
              TCombobox(panl.Controls[i]).Text :='' ;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ClearText(panel1);
end;

end.
原创粉丝点击