代码改变世界

今日学习笔记:c# extern 关键字 和 kernel32.dll中的api:WritePrivateProfileString,GetPrivateProfileString

2010-12-08 14:10  于为源  阅读(3917)  评论(0编辑  收藏  举报

关于Extern修饰符的用法网上其实很多了,这里我来老生常谈一下,以便加深印象。

extern 主要用于声明在外部实现的方法,什么叫外部实用的方法呢,一般说来就是用System.Runtime.InteropServices服务的DllImport方法引入非托管代码程序集。例如调用系统API,C语言写的方法等等。在这种情况下,声明必须为static

同时,extern 关键字还可以定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。

下面是一个改写自MSDN上的简单的例子,调用系统winmm.DLL播放wav文件:

//系统API的调用的声明
        [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        public static extern void PlaySound(string path,System.IntPtr hMod,PlaySoundFlags flags);

//调用该方法

    string path = @"c:\111.wav";
            try
            {
                PlaySound(path, new System.IntPtr(), PlaySoundFlags.SND_SYNC);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

 

C#读写ini文件的类,调用kernel32.dll中的api:WritePrivateProfileString,GetPrivateProfileString

using  System.Runtime.InteropServices;

using  System.Text; 

namespace  INIFile

{

     ///  <summary>

     ///  读写ini文件的类

     ///  调用kernel32.dll中的两个apiWritePrivateProfileStringGetPrivateProfileString来实现对ini  文件的读写。

     ///

     ///  INI文件是文本文件,

     ///  由若干节(section)组成,

     ///  在每个带括号的标题下面,

     ///  是若干个关键词(key)及其对应的值(value)

     /// 

  ///[Section]

  ///Key=value

     ///

     ///  </summary>

     public  class  IniFile

 

     {

         ///  <summary>

         ///  ini文件名称(带路径)

         ///  </summary>

         public  string  filePath; 

 

         //声明读写INI文件的API函数

         [DllImport("kernel32")] 

         private  static  extern  long  WritePrivateProfileString(string  section,string  key,string  val,string  filePath);

 

         [DllImport("kernel32")]

         private  static  extern  int  GetPrivateProfileString(string  section,string  key,string  def,StringBuilder  retVal,int  size,string  filePath);

 

         ///  <summary>

         ///  类的构造函数

         ///  </summary>

         ///  <param  name="INIPath">INI文件名</param> 

         public  IniFile(string  INIPath)   

         { 

              filePath  =  INIPath;

 

         }

 

         ///  <summary>

         ///   INI文件

         ///  </summary>

         ///  <param  name="Section">Section</param>

         ///  <param  name="Key">Key</param>

         ///  <param  name="value">value</param>

         public  void  WriteInivalue(string  Section,string  Key,string  value)   

         {     

              WritePrivateProfileString(Section,Key,value,this.filePath);

 

         }

 

         ///  <summary>

         ///    读取INI文件指定部分

         ///  </summary>

         ///  <param  name="Section">Section</param>

         ///  <param  name="Key">Key</param>

         ///  <returns>String</returns> 

         public  string  ReadInivalue(string  Section,string  Key) 

         {   

              StringBuilder  temp  =  new  StringBuilder(255);

              int  i  =  GetPrivateProfileString(Section,Key,"",temp,255,this.filePath); 

              return  temp.ToString();

 

         } 

     } 

}