Titan專欄

          用文字來(lái)整理生命

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            44 隨筆 :: 49 文章 :: 19 評(píng)論 :: 0 Trackbacks
          使用C#時(shí)不免用調(diào)用別的DLL,如WIN32的API和自己以前做的DLL,
          C#調(diào)用DLL很像VB,下面討論的C#調(diào)用DLL的方式。
          看看下面的例子,演示了怎么定義DLL函數(shù)接口
          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);
             }
          }
            
          值得注意的是,缺省的調(diào)用規(guī)則(CallingConvention)是Stdcall,同Winapi,在
          C++里是__stdcall的形式,函數(shù)入口(EntryPoint)缺省是同名,如CreateDirectory
          的定義也可以為
             [DllImport("kernel32")]
             static extern bool CreateDirectory(string name, SecurityAttributes sa);
            
          WIN32 API原型為
          BOOL CreateDirectory(
            LPCTSTR lpPathName,                         // directory name
            LPSECURITY_ATTRIBUTES lpSecurityAttributes  // SD
          );
            
          在調(diào)用WIN32 API時(shí)注意那些類型的轉(zhuǎn)換,如結(jié)構(gòu)(struct)、指針(pointer),

          有關(guān)各種語(yǔ)言之間類型轉(zhuǎn)換和DllImport屬性的詳細(xì)信息可以參考SDK文檔 
          posted on 2006-02-11 22:22 Titan 閱讀(304) 評(píng)論(0)  編輯  收藏 所屬分類: C#技術(shù)

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 白玉县| 镇远县| 三门峡市| 淮滨县| 江源县| 醴陵市| 襄汾县| 许昌县| 额尔古纳市| 扬州市| 资源县| 秦皇岛市| 绥宁县| 灯塔市| 庆安县| 清丰县| 延川县| 隆昌县| 化州市| 蓬溪县| 大足县| 诏安县| 新余市| 交口县| 博乐市| 哈巴河县| 昭通市| 罗定市| 黎城县| 沽源县| 蓝山县| 浮梁县| 高雄县| 定边县| 农安县| 德钦县| 扬中市| 南康市| 汕尾市| 新营市| 勃利县|