VC 中 import的使用

2021-04-25 03:03:51 字數 3644 閱讀 2739

#import 指令

1. c++中使用 com 簡單的方法是用 #import 匯入 type library

2. # import 指令將 com 產生對應的 c++類別,可以用接近於vbscript和visual basic的語句操作 com

3. 使用 #import 命令就可以將該檔案匯入到我們的程式碼中。type library 的內容將被轉換為描述了 com inte***ce 的 com smart pointer

4. 語法

#import  "filename" [attributes]

#import  [attributes]

attributes: 

用來通知編譯器修改 type library header 的內文。可以使用空白或逗號分隔 attribute。該選項為選擇性。如果 attribute 太多,可以使用 / 斷行分隔

a) auto_rename

b) auto_search

c) embedded_idl

d) exclude

e) high_method_prefix

f) high_property_prefixes

g) implementation_only

h) include()

i) inject_statement

j) named_guids

k) no_auto_exclude

l) no_dual_inte***ces

m) no_implementation no_namespace

n) no_search_namespace

o) no_smart_pointers

p) raw_dispinte***ces

q) raw_inte***ces_only

r) raw_method_prefix

s) raw_native_types

t) raw_property_prefixes

u) rename

v) rename_namespace

w) rename_search_namespace

x) tlbid 

filename:

你想要匯入的 type library,可以指定的值如下

a) type library (.tlb, .odl): #import "drawctl.tlb"

b) progid: #import "progid:my.prog.id.1.5"

可以額外指定地區id和版本號碼,如下:

#import "progid:my.prog.id" lcid("0") version("4.0)

假如沒有指定地區 id 會依循下列規則自動選擇

1) 假如只有乙個地區 id 就直接使用

2) 假如有多個地區 id,選擇版本編號的第一碼為 0, 9 或 499 的使用

3) 假如有多個地區 id,且版本編號的第一碼為 0, 9 或 499 有多個可以選擇,則選用最後乙個

4) 假如沒有指定版本號碼,則使用最近的版本號碼

c) type library id: #import "libid:12341234-1234-1234-1234-123412341234" version("4.0") lcid("9")

d) 乙個可執行檔(.exe)

e) 包含 type library 資訊的 dll

f) 內含 type library 的檔案

g) 任何可被 loadtypelib 接受的檔案

filename 如果為乙個實體檔案,會依循下面的規則進行搜尋

1) #import 中指定的路徑

2) 引用該檔案的程式所在路徑

3) path 環境變數

4) lib 環境變數

5) 編譯器選項 /i 指定的路徑

5. #import 產生的 header 檔

包含類似 midl(microsoft inte***ce definition language) 產生的主要 header 檔,但是包含額外的編譯器產生的程式碼和資料。該檔案和 type library 具有相同的名稱但附檔名為 .tlh。第二個 header 檔也具有和 type library 相同的檔名,但附檔名為 .tli。他內含編譯器產生的成員函式的實作,且該 header 被包含在主要 header 檔(.tlh)中。

假如併入(import)乙個包含 byref 引數的 dispinte***ce property, #import 將不會產生 __declspec (property) 敘述

這兩個 header 放置在  /fo 指定的目錄,

#import 在接到 type library 同時間會產生 header 檔。當 #import 處理完,編譯器會檢查檔案是否存在並且日期夠新。假如條件吻合則不重新建立。

#import 指令也可以放在預先編譯的 header 中,詳請參考

6. 主要的 header (.tlh)包含七個部分  

a) 固定的標頭:包含註解、#include "comdef.h"和其他安裝資訊

b) 向前參照和 typdef

c) 智慧型指標宣告:樣板類別 _com_ptr_t 屬於乙個智慧型指標,該類別封裝了介面指標並排除呼叫 addref, release, queryinte***ce 等繁瑣的步驟。另外也隱藏了 cocreateinstance 建立乙個新的 com 物件的呼叫。這個部分使用 _com_smartptr_typedef 來產生特異化(specialization)版本的 _com_ptr_t 類別。例如:

_com_smartptr_typedef(imyinte***ce, __uuidof(imyinte***ce));

編譯器會將上述的程式擴充套件成

typedef _com_ptr_t<_com_iiid> imyinte***ceptr;

d) typeinfo 宣告

e) 舊型 guide 定義:選擇性部分,包含命名過的 guid 常數 ,命名類似 clsid_coclass 和 iid_inte***ce,類似 midl 編譯器產生的資料

f) #include 第二個 header (*.tli)

g) 檔尾: #pragma pack(pop)

7. 使用 type library 可以用全域的解析或是明確的使用 namespace,如下

using namespace mylib;

該程式碼必須加在 #import 之後。

可以使用 no_namespace attribute 不需指定 namespace,不過可能發生名稱衝突。也可以使用 rename_namespace attribute 變更 namespace 名稱。

參考資料:

VC 中的Import指令的使用

import 指令 1.c 中使用 com 簡單的方法是用 import 匯入 type library 2.import 指令將 com 產生對應的 c 類別,可以用接近於vbscript和visual basic的語句操作 com 3.使用 import 命令就可以將該檔案匯入到我們的程式碼中。...

Python中import的使用

python中的import語句是用來匯入模組的,在python模組庫中有著大量的模組可供使用,要想使用這些檔案需要用import語句把指定模組匯入到當前程式中。import語句的作用 import語句作用就是用來匯入模組的,它可以出現在程式中的任何位置。import語句語法 使用import語句匯...

import與 import的使用

在xcode 5 下,為了更易於專案開發,增加了modules和 auto linking 這兩個新特性。用 import 來增加框架專案中比用 import會更有效.modules and auto linking 預設情況下是enabled的。如果是舊的專案,你可以通過設定 language m...