2016-08-07 16 views
0

我有一個包含名爲「Container.Pas」的子單元的「Utilities.DPR」庫。在「Utilities.DPR」庫中有一些功能。我可以使用在Container.pas中定義和聲明的函數,並調用它的Library.dpr單元。但是我也想使用庫單元中的一些功能。如何在其單元中使用庫函數

library Utilities; 

{ 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 
    System.SysUtils, System.Classes, windows, Winapi.Messages, System.Variants, Vcl.Graphics, Vcl.Controls, 
    Vcl.Forms, Vcl.Dialogs,System.NetEncoding, Vcl.StdCtrls, 
    DCPcrypt2, DCPblockciphers, DCPblowfish, DCPsha256, IdGlobal, 
    Types, Soap.EncdDecd, IdCoder,IdCoderMIME, LbCipher, Winsock, 
    DateUtils, 
    container in 'container.pas' {frmContainer}; 

{$R *.res} 
var 
    s,n,Temp:widestring; 
    length_:integer; 

function bitshifter(Const TestStr:WideString):Boolean; 
begin 
     .... 
     .... 

end; 

unit container; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Def, 
    LbClass,DateUtils; 

type 
    TfrmContainer = class(TForm) 
    FDSQLiteFunc: TFDSQLiteFunction; 
    SQLiteConn: TSQLConnection; 
    FDSQLiteRTree: TFDSQLiteRTree; 
    FDSQLiteBkp: TFDSQLiteBackup; 
    SQLQuery: TSQLQuery; 
    FDSQLiteValidate1: TFDSQLiteValidate; 
    FDLocalSQL1: TFDLocalSQL; 
    FDConn: TFDConnection; 
    FDQuery1: TFDQuery; 
    FDSQLiteSec: TFDSQLiteSecurity; 
    FDGUIxWaitCursor1: TFDGUIxWaitCursor; 
    FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink; 

    procedure FDSQLiteValidate1Progress(ASender: TFDPhysDriverService; 
     const AMessage: string); 
    procedure FormCreate(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

function CheckSQLiteDLL:boolean; 
function CheckAllowed(const s: string): boolean; 

var 
    frmContainer: TfrmContainer; 



implementation 

procedure TfrmContainer.FormCreate(Sender: TObject); 
begin 
     //Connection should be created once only) 
     bitshifter('Hello'); 
     .... 
end; 
+0

歡迎來到StackOverflow!請更具體地說明你的問題。例如。如果你需要訪問'bitshifter',你必須'輸出'它,然後通過聲明爲'external'來導入 –

回答

0

在Delphi中,dpr文件基本上只是一種特殊類型的單元。 dpr和其他單位之間的一個區別是,您不能從其他單位使用它。

要注意的另一件事是兩個單位不能在他們的接口部分使用彼此。這將創建一個循環單位參考,這是不允許的。

所以,把你的情況爲例:

公用事業DPR:

  • 器具bitshifter功能
  • 用途集裝箱單元

集裝箱PAS:

製成
  • 這需要使用bitshifter功能

對於bitshifter函數A的形式提供給其他單元它必須首先被移動到單元本身移動到單元,其中它被使用。

即您可以將其移動到集裝箱單位。但將通用實用功能移到一個實現UI非常特定組件的單元(因爲在這種情況下,容器是一個表單元)並不是一個好主意。

這裏更有意義的是創建一個專門用於這個和任何相關例程的新單元。在這種情況下,我們可能會創建一個名爲BitUtilities.pas的單位。

如果DPR包含使用函數,則需要DPR同時使用現有集裝箱單元以及新BitUtilities單位代碼。你可能希望讓DPR使用這個單元,即使它不是直接調用其中的函數,因爲讓DPR知道項目中使用的所有單元可以幫助確保某些IDE工具按預期工作(例如ctrl +點擊導航功能等)。

否則,集裝箱單元只需要使用BitUtilities

公用事業DPR:

  • 用途BitUtilities單元(可選,除非的DPR還包含代碼調用bitshifter函數本身)
  • 用途容器單元

BitUtilities PAS:

  • 實現bitshifter功能(以及任何其它類似的部分/相關的 「位」 功能)

容器PAS:

  • 使用BitUtilities單元
0

單位不能引用在項目文件中定義的符號。對於貨櫃致電bitshifter,您需要將該功能移至單位。它可能是集裝箱,或者它可能是一些其他單位。

相關問題