我想要创建个Form1 窗体调用下面的DLL文件 我应该怎么 写啊 ?我想要创建个Form1 窗体调用下面的DLL文件 我应该怎么 写啊 ?
function HasChineseChar(Str:String):Boolean;
{.........................................
    Result:=0;}
function Proc_RequestString(pOrgReqString:pchar):integer;
{.........................................
  Result:=0;}
function Proc_TrainNumInq(strNumTrain:PChar):Integer;
{.........................................      
Result:=PChar(g_strResult);}//DLL主程序(数据查询)
function TrainInfo(RequestString:PChar;RequestRefNo:PChar):Pchar;stdcall;begin
  try
    CoInitialize(Nil);
    g_ADOQuery1:= TADOQuery.Create(nil);    try
      g_ADOQuery1.Connection:=g_ADOConnection1;
      g_ADOQuery1.ConnectionString:='Provider=SQLOLEDB.1;Password=jM2$hh8CRecK0);Persist Security Info=True;User ID=sa;password=sa;Initial Catalog=TrainInfo;Data Source=(local)';
      g_strReqRefNo:=RequestRefNo;
      g_strResult:='';      if  Proc_RequestString(RequestString)=1 then
        begin
        Result:=PChar(g_strResult);
        exit;
        end;      if g_nInqType=1 then
        begin
        if Proc_CityNameInq()=1 then
          begin
          Result:=PChar(g_strResult);
          exit;
          end;
        end;      if g_nInqType=2 then
        begin
        if Proc_TrainNumInq(RequestString)=1 then
          begin
          Result:=PChar(g_strResult);
          exit;
          end;
        end;      Result:=PChar(g_strResult);    except
      //showmessage('Oh!');
      Result:=PChar('对不起,系统发生异常,无法得到有效列车信息!');
    end;  finally
    g_ADOConnection1.Free;
    g_ADOQuery1.Free;
    CoUninitialize;
  end;end;
exports
  TrainInfo;
end.

解决方案 »

  1.   

    第一種方法﹐靜態調用﹕在Delphi中静态调用DLL
        调用一个DLL比写一个DLL要容易一些。首先给大家介绍的是静态调用方法,稍后将介绍动态调用方法,并就两种方法做一个比较。同样的,我们先举一个静态调用的例子。
    unit Unit1;
    interface
    uses
    Windows, Messages, SysUtils, Classes, Graphics,
    Controls, Forms, Dialogs, StdCtrls;
    type
    TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    var
    Form1: TForm1;
    implementation
    {$R *.DFM}
    //本行以下代码为我们真正动手写的代码
    function TestDll(i:integer):integer;stdcall;
    external 'Delphi.dll';
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    Edit1.Text:=IntToStr(TestDll(1));
    end;
    end.
    ///////////////////
    上面的例子中我们在窗体上放置了一个编辑框(Edit)和一个按钮(Button),并且书写了很少的代码来测试我们刚刚编写的Delphi.dll。大家可以看到我们唯一做的工作是将TestDll函数的说明部分放在了implementation中,并且用external语句指定了Delphi.dll的位置。(本例中调用程序和Delphi.dll在同一个目录中。)让人兴奋的是,我们自己编写的TestDll函数很快被Delphi认出来了。您可做这样一个实验:输入“TestDll(”,很快Delphi就会用fly-by提示条提示您应该输入的参数是什么,就像我们使用Delphi中定义的其他函数一样简单。注意事项有以下一些:
    一、调用参数用stdcall。
        和前面提到的一样,当引用DLL中的函数和过程时也要使用stdcall参数,原因和前面提到的一样。
    二、用external语句指定被调用的DLL文件的路径和名称。
        正如大家看到的,我们在external语句中指定了所要调用的DLL文件的名称。没有写路径是因为该DLL文件和调用它的主程序在同一目录下。如果该DLL文件在C:\,则我们可将上面的引用语句写为external 'C:\Delphi.dll'。注意文件的后缀.dll必须写上三、不能从DLL中调用全局变量。
    第二種方法﹐動態調用﹕舉例﹕动态调用DLL相对复杂很多,但非常灵活。为了全面的说明该问题,这次我们举一个调用由C++编写的DLL的例子。首先在C++中编译下面的DLL源程序。
    #include
    extern "C" _declspec(dllexport)
    int WINAPI TestC(int i)
    {
    return i;
    }
        编译后生成一个DLL文件,在这里我们称该文件为Cpp.dll,该DLL中只有一个返回整数类型的函数TestC。为了方便说明,我们仍然引用上面的调用程序,只是将原来的Button1Click过程中的语句用下面的代码替换掉了。
    procedure TForm1.Button1Click(Sender: TObject);
    type
    TIntFunc=function(i:integer):integer;stdcall;
    var
    Th:Thandle;
    Tf:TIntFunc;
    Tp:TFarProc;
    begin
    Th:=LoadLibrary('Cpp.dll'); {装载DLL}
    if Th>0 then
    try
    Tp:=GetProcAddress(Th,PChar('TestC'));
    if Tp<>nil
    then begin
    Tf:=TIntFunc(Tp);
    Edit1.Text:=IntToStr(Tf(1)); {调用TestC函数}
    end
    else
    ShowMessage('TestC函数没有找到');
    finally
    FreeLibrary(Th); {释放DLL}
    end
    else
    ShowMessage('Cpp.dll没有找到');
    end;
        大家已经看到了,这种动态调用技术很复杂,但只要修改参数,如修改LoadLibrary('Cpp.dll')中的DLL名称为'Delphi.dll'就可动态更改所调用的DLL。