Delphi中如何编写Dll,请高手指点迷津,最好能附上例子,谢谢啦

解决方案 »

  1.   

    这个找一两本经典的书看看相关章节就会有所了解的。
    比如:delphi5开发指南 等等
      

  2.   

    以下是我写的一个简单Dll,共2个文件
    文件Assistant.dpr
    内容:
    library Assistant;{ 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,
      FileIO in 'FileIO.pas';{$R *.res}exports
      ASTModulePath,
      ASTFileSize,
      ASTUserCount,
      ASTGetUser,
      ASTAddNewUser,
      ASTHasTheUser;
    beginend.文件:FileIO.pas
    内容:
    library Assistant;{ 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,
      FileIO in 'FileIO.pas';{$R *.res}exports
      ASTModulePath,
      ASTFileSize,
      ASTUserCount,
      ASTGetUser,
      ASTAddNewUser,
      ASTHasTheUser;
    beginend.你Compile一下就会有个Assistant.dll了!
      

  3.   

    啊,哦,FileIO.pas文件内容贴错了,应该如下:
    unit FileIO;interface
    uses
      Windows, SysUtils;function ASTModulePath: Pchar; stdcall;
    function ASTUserCount: Integer; stdcall;
    function ASTFileSize(FileName: PChar): cardinal; stdcall;
    function ASTGetUser(index: Integer; Buffer: PChar; Count: Integer): Integer; stdcall;
    function ASTAddNewUser(UserName: Pchar): Boolean; stdcall;
    function ASTHasTheUser(UserName: Pchar): Boolean; stdcall;
    implementationfunction ASTModulePath: Pchar; stdcall;
    var
      PS: array[0..255] of Char;
    begin
      if GetModuleFileName(HInstance, @PS[0], 256) = 0 then
        Result := ''
      else
        Result := PS;
    end;function ASTFileSize(FileName: PChar): cardinal; stdcall;
    var
      FHandle:THandle;
    begin
      FHandle := CreateFile(FileName, 0, FILE_SHARE_READ, nil, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
      Result := GetFileSize(FHandle, nil);
      CloseHandle(FHandle);
    end;function ASTUserCount: Integer; stdcall;
    var
      APathFile: string;
    begin
      APathFile := ExtractFilePath(ASTModulePath);
      Result := -1;
      if Trim(APathFile) <> '' then
      begin
        APathFile := APathFile + 'UserList.Dat';
        if FileExists(APathFile) then
          Result := ASTFileSize(PChar(APathFile)) div 20;
      end;
    end;function ASTGetUser(index: Integer; Buffer: PChar; Count: Integer): Integer; stdcall;
    var
      UserCount: Integer;
      F: File;
    begin
      UserCount := ASTUserCount;
      Result := 0;
      if (index > 0) and (index <= UserCount) then
      begin
        AssignFile(F, ExtractFilePath(ASTModulePath) + 'UserList.Dat');
        Reset(F, 1);
        Seek(F, (index - 1) * 20);
        BlockRead(F, Buffer^, 20);
        CloseFile(F);
        Result := 1;
      end;
    end;function ASTAddNewUser(UserName: Pchar): Boolean; stdcall;
    var
      AName: Pchar;
      UserCount, i: Integer;
      APathFile : string;
      F: File;
      NameBuffer : array[0..19] of char;
    begin
      UserCount := ASTUserCount;
      APathFile := ExtractFilePath(ASTModulePath);
      if (APathFile <> '') then
      begin
        APathFile := APathFile + 'UserList.Dat';
        AssignFile(F, APathFile);
        if FileExists(APathFile) then
        begin
          Reset(F, 1);
          GetMem(AName, 255);
          for i := 1 to UserCount do
          begin
            BlockRead(F, AName^, 20);
            if StrComp(AName, UserName) = 0 then
            begin
              Result := True;
              FreeMem(AName, 255);
              exit;
            end;
          end;
          Seek(F, FileSize(F));
          FreeMem(AName, 255);
        end
        else
          Rewrite(F, 1);    StrCopy(NameBuffer, UserName);
        BlockWrite(F, NameBuffer, 20);
        Result := True;
        CloseFile(F);
      end
      else
        Result := False;
    end;function ASTHasTheUser(UserName: Pchar): Boolean; stdcall;
    var
      i, UserCount: Integer;
      APathFile : string;
      F: File;
      AName: PChar;
    begin
      UserCount := ASTUserCount;
      APathFile := ExtractFilePath(ASTModulePath);
      Result := False;
      if (APathFile <> '') then
      begin
        APathFile := APathFile + 'UserList.Dat';
        AssignFile(F, APathFile);
        if FileExists(APathFile) then
        begin
          Reset(F, 1);
          GetMem(AName, 255);
          for i := 1 to UserCount do
          begin
            BlockRead(F, AName^, 20);
            if StrComp(AName, UserName) = 0 then
            begin
              Result := True;
              break;
            end;
          end;
          FreeMem(AName, 255);
        end;
      end;
    end;end.
      

  4.   

    使用向导创建,Export中包含需要导出的函数即可,注意Dll中的接口不要使用String类型,要改为PCHar