Reference "kernel32" to read and write ini configuration files

Reference "kernel32" to read and write ini configuration files

Reference "kernel32" to read and write ini configuration files

OverView

kernel32.dll is a very important 32-bit dynamic link library file in Windows, which belongs to the kernel-level file. It controls the system's memory management, data input and output operations and interrupt processing. When Windows starts, kernel32.dll resides in a specific write-protected area in the memory, so that other programs cannot occupy this memory area.
Compare standardshader and toonshader:
![standardshader][1]![toonshader][2]

Precautions:

  • ini file path must be complete
  • The ini file path must use \, because in VC++, \\ only means \
  • You can put ini in the directory where the program is located, at this time the IpFileName parameter is ".\FileName.ini"

read

GetPrivateProfileInt

Use this method to get ini type data, if not, it will take the default data set

UINT WINAPI GetPrivateProfileInt
(
_In_LPCTSTR lpAppName, //ini文件中区块名称
_In_LPCTSTR lpKeyName, //键名
_In_INT nDefault, //默认值
_In_LPCTSTR lpFileName //ini文件路径
);

GetPrivateProfileString

Use this method to get string type data, if not, it will take the default data set

UINT WINAPI GetPrivateProfileString
(
_In_LPCTSTR lpAppName, //ini文件中区块名称
_In_LPCTSTR lpKeyName, //键名
_In_INT nDefault, //默认值
_In_LPSTR lpReturnedString,//接受ini文件中值的CString对象,指定一个字符串缓冲区,长度至少为nSize
_In_DWORD nSize,//指定装载到IpReturnedString缓冲区的最大字符数
_In_LPCTSTR lpFileName //ini文件路径
);

Write

Writes values ​​to ini, so just writing strings is enough

WritePrivateProfileString

BOOL WritePrivateProfileString(
LPCTSTR lpAppName,//ini文件中区块名
LPCTSTR lpKeyName,//键名
LPCTSTR lpString,//键值
LPCTSTR lpFileName
);

Simple to use in unity

Singleton

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

/// <summary>
/// ws:配置文件读写类,这就很nice。配置文件必须放在StreamingAssets文件夹下
/// </summary>
public class ConfigUtilty : Singleton<ConfigUtilty> {

    // StringBuilder temp = new StringBuilder(1024);
    //int i = GetPrivateProfileString("system", "StrServeIP", "", temp, 500, g_ConfigIniFile);
    //SetProfileString("Flag","Flag","10","MainConfig.ini");

    //系统动态库
    [DllImport("kernel32")]
    private static extern void WritePrivateProfileString(string section, string key, string def,string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

     private string ConfigPath;//配置文件所在位置
     private StringBuilder retVal;
     void Awake()
     {
         ConfigPath = Application.streamingAssetsPath;
         retVal = new StringBuilder();

    }

    
    public void SetProfileString(string section,string key,string def,string filename)
    {
        WritePrivateProfileString(section,key,def,ConfigPath+"\\"+filename);
    }

    public int GetProfileInt(string section,string key,int def,string filename)
    {
        return GetPrivateProfileInt(section, key, def, this.ConfigPath+"\\"+filename);
    }

    public string GetProfileString(string section,string key,string def,int size,string filename)
    {
        int i=GetPrivateProfileString(section, key,def,retVal,size,ConfigPath + "\\" + filename);
        return i == 0 ? "" : retVal.ToString();
    }


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325692293&siteId=291194637