我想实现的功能很简单,通过Delphi创建一个dll文件,然后我把这个dll文件拷在服务器的指定目录,在客户端通过浏览器访问服务器这个文件就可以让这个DLL在服务器上运行,我试过NEW->other->AcitveX->下面应该选哪一项呢

解决方案 »

  1.   


    《手把手教delphi:写你的dll文件》
            ——转载自:中华网科技 http://tech.china.com   一、开始你的第一个DLL专案 
      1.File->Close all->File->New﹝DLL﹞   代码:
      //自动产生Code如下 
      library Project2; 
      //这有段废话 
      uses 
      SysUtils, 
      Classes;   {$R *.RES}   begin 
      end.
      2.加个Func进来: 
      代码:
      library Project2; 
      uses 
      SysUtils, 
      Classes; Function MyMax ( X , Y : integer ) : integer ; stdcall ; 
    begin 
    if X > Y then 
    Result := X 
    else 
    Result := Y ; 
    end ; 
    //切记:Library 的名字大小写没关系,可是DLL-Func的大小写就有关系了。 
    // 在 DLL-Func-Name写成MyMax与myMAX是不同的。如果写错了,立即 
    // 的结果是你叫用到此DLL的AP根本开不起来。 
    //参数的大小写就没关系了。甚至不必同名。如原型中是 (X,Y:integer)但引 
    // 用时写成(A,B:integer),那是没关系的。 
    //切记:要再加个stdcall。书上讲,如果你是用Delphi写DLL,且希望不仅给 
    // Delphi-AP也希望BCB/VC-AP等使用的话,那你最好加个Stdcall ; 的指示 
    //参数型态:Delphi有很多种它自己的变量型态,这些当然不是DLL所喜欢的 
    // ,Windows/DLL的母语应该是C。所以如果要传进传出DLL的参数,我们 
    // 尽可能照规矩来用。这两者写起来,后者会麻烦不少。如果你对C不熟 
    // 的话,那也没关系。我们以后再讲。   {$R *.RES}   begin 
      end.
      3.将这些可共享的Func送出DLL,让外界﹝就是你的Delphi-AP啦﹞使用:光如此,你的AP还不能用到这些,你还要加个Exports才行。 
      代码: 
      {$R *.RES} 
      exports 
      MyMax ; 
      begin 
      end.4.好了,可以按 Ctrl-F9编译了。此时可不要按F9。DLL不是EXE┌不可单独执行的,如果你按F9,会有ErrorMsg的。这时如果DLL有Error,请修正之。再按Ctrl-F9。此时可能有Warning,不要紧,研究一下,看看就好。再按Ctrl-F9,此时就『Done , Compiled 』。同目录就会有个 *.dll 。恭喜,大功告成了。 二、进行测试:开个新application: 
      1.加个TButton 
      代码:
      ShowMessage ( IntToStr(MyMax(30,50)) ) ;
      2.告知Exe到那里抓个Func 
      代码:
      //在Form,interface,var后加 
      Function MyMax ( X , Y : integer ) : integer ; stdcall ; external ‘MyTestDLL.dll‘ ; 
      // MyTestDLL.dll为你前时写的DLL项目名字 
      // DLL名字大小写没关系。不过记得要加 extension的 .DLL。在Win95或NT, 
      // 是不必加 extension,但这两种OS,可能越来越少了吧。要加extension  可以了,简单吧。
      上面的例子是不是很简单?熟悉Delphi的朋友可以看出以上代码和一般的Delphi程序的编写基本是相同的,只是在TestDll函数后多了一个stdcall参数并且用exports语句声明了TestDll函数。只要编译上面的代码,就可以玫揭桓雒狣elphi.dll的动态链接库。现在,让我们来看看有哪些需要注意的地方:  1.在DLL中编写的函数或过程都必须加上stdcall调用参数。在Delphi 1或Delphi 2环境下该调用参数是far。从Delphi 3以后将这个参数变为了stdcall,目的是为了使用标准的Win32参数传递技术来代替优化的register参数。忘记使用stdcall参数是常见的错误,这个错误不会影响DLL的编译和生成,但当调用这个DLL时会发生很严重的错误,导致操作系统的死锁。原因是register参数是Delphi的默认参数。   2.所写的函数和过程应该用exports语句声明为外部函数。 
      正如大家看到的,TestDll函数被声明为一个外部函数。这样做可以使该函数在外部就能看到,具体方法是单激鼠标右键用“快速查看(Quick View)”功能查看该DLL文件。(如果没有“快速查看”选项可以从Windows CD上安装。)TestDll函数会出现在Export Table栏中。另一个很充分的理由是,如果不这样声明,我们编写的函数将不能被调用,这是大家都不愿看到的。   3.当使用了长字符串类型的参数、变量时要引用ShareMem。 
      Delphi中的string类型很强大,我们知道普通的字符串长度最大为256个字符,但Delphi中string类型在默认情况下长度可以达到2G。(对,您没有看错,确实是两兆。)这时,如果您坚持要使用string类型的参数、变量甚至是记录信息时,就要引用ShareMem单元,而且必须是第一个引用的。既在uses语句后是第一个引用的单元。如下例: 
      uses 
      ShareMem, 
      SysUtils, 
      Classes;  还有一点,在您的工程文件(*.dpr)中而不是单元文件(*.pas)中也要做同样的工作,这一点Delphi自带的帮助文件没有说清楚,造成了很多误会。不这样做的话,您很有可能付出死机的代价。避免使用string类型的方法是将string类型的参数、变量等声明为Pchar或ShortString(如:s:string[10])类型。同样的问题会出现在当您使用了动态数组时,解决的方法同上所述。 
     
      

  2.   

    NEW->other->AcitveX->下面应该选Active Server Object,
    里面的方法可以通过
    View->Type Libary来添加
      

  3.   

    我自己比着做了一下,代码如下:{调用单元}
    unit uCallDll;interfaceuses
      Windows, Messages, SysUtils, Variants, 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 MyMax(X,Y:integer):integer;stdcall;external 'pDll.dll';implementation{$R *.dfm}
     procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(IntToStr(MyMax(30,50)));
    end;end.
    {DLL单元}
    library pDll;{ 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;  function MyMax ( X , Y : integer ) : integer ; stdcall ;
      begin
        if X > Y then
        Result := X
        else
        Result := Y ;
      end;{$R *.res}exports
      MyMax;
    beginend.一起学习:)