国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

基于C#的接口基礎教程之六(5)

2024-07-21 02:17:52
字體:
來源:轉載
供稿:網友
在 .net 框架程序中通過dllimport使用 win32 api
  
    .net 框架程序可以通過靜態 dll 入口點的方式來訪問本機代碼庫。dllimport 屬性用于指定包含外部方法的實現的dll 位置。
  
    dllimport 屬性定義如下:
  
  namespace system.runtime.interopservices
  {
   [attributeusage(attributetargets.method)]
   public class dllimportattribute: system.attribute
   {
    public dllimportattribute(string dllname) {...}
    public callingconvention callingconvention;
    public charset charset;
    public string entrypoint;
    public bool exactspelling;
    public bool preservesig;
    public bool setlasterror;
    public string value { get {...} }
   }
  }
  
    說明:
  
    1、dllimport只能放置在方法聲明上。
  
    2、dllimport具有單個定位參數:指定包含被導入方法的 dll 名稱的 dllname 參數。
  
    3、dllimport具有五個命名參數:
  
     a、callingconvention 參數指示入口點的調用約定。如果未指定 callingconvention,則使用默認值 callingconvention.winapi。
  
     b、charset 參數指示用在入口點中的字符集。如果未指定 charset,則使用默認值 charset.auto。
  
     c、entrypoint 參數給出 dll 中入口點的名稱。如果未指定 entrypoint,則使用方法本身的名稱。
  
     d、exactspelling 參數指示 entrypoint 是否必須與指示的入口點的拼寫完全匹配。如果未指定 exactspelling,則使用默認值 false。
  
     e、preservesig 參數指示方法的簽名應當被保留還是被轉換。當簽名被轉換時,它被轉換為一個具有 hresult 返回值和該返回值的一個名為 retval 的附加輸出參數的簽名。如果未指定 preservesig,則使用默認值 true。
  
     f、setlasterror 參數指示方法是否保留 win32"上一錯誤"。如果未指定 setlasterror,則使用默認值 false。
  
    4、它是一次性屬性類。
  
    5、此外,用 dllimport 屬性修飾的方法必須具有 extern 修飾符。
  
    下面是 c# 調用 win32 messagebox 函數的示例:
  
  using system;
  using system.runtime.interopservices;
  class mainapp
  { //通過dllimport引用user32.dll類。messagebox來自于user32.dll類
   [dllimport("user32.dll", entrypoint="messagebox")]
   public static extern int messagebox(int hwnd, string strmessage, string strcaption, uint uitype);
   public static void main()
   {
    messagebox( 0, "您好,這是 pinvoke!", ".net", 0 );
   }
  }
  
    面向對象的編程語言幾乎都用到了抽象類這一概念,抽象類為實現抽象事物提供了更大的靈活性。c#也不例外, c#通過覆蓋虛接口的技術深化了抽象類的應用。欲了解這方面的知識,請看下一節-覆蓋虛接口
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浏阳市| 江口县| 昌都县| 广灵县| 贵阳市| 明溪县| 柳州市| 东宁县| 长垣县| 遵义县| 长寿区| 嘉义市| 会同县| 加查县| 正安县| 遂昌县| 莱西市| 北安市| 湾仔区| 三原县| 靖安县| 通化市| 太保市| 达尔| 淳安县| 汾阳市| 乾安县| 孝昌县| 山阳县| 青海省| 万山特区| 屏东县| 百色市| 卫辉市| 东城区| 临高县| 余江县| 波密县| 南开区| 波密县| 禄劝|