文章編號: 143258 - 上次校閱: 2004年7月1日 - 版次: 1.3

如何建立常數和 DLL 宣告型別程式庫中

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

它可以是非常有用封裝常數定義和 DLL 宣告型別程式庫中。Visual Basic 可讓您存取型別程式庫和它們的內容。一旦進行型別程式庫的參考可以在 Visual Basic 的自己物件瀏覽器中檢視其資訊。型別程式庫也是上級,因為您也可以提供說明字串連同永遠讓使用者知道什麼定義宣告是針對。

其他相關資訊

若要建立型別程式庫,您必須在命令列程式 UUID.EXE 和 MKTYPLIB.EXE。這些程式會提供使用 Visual C++。

假設我們想要建立型別程式庫有三個整數常數、 字串常數和一個 Windows API 宣告。以下是產生型別程式庫所需的物件描述語言 (ODL) 程式碼。 透過 [ODL 讀取和那里將會解釋每個區段註解及原因我們需要。
   [
   // The Universal Unique identifier (UUID), needs to be generated by
   // using uuid.exe program. Then, you paste the value into the type
   // library.
   uuid(006da100-110f-11cf-83b2-00aa0068851c),

   // The Help String comes up in the object browser as the second
   // piece of the library description in the "Libraries/Projects" combo
   // box.
   helpstring("Constants TypeLib"),

   // The Locale Identifier (LCID), identifies the language the type
   // library applies to.
   lcid(0x9),

   // This is the version number of the type library.
   version(1.0)
   ]

   // This is the name of the type library. It comes up in the object
   // browser as the first piece of the library description in the
   // "Libraries/Projects" combo box.
   library MyLib
   {
     // Define numeric constants.
     typedef enum tagConst
        {
          mylibConst1,       // Make the constant mylibconst1 equal to 0.
          mylibConst2=5,     // Make the constant mylibconst2 equal to 5.
          mylibConst3=7      // Make the constant mylibconst3 equal to 7.
        }Constants;

      // You need to define string constants in a module.
      // Modules also need to reference a dll name. In this case you don't
      // need to, so just give it a bogus name.
      [dllname("bogus")]
      module MoreConstants
       {
         // Define a constant mystr and assign it the value StringConstant.
         const LPSTR mystr="StringConstant";
       };

      // Now call a function located in the Windows API, specifically
      // User32.dll. Now you see why you need a dll name here, this is
      // where the API function will come from.
      [dllname("user32.dll")]
      module APIDeclare
       {
         // Give the API function a descriptive help line, this will
         // be seen in the Object browser. Then, you can declare the
         // function. The entry attribute specifies the identifier for the
         // entry point into the dll.
         // The in attribute specifies a parameter as a value going into
         // the function.
         //Note that the following two lines need to be all on one line.
   [helpstring("Test API function Declaration"), entry("CloseWindow")]_
    boolean

   CloseWindow([in] long Winhndl);
       };
   }
				

編譯此程式碼]、 將它複製到文字檔案],並呼叫 test.odl,然後執行命令列程式 MkTypLib,如下所示]:
   mktyplib /nocpp test.odl
				

會出現下列訊息:
   Successfully generated type library 'test.tlb'.
				

要使用從 Visual Basic 型別程式庫

  1. 從 Visual Basic 開始新的專案。
  2. 從 [工具] 功能表,選取 [參考]。這將會出現 [參考] 對話方塊 ; 按一下瀏覽] 按鈕以搜尋 Test.tlb 檔案。 一旦找到該檔案按一下 [確定]。
  3. 型別程式庫現在將會在參考清單中,確定它已選取然後結束 [參考] 對話方塊。
  4. 在 form1 上繪製命令按鈕。
  5. 請輸入下列程式碼:
          Private Sub Command1_Click()
    
          Dim x As Boolean
    
          'Call the Windows API function CloseWindow without making one
          'declare at all. Note that the CloseWindow API will close the current
          'window.
          x = CloseWindow(Me.hWnd)
    
          'Now print out the value of the constants in the type library
          'using message boxes.
          MsgBox mylibConst1
          MsgBox mylibConst2
          MsgBox mylibConst3
          MsgBox mystr
    
          End Sub
    						
  6. 執行程式 ; 您會看到 form1 最小化,然後您會看到訊息方塊的每個我們在我們的型別程式庫中定義的常數,並每個常數的值。

?考

OLE 2 程式師參考磁碟區 2

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
關鍵字:?
kbmt kbhowto KB143258 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:143258? (http://support.microsoft.com/kb/143258/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。