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 閱讀(298) 評論(0)  編輯  收藏 所屬分類: C#技術

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


          網站導航:
           
          主站蜘蛛池模板: 穆棱市| 礼泉县| 合作市| 宁波市| 元氏县| 林芝县| 泰兴市| 美姑县| 邢台县| 宁城县| 宜阳县| 吴川市| 囊谦县| 德阳市| 广水市| 赤水市| 增城市| 墨脱县| 日照市| 舞阳县| 长春市| 奇台县| 崇阳县| 吉木乃县| 西林县| 阿拉善左旗| 漠河县| 大英县| 宁城县| 遂宁市| 屏边| 安乡县| 左贡县| 兴和县| 北海市| 瑞丽市| 关岭| 峨山| 和硕县| 澳门| 体育|