Titan專欄

          用文字來整理生命

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            44 隨筆 :: 49 文章 :: 19 評論 :: 0 Trackbacks
          使用C#時不免用調用別的DLL,如WIN32的API和自己以前做的DLL,
          C#調用DLL很像VB,下面討論的C#調用DLL的方式。
          看看下面的例子,演示了怎么定義DLL函數接口
          public class Utility
          {
             [DllImport("kernel32",
          EntryPoint=”CreateDirectory”,
          CallingConvention=CallingConvention.StdCall]
             public static extern bool Create (string name);
            
             [DllImport("User32"]
          EntryPoint=”MessageBox”,
          CallingConvention=CallingConvention.StdCall]
             public static extern int MsgBox (string msg);
          }
            
          class MyClass
          {
             public static int Main()
             {
                string myString;
                Console.Write("Enter your message: ");
                myString = Console.ReadLine();
                return Utility.MsgBox(myString);
             }
          }
            
          值得注意的是,缺省的調用規則(CallingConvention)是Stdcall,同Winapi,在
          C++里是__stdcall的形式,函數入口(EntryPoint)缺省是同名,如CreateDirectory
          的定義也可以為
             [DllImport("kernel32")]
             static extern bool CreateDirectory(string name, SecurityAttributes sa);
            
          WIN32 API原型為
          BOOL CreateDirectory(
            LPCTSTR lpPathName,                         // directory name
            LPSECURITY_ATTRIBUTES lpSecurityAttributes  // SD
          );
            
          在調用WIN32 API時注意那些類型的轉換,如結構(struct)、指針(pointer),

          有關各種語言之間類型轉換和DllImport屬性的詳細信息可以參考SDK文檔 
          posted on 2006-02-11 22:22 Titan 閱讀(304) 評論(0)  編輯  收藏 所屬分類: C#技術

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 阳东县| 海盐县| 涪陵区| 广汉市| 镇康县| 商南县| 朝阳市| 腾冲县| 原平市| 宝清县| 扬中市| 陆丰市| 平远县| 布拖县| 什邡市| 称多县| 清水河县| 沂水县| 平顶山市| 德保县| 仙游县| 灵寿县| 武隆县| 成武县| 观塘区| 育儿| 湘潭市| 静安区| 前郭尔| 苍山县| 华宁县| 涞水县| 梧州市| 延长县| 绥中县| 通榆县| 沙洋县| 纳雍县| 民和| 江永县| 贵阳市|