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#技術

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


          網站導航:
           
          主站蜘蛛池模板: 浦江县| 武穴市| 屏山县| 贞丰县| 馆陶县| 浠水县| 玉门市| 木里| 台南县| 太原市| 上虞市| 绥滨县| 长泰县| 铜陵市| 综艺| 博湖县| 南江县| 武平县| 湾仔区| 岳池县| 正蓝旗| 西和县| 微山县| 杂多县| 松阳县| 凤翔县| 濉溪县| 正安县| 敦化市| 美姑县| 北京市| 大田县| 通山县| 班玛县| 古交市| 右玉县| 开鲁县| 洮南市| 辉南县| 汾西县| 紫云|