我是DELPHI的初学者,现在要写一个简单的DLL,提供一个函数,输入两个字符串参数,返回一个字符串,请高手提供一个简单的例子!!谢谢!!

解决方案 »

  1.   

    最好能在一个窗体中用一个按钮的Click事件来实现!!
      

  2.   

    function GetStr: pchar; StdCall; external 'StrSS.DLL';
      

  3.   

    NamasAmitabha(银雨辰):
    我是初学者啊,您能不能提供完整的一个例子啊!!
      

  4.   

    随便找一本有dll编写的书看看就有啊
      

  5.   

    简单的例子。dll格式基本这样。代码不是很对
    library Project2;
    uses
      SysUtils,
      Classes,
      Unit1 in 'Unit1.pas';
    Exports GetStr;{$R *.RES}
    begin
    end.
    unit Unit1;
    interface
    uses sysutils;
    function GetStr(s1:pChar;s2:pChar):pChar; StdCall ;
    implementation
    function GetStr(s1:pChar;s2:pChar):pChar;
    begin
      GetMem(Result,Length(s1)+Length(s2));
      Result:=StrCat(Result,s1);
      Result:=StrCat(Result,s2);
    end;
    end.
    以下是调用
    unit Unit6;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;
    function GetStr(s1:pchar;s2:pchar):pchar;stdcall;external 'Project2.dll'
    implementation
    {$R *.DFM}
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage( GetStr('fdfd','fff'));
    end;
    end.
      

  6.   

    to:rockswj(石头)
    您的方法我先试一试!!
      

  7.   

    library Project1;{ 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;{$R *.res}
    function ReadPort1(Port:integer):integer;stdcall;
    var
    a:integer;
    begin
    a:=port+1;
    Result:=a;
    end;function ReadPort(Port:WORD):BYTE;stdcall;
    var
    B:BYTE;
    begin
    asm
    MOV DX, Port;
    IN AL, DX;
    MOV B, AL;
    end;
    Result:=B;
    end;procedure  WritePort(Port:WORD;ConByte:BYTE);stdcall;
    begin
    ASM
    MOV DX, Port;
    MOV AL, ConByte;
    OUT DX, AL;
    END;
    end;exports
    readport1,
    readport,
    WritePort;
    begin
    end.
      

  8.   

    to:rockswj(石头)
    您的代码编译不过啊!!
      

  9.   

    楼主:源代码为这样:
    =========================
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        editIN: TEdit;
        btDouble: TButton;
        btTriple: TButton;
        Label1: TLabel;
        Label2: TLabel;
        editRESULT: TEdit;
        Edit1: TEdit;
        Label3: TLabel;
        Edit2: TEdit;
        Label4: TLabel;
        Edit3: TEdit;
        Label5: TLabel;
        Button1: TButton;
        procedure btDoubleClick(Sender: TObject);
        procedure btTripleClick(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    //
    function Double(N:Integer):Integer;
      stdcall;external'myDLL.dll';
    function Triple(N:Integer):Integer;
      stdcall;external'myDLL.dll';
    function GetStr(A1:OleVariant;A2:OleVariant):OleVariant;
      stdcall;external'myDLL.dll';
    {$R *.dfm}procedure TForm1.btDoubleClick(Sender: TObject);
    begin
      if editIN.Text<>'' then
      begin
          editResult.Text:=
          IntToStr(Double(StrToInt(editIN.Text)));
      end;
    end;procedure TForm1.btTripleClick(Sender: TObject);
    begin
      if editIN.Text<>'' then
      begin
          editResult.Text:=
          IntToStr(Triple(StrToInt(editIN.Text)));
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Edit3.Text:=GetStr(Edit1.Text,Edit2.Text);
    end;end.
      

  10.   

    范例及代码全部都发出去给你了!
    清查收吧,还有问题可以再找我。
    ===============================
    我这段时间在CSDN上见到很多兄弟
    都提这个问题:“怎样在DLL上传递String
    的参数,在我这个范例中我已经作了很容易
    明白的例子”如果哪位需要的都可以跟我联系!
    ===========================================
    我来帮你们回答!
      

  11.   

    to:lgqTiger(【老虎】) 
    您的范例已经收到,先谢了,对于在DLL中使用一个窗体或者是一个VCL控件,又该怎么办呢??
      

  12.   

    不是吧。应该能编译过去吧。
    unit6是一个新的工程的单元,是用来测试dll的。
      

  13.   

    回复人: Sunny_Yirui(雪飘飘) ( ) 信誉:100  2004-01-31 14:05:00  得分:0 
     
     
      to:lgqTiger(【老虎】) 
    您的范例已经收到,先谢了,对于在DLL中使用一个窗体或者是一个VCL控件,又该怎么办呢??
      
     
    ==============================先作一个带窗体的unit,然后再在DLL中uses,试试吧?
      

  14.   

    谢谢老虎,给我你份
    [email protected]
      

  15.   

    给个例子!
    邮箱为:[email protected]
    谢谢老虎