zoukankan      html  css  js  c++  java
  • 今日学习笔记:c# extern 关键字 和 kernel32.dll中的api:WritePrivateProfileString,GetPrivateProfileString

    关于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();

     

             } 

         } 

    }

    作者:于为源 出处: http://www.cnblogs.com/yuanyuan/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    第十六周博客总结
    第十五周博客总结
    自学第六次博客(动作事件的处理)
    第十四周博客总结
    自学的第五篇博客
    自学电脑游戏第四天(Swing)
    c++面向对象程序设计第四章课后习题
    SQL注入
    VirtualBox+Vagrant环境配置
    测试
  • 原文地址:https://www.cnblogs.com/yuanyuan/p/1900191.html
Copyright © 2011-2022 走看看