• wince 基础技能 使用ini配置文件来保存信息


    今天和大家分享最近用到的一个基础技能:ini文件的使用

    最近的项目arm没有外接eeprom,运行过程中应用程序开开关关数据的保存是个问题

    所以用最常见的方式ini文件。

    最开始我把ini文件存在nand flash中,但是后来发现使用的nand的擦写次数仅有10000次

    于是就把ini放置在内存文件夹中

    下面具体展示下代码

    首先是 封装好的代码不是我写的,是网上收集的,这个算是写的不错的:

    RdWrtIni.h
     1 #ifndef __RDWRTINI_H__
     2 #define __RDWRTINI_H__
     3 
     4 #define DIM(a)  sizeof(a)/sizeof(a[0])
     5 
     6 //////////////////////////////////////////////////////////////////////////
     7 // 函数声明
     8 int  ReadIniFile(LPCWSTR lpFileName);
     9 DWORD GetLine(LPWSTR pLine, DWORD dwOffset, DWORD dwSize);
    10 BOOL IsComment(LPCWSTR pLine);
    11 BOOL IsSection(LPCWSTR pLine);
    12 BOOL IsSectionName(LPCWSTR pLine, LPCWSTR pSection);
    13 BOOL IsKey(LPCWSTR pLine , LPCWSTR pKeyName, LPWSTR* pValue, DWORD* dwValLen );
    14 void WriteValue(HANDLE m_hOutput , LPCWSTR pAppName, LPCWSTR pKeyName, LPCWSTR pString);
    15 DWORD GetString(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPWSTR lpReturnedString,DWORD nSize,LPCWSTR lpFileName );
    16 DWORD GetPrivateProfileString(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPCWSTR lpDefault, LPWSTR  lpReturnedString,DWORD   Size,LPCWSTR lpFileName );
    17 UINT GetPrivateProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName, INT nDefault,LPCTSTR lpFileName );
    18 BOOL WritePrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpString,LPCTSTR lpFileName);
    19 BOOL WINAPI WritePrivateProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName,INT Value,LPCTSTR lpFileName);
    20 
    21 #endif//__RDWRTINI_H__
    RdWrtIni.cpp
      1 /********************************************************************
      2 created: 2007/12/03
      3 filename:  RdWrtIni.cpp
      4 author:  Kaoya
      5 purpose: Read and Write Ini file
      6 *********************************************************************/
      7 #include "StdAfx.h"
      8 #include "RdWrtIni.h"
      9 #include <stdio.h> 
     10 
     11 //////////////////////////////////////////////////////////////////////////
     12 // 全局变量
     13 LPWSTR g_pData = NULL;    // 存储整个INI文件数据的缓冲区
     14 
     15 /************************************************************************
     16 ** 函数:GetString
     17 **功能:读INI文件
     18 **参数:
     19 lpAppName[in]         - 字段名
     20 lpKeyName[in]         - 键名
     21 lpReturnedString[out] - 键值
     22 nSize[in]             - 键值缓冲区大小(in characters )
     23 lpFileName[in]        - 完整的INI文件名
     24 **返 回:Returns the number of bytes read.
     25 **备注:
     26 ************************************************************************/
     27 DWORD GetString( LPCWSTR lpAppName,LPCWSTR lpKeyName,LPWSTR lpReturnedString, DWORD nSize,LPCWSTR lpFileName)
     28 {
     29     //WCHAR szFileName[MAX_PATH];
     30     DWORD dwSize , cchCopied;
     31     DWORD dwOffset = 0;
     32     TCHAR pLine[MAX_PATH] = {0} ;
     33     DWORD dwValLen = 0;
     34 
     35     if (!lpAppName || !lpFileName)
     36     return 0;
     37 
     38     //打开文件,将文件数据拷贝到缓冲区 g_pData中,返回INI文件大小
     39     if (0 == (dwSize = ReadIniFile(lpFileName)))
     40     {
     41         RETAILMSG(1, (_T("GetString, Could not ReadIniFile INI file: %s\n"), lpFileName));
     42         return 0;
     43     }
     44 
     45     cchCopied = 0;
     46     while ( 0 != (dwOffset = GetLine( pLine , dwOffset , dwSize )))
     47     {
     48         //RETAILMSG(1,(_T("%s\n"),szLine));
     49         // 是不是注释行
     50         if (IsComment(pLine))
     51         continue;
     52 
     53         // 是不是段名
     54         if (IsSection(pLine))
     55             {
     56             // 是不是我们要找的段名
     57             if (IsSectionName(pLine,lpAppName))
     58                 {
     59                 // 寻找我们要的键名
     60                 while ( 0 != (dwOffset = GetLine(pLine , dwOffset , dwSize)))
     61                     {
     62                     LPWSTR pValue=NULL;
     63 
     64                     if (IsSection(pLine))
     65                     break;
     66 
     67                     if (IsKey(pLine , lpKeyName, &pValue, &dwValLen))
     68                         {
     69                         cchCopied = min(dwValLen, nSize-1);
     70                         wcsncpy(lpReturnedString, pValue, cchCopied);
     71                         lpReturnedString[cchCopied] = 0;
     72                         // We're done.
     73                         break;      
     74                         }
     75                     }
     76                 break;
     77                 }
     78             }
     79     }
     80 
     81      return cchCopied;
     82 }
     83 
     84 /************************************************************************
     85 ** 函数:ReadIniFile
     86 **功能:打开文件,并将文件数据拷贝到一缓冲区g_pData中 
     87 **参数:
     88 lpFileName[in] - INI文件名,如果没有路径,默认路径为\\windows\\
     89 **返回:
     90 非0 - 该INI文件的大小(in bytes)
     91 0   - 失败
     92 **备注:
     93 1). 判断一个文本文件是不是UNICODE文件:读取文件前两个字节,如果是0XFF和0XFE
     94 则 为UNICODE文件,否则为ASCII文件(注意根据CPU是大端还是小端格式),这里默
     95 认为小端格式.如果是UNICODE文件,则丢去 前两个字节.
     96 2). 先将INI整个文件数据读到BYTE型缓冲区中,再转为WCHAR型,为什么?还没弄明白
     97 ************************************************************************/
     98 int ReadIniFile(LPCWSTR lpFileName)
     99 {
    100     int nReturn = 0;
    101     WCHAR szFileName[MAX_PATH] ={0} ;
    102     BY_HANDLE_FILE_INFORMATION fi = {0}; 
    103     HANDLE hfile;
    104     DWORD dwSize , dwIO;
    105     BOOL bUnicode = FALSE, bRet = FALSE; // bUnicode - 标志INI文件是不是Unicode文件
    106     BYTE* pData = NULL;
    107 
    108     if (!lpFileName)
    109         {
    110         nReturn = 0;
    111         goto exit;
    112         }
    113 
    114     if (wcschr(lpFileName, '\\'))
    115     wcscpy(szFileName, lpFileName);
    116     else
    117     wsprintf(szFileName, _T("\\My Documents\\%s"), lpFileName);
    118 
    119     // ReadIniFile the file.
    120     hfile = CreateFile(szFileName, GENERIC_READ,FILE_SHARE_READ,(LPSECURITY_ATTRIBUTES)0, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, (HANDLE)0);
    121 
    122     if (hfile == INVALID_HANDLE_VALUE)
    123     {
    124     RETAILMSG(1, (_T("ReadIniFile, CreateFile failed on INI file: %s, err %d\n"), szFileName, GetLastError()));
    125     nReturn = 0;
    126     goto exit;
    127     }
    128 
    129     // Get its size.
    130     if (!GetFileInformationByHandle(hfile, &fi))
    131     {
    132     CloseHandle(hfile);
    133     RETAILMSG(1, (_T("ReadIniFile, GetFileInformationByHandle failed on INI file: %s\n"), szFileName));
    134     nReturn = 0;
    135     goto exit;
    136     }
    137 
    138     dwSize = (fi.nFileSizeLow + 1) & ~0x1; //keep it word aligned
    139 
    140     // Now check if the file is unicode.
    141     dwIO = 0;
    142     if (sizeof(WORD) <= dwSize)
    143         {
    144         WORD wByteOrderMark = 0;
    145 
    146         // See comment at the bottom of the file.
    147         if ((ReadFile(hfile, &wByteOrderMark, sizeof(WORD), &dwIO, NULL)) && (dwIO == sizeof(WORD)) && (0xfeff == wByteOrderMark))
    148         {
    149         dwSize -= sizeof(WORD);
    150         bUnicode = TRUE;
    151         }
    152         else
    153         SetFilePointer(hfile, 0, NULL, FILE_BEGIN);
    154         }
    155 
    156     // Next, load the data.
    157     //RETAILMSG(1, (_T("ReadIniFile, the size of ini file [%s] is [%d]\n"), szFileName, dwSize)); 
    158     if (0 < dwSize)
    159         {
    160         pData = (BYTE *)malloc( dwSize + sizeof(WCHAR) );
    161         if (!pData)
    162             {
    163             RETAILMSG(1, (_T("ReadIniFile, no enough memory\n")));
    164             nReturn = 0;
    165             goto exit;
    166             }
    167         memset(pData,0,dwSize + sizeof(WCHAR));
    168         bRet = ReadFile(hfile, pData, dwSize, &dwIO, NULL);
    169         CloseHandle(hfile);
    170         if (!bRet)
    171         {
    172         RETAILMSG(1, (_T("ReadIniFile, ReadFile fail, err = [%d]\n"),GetLastError())); 
    173         nReturn = 0;
    174         goto exit;
    175         }
    176 
    177         // Create the buffer.
    178         if( g_pData )
    179         {
    180         free(g_pData);
    181         g_pData = NULL;
    182         }
    183 
    184         dwSize = MultiByteToWideChar( CP_ACP,0, (char *)pData ,-1,NULL,0);
    185         g_pData = (TCHAR *)malloc(dwSize*sizeof(WCHAR));
    186         
    187         if(!g_pData)
    188         {
    189         RETAILMSG(1, (_T("ReadIniFile, no enough momery\n")));
    190         nReturn = 0;
    191         goto exit;
    192         }
    193         MultiByteToWideChar(CP_ACP,0,(char *)pData,-1,g_pData,dwSize);
    194 
    195         dwSize--; // includes the NULL termination character
    196         nReturn = dwSize;
    197         }
    198 
    199     exit:
    200     if(pData)
    201     {
    202     free(pData);
    203     pData = NULL;
    204     }
    205     if(hfile)
    206     {
    207     CloseHandle(hfile);
    208     hfile = NULL;
    209     }
    210     return nReturn;
    211 }
    212 
    213 
    214 /************************************************************************
    215 ** 函数:GetLine
    216 **功能:获取在g_pData中从dwOffset位置开始的一行数据并保存到pLine,同时把偏移量dwOffset
    217 移 到下一行行首
    218 **参数:
    219 pLine[out]   - 接收一行数据(不包括\r\n)
    220 dwOffset[in] - 要读取的那一行的开始位置
    221 dwSize[in]   - INI文件大小
    222 **返回:
    223 正确 - 下一行行首的位置
    224 错 误 - 0
    225 **作者:XZP
    226 **日期:07.12.7
    227 **备注:
    228 /************************************************************************/
    229 DWORD GetLine(LPWSTR pLine, DWORD dwOffset, DWORD dwSize)
    230 {
    231     DWORD len = 0;
    232     DWORD len2 = 0;
    233     // Look for the end of the line.
    234     while ( dwOffset + len < dwSize&& '\r' != g_pData[dwOffset+len] && '\n' != g_pData[dwOffset+len])
    235     {
    236         if( g_pData[dwOffset+len]==0 )
    237         break;
    238         pLine[len] = g_pData[dwOffset+len] ;
    239         ++len;
    240     }
    241 
    242     pLine[len] = 0 ;
    243     // Now push the internal offset past the newline.
    244     // (We assume \r\n pairs are always in this order)
    245     if (dwOffset + len + len2 < dwSize && '\r' == g_pData[dwOffset+len+len2])
    246     ++len2;
    247     if (dwOffset + len + len2+1 < dwSize && '\n' == g_pData[dwOffset+len+len2])
    248     ++len2;
    249     if (2 >= len + len2 && (dwOffset +2 >=dwSize) )
    250     return 0;
    251 
    252     dwOffset += len + len2;
    253     return dwOffset;
    254 }
    255 
    256 /************************************************************************
    257 ** 函数:IsComment
    258 **功能:判断是不是注释行
    259 **参数:
    260 pLine[in] - INI的一行数据
    261 **返 回:
    262 1 - 注释行
    263 0 - 不是注释行
    264 **备注:
    265 1). 空行也视为注释行
    266 ************************************************************************/
    267 BOOL IsComment(LPCWSTR pLine) 
    268 {
    269     if (!pLine || 0 == wcslen(pLine) || ';' == *pLine)
    270     return TRUE;
    271     return FALSE;
    272 }
    273 
    274 /************************************************************************
    275 ** 函数:IsSection
    276 **功能:判断是不是段名
    277 **参数:
    278 pLine[in] - INI的一行数据
    279 **返回:
    280 1 - 是段名
    281 0 - 不是
    282 ************************************************************************/
    283 BOOL IsSection(LPCWSTR pLine)
    284 {
    285     if (pLine && '[' == *pLine)
    286     return TRUE;
    287     return FALSE;
    288 }
    289 
    290 /************************************************************************
    291 ** 函数:IsSectionName
    292 **功能:判断是INI文件的一行(pLine)是不是我们要找的段名(pSection)
    293 **参数:
    294 pLine[in]    - INI文件的一行数据
    295 pSection[in] - 要找的段名
    296 **返回:
    297 1 - 是
    298 0 - 不是
    299 ** 备注:
    300 ************************************************************************/
    301 BOOL IsSectionName(LPCWSTR pLine, LPCWSTR pSection)
    302 {
    303     if (IsSection(pLine))
    304     {
    305         DWORD len = wcslen(pSection);
    306         if (wcslen(pLine) - 2 == len && 0 == _wcsnicmp(pLine+1, pSection, len))
    307         return TRUE;
    308     }
    309     return FALSE;
    310 }
    311 
    312 /************************************************************************
    313 ** 函数:IsKey
    314 **功能:判断INI文件中一行的数据是不是我们要找的键名,如果是并读取键值
    315 **参数:
    316 pLine[in] - INI文件某行数据
    317 pKeyName[in] - 要寻找的键名
    318 pValue[out] - 键值
    319 dwValLen[out] - 键值pValue大小(in bytes)
    320 **返回:
    321 1 - 是,同时pValue返回键值
    322 0 - 不是,pValue为NULL
    323 **作者:XZP
    324 **日期:07.12.9
    325 **备注:
    326 ************************************************************************/
    327 BOOL IsKey(LPCWSTR pLine , LPCWSTR pKeyName, LPWSTR* pValue, DWORD* dwValLen )
    328 {
    329     LPCWSTR pEqual = NULL;
    330     DWORD length = 0, len = 0;
    331 
    332      if(!pLine || !pKeyName)
    333     return FALSE;
    334 
    335     // pLine是不是注释行
    336     if (IsComment( pLine ))
    337     return FALSE;
    338 
    339     // 寻找"="号
    340     pEqual = wcschr(pLine, L'=' );
    341     if (!pEqual)
    342     return FALSE;
    343 
    344      // 寻找键名最后一字符的位置
    345     while (pEqual - 1 >= pLine && iswspace(*(pEqual-1)))
    346     --pEqual;
    347     
    348     // Badly formed file.
    349     if (pEqual - 1 < pLine)
    350     return FALSE;
    351 
    352     // 键名长度
    353     length = pEqual - pLine;
    354 
    355     len = wcslen(pKeyName);
    356     //if (len == length && 0 == _wcsnicmp(pLine, pKeyName, len))
    357     if ( 0 == _wcsnicmp(pLine, pKeyName, len))
    358     {
    359         *pValue = (LPWSTR)wcschr(pLine, '=' );
    360         ++(*pValue);
    361         *dwValLen = wcslen(pLine) - ((*pValue) - pLine);
    362 
    363           // 去掉紧跟在"="号后的所有空格
    364         while (0 < *dwValLen && iswspace(**pValue))
    365         {
    366         ++(*pValue);
    367         --(*dwValLen);
    368         }
    369         while (0 < *dwValLen && iswspace((*pValue)[*dwValLen-1]))
    370         {
    371         --(*dwValLen);
    372         }
    373         // If the string is surrounded by quotes, remove them.
    374         if ('"' == (**pValue))
    375         {
    376         ++(*pValue);
    377         --(*dwValLen);
    378         if ('"' == (*pValue)[*dwValLen-1])
    379         {
    380         --(*dwValLen);
    381         }
    382         }
    383         return TRUE;
    384     }
    385     else
    386     {
    387     *pValue = NULL;
    388     return FALSE;
    389     }
    390 }
    391 
    392 
    393 /************************************************************************
    394 ** 函数:GetPrivateProfileString
    395 **功能:WCE下读取INI文件中某段名/键名的键值的字符串
    396 **参数:
    397 lpAppName[in]         - points to section name
    398 lpKeyName[in]         - points to key name
    399 lpDefault[in]         - points to default string
    400 lpReturnedString[out] - points to destination buffer
    401 nSize[in]             - size of destination buffer "lpReturnedString"(in characters)
    402 lpFileName[in]        - points to initialization filename 
    403 **返回:The return value is the number of characters copied to the buffer, 
    404 not including the terminating null character.   
    405 **作者:XZP
    406 **日期:07.12.11
    407 **备注:
    408 1). 如果INI文件没有你关心的数据,返回默认值lpDefault
    409 ************************************************************************/
    410 DWORD GetPrivateProfileString(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPCWSTR lpDefault, LPWSTR  lpReturnedString,DWORD Size,LPCWSTR lpFileName )
    411 {
    412     DWORD dwRc = 0, dwReturn = 0;
    413     if(!lpAppName || !lpKeyName || !lpReturnedString || !lpFileName || Size<=0 )
    414     return 0;
    415 
    416      dwRc = GetString(lpAppName,lpKeyName,lpReturnedString,Size,lpFileName);
    417     if(dwRc != 0)
    418     {
    419         dwReturn = dwRc;
    420     }
    421     else
    422     {
    423         if(lpDefault)
    424         {
    425         wcsncpy(lpReturnedString, lpDefault, Size);
    426         lpReturnedString[Size-1] = NULL;
    427         }
    428         else
    429         *lpReturnedString = 0;
    430         dwReturn = wcslen(lpReturnedString);
    431     }
    432 
    433      // 释放内存
    434     if (NULL != g_pData)
    435     {
    436         free( g_pData );
    437         g_pData = NULL;
    438     }
    439 
    440      return dwReturn;
    441 }
    442 
    443 /************************************************************************
    444 ** 函数:GetPrivateProfileInt
    445 **功能: retrieves an integer associated with a key in the
    446 specified section of the given initialization file
    447 ** 参数:
    448 LPCTSTR lpAppName,  // address of section name
    449 LPCTSTR lpKeyName,  // address of key name
    450 INT nDefault,       // return value if key name is not found
    451 LPCTSTR lpFileName  // address of initialization filename
    452 **返回:
    453 The return value is the integer equivalent of the string following 
    454 the specified key name in the specified initialization file. If the 
    455 key is not found, the return value is the specified default value. 
    456 If the value of the key is less than zero, the return value is zero. 
    457 **作者:XZP
    458 **日期:07.12.11
    459 ** 备注:
    460 ************************************************************************/
    461 UINT GetPrivateProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName, INT nDefault,LPCTSTR lpFileName )
    462 {
    463     WCHAR szRet[80] ={0};
    464 
    465      if(!lpAppName || !lpKeyName || !lpFileName )
    466     return 0;
    467 
    468     DWORD cch = GetString(lpAppName, lpKeyName, szRet, sizeof(szRet)/sizeof(WCHAR), lpFileName);
    469 
    470      // 释放内存
    471     if (NULL != g_pData)
    472     {
    473         free( g_pData );
    474         g_pData = NULL;
    475     }
    476 
    477      if (cch)
    478     return _wtoi(szRet);
    479     else
    480     return nDefault; 
    481 }
    482 
    483 /************************************************************************
    484 ** 函数:WriteLine
    485 **功能:向文件写入一行数据(包括回车换行符)
    486 **参数:
    487 hOutput[in] - 已打开的文件句柄
    488 pLine[in]   - 要写入的一行数据
    489 **返回:NONE
    490 **作者:XZP
    491 **日 期:08.1.2
    492 **备注:
    493 1). 写入一行数据,也包括写入行末的"\r\n"两个字符
    494 2). 注意区分是不是UNICODE版本的INI文件,如果不是,要将WCHAR转为char再写入INI文件
    495 3). 注意不要将结束符也写入文件
    496 ************************************************************************/
    497 void WriteLine(HANDLE hOutput , LPCWSTR pLine)
    498 {
    499     DWORD dwWrote = 0;
    500     WCHAR wBuffer[MAX_PATH] = {0};
    501     char buffer[MAX_PATH] ={0};
    502     DWORD dwlen = wcslen(pLine) ;
    503     BOOL bUnicode = FALSE ;
    504 
    505     if(bUnicode)
    506     {
    507         if (pLine)
    508         {
    509         WriteFile(hOutput, pLine, wcslen(pLine)*sizeof(WCHAR), &dwWrote, NULL);
    510         WriteFile(hOutput, L"\r\n", 2*sizeof(WCHAR), &dwWrote, NULL);
    511         }
    512     }
    513     else
    514     {
    515         if (pLine)
    516         {
    517         int bsize ;
    518         TCHAR szTempLine[MAX_PATH] = {0} ;
    519         wcsncpy( szTempLine, pLine, dwlen);
    520 
    521         bsize=WideCharToMultiByte(CP_ACP,0,szTempLine,-1,NULL,0,NULL,NULL);
    522         WideCharToMultiByte(CP_ACP,0,szTempLine,-1,buffer, bsize,NULL,NULL);
    523         buffer[bsize] = 0 ;
    524 
    525         WriteFile(hOutput, buffer, bsize-1 , &dwWrote, NULL); // 注意不要将结束符也写入
    526         WriteFile(hOutput, "\r\n", 2, &dwWrote, NULL);
    527         }
    528     }
    529 }
    530 
    531 /************************************************************************
    532 ** 函数:WritePrivateProfileString
    533 **功能:WCE环境下,向指定INI文件指定段名写入字符串数据
    534 **参数:
    535 lpAppName[in]
    536 Pointer to a null-terminated string containing section name. If
    537 the section does not exit, it is created.
    538 lpKeyName[in]
    539 Pointer to a null-terminated string containing key name. If the
    540 key does not exit in the specified section pointed to by the lpAppName 
    541 parameter, it's created. If this parameter is NULL, the ertire section, 
    542 including all keys within the section, is deleted. When deleting a 
    543 section, leave the comments intact.
    544 lpString[in]
    545 pointer to a null-terminated string to be written to the file. 
    546 If this parameter is NULL, the key pointed to by the lpKeyName 
    547 parameter is deleted.
    548 lpFileName[in]
    549 Pointer to a null-terminated string that names the initialization file. 
    550 **返回:
    551 FALSE - fail
    552 TRUE  - success
    553 **作者:XZP
    554 **日期:08.1.17
    555 **备注:
    556 1). 先将要修改的INI文件的全部数据读取到全局内存g_pData中
    557 2). 在g_pData中定位到我们要修改的位置,将其它数据和我们修改的数据写入一临时ini文件
    558 3). 最后将临时ini文件覆盖原来的ini文件,再删除临时ini文件
    559 4). 主要的API函数:
    560 CreateFile、 ReadFile、WriteFile、SetEndOfFile、CopyFile、DeleteFile
    561 5). 如果lpKeyName == NULL, 删除整个段, 如果lpString == NULL, 删除健
    562 ************************************************************************/
    563 BOOL WritePrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpString,LPCTSTR lpFileName)
    564 {
    565     DWORD dwSize, dwOffset;                // dwSize - ini文件大小, dwOffset - 偏移量
    566     BOOL  bReadLine = TRUE;
    567     BOOL  bWrote = FALSE;
    568     TCHAR pszLine[MAX_PATH] = {0};         // 存储一行的数据
    569     TCHAR pszIniFileTemp[MAX_PATH] = {0};  // 临时ini文件的名称(包括路径)
    570     HANDLE hOutputFile ;
    571     LPWSTR pValue;
    572     DWORD dwValLen;
    573     LPWSTR pName;
    574     RETAILMSG(1, (TEXT("WritePrivateProfileString  start!")));
    575     dwOffset = 0;
    576     if (!lpFileName)
    577     return FALSE;
    578 
    579      // 读取INI文件内容到全局变量g_pData内存中
    580     dwSize = ReadIniFile(lpFileName);
    581 
    582      //RETAILMSG(1, (TEXT("lpFileName=[%s], dwSize=[%d]"), lpFileName, dwSize));
    583 
    584     // Create the output file.
    585     //wcscpy(pszIniFileTemp, lpFileName);
    586     pName = (LPWSTR)_tcsrchr(lpFileName, L'\\');
    587     RETAILMSG(1, (TEXT("WritePrivateProfileString  2222!")));
    588     if(pName)
    589     {
    590         pName++;
    591         wsprintf(pszIniFileTemp,TEXT("\\My Documents\\%s.tmp"),pName);
    592     }
    593     else
    594     { 
    595         wsprintf(pszIniFileTemp, TEXT("\\My Documents\\%s.ini.tmp"),lpAppName);
    596     }
    597     RETAILMSG(1, (TEXT("pszIniFileTemp is %s !"),pszIniFileTemp));
    598      hOutputFile = CreateFile(pszIniFileTemp,GENERIC_WRITE,0,(LPSECURITY_ATTRIBUTES)0, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    599      
    600      RETAILMSG(1, (TEXT("WritePrivateProfileString  4444!")));
    601     if (INVALID_HANDLE_VALUE == hOutputFile)
    602     {
    603         RETAILMSG(1, (TEXT("Could not open output file: %s\n"), pszIniFileTemp));
    604         return FALSE;
    605     }
    606     RETAILMSG(1, (TEXT("WritePrivateProfileString  5555!")));
    607     // 将所有数据写入临时ini文件
    608 
    609     for (;;)
    610     {
    611         // The bReadLine flag is used to not read a new line after we break
    612         // out of the inner loop. We've already got a line to process.
    613         if (bReadLine)
    614         {
    615             dwOffset = GetLine( pszLine , dwOffset , dwSize );
    616             if (!dwOffset)
    617             break;
    618         }
    619         bReadLine = TRUE;
    620         // Skip past comments.
    621         if (IsComment(pszLine))
    622         {
    623             WriteLine(hOutputFile , pszLine);
    624             continue;
    625         }
    626         // Found a section name.
    627         if (IsSection(pszLine))
    628         {
    629             RETAILMSG(1, (TEXT("WritePrivateProfileString  6666!")));
    630             // It's the section we want.
    631             if (IsSectionName(pszLine , lpAppName))
    632             {
    633                 RETAILMSG(1, (TEXT("WritePrivateProfileString  7777!")));
    634                 // 以下如果lpKeyName为NULL,删除整个段
    635                 if (lpKeyName)
    636                 WriteLine(hOutputFile , pszLine);
    637 
    638                 // Process the whole section.
    639                 while (0 != (dwOffset = GetLine( pszLine , dwOffset , dwSize )))
    640                 {
    641                     
    642                     // Reached the end of the section.
    643                     if (IsSection(pszLine))
    644                     {
    645                         bReadLine = FALSE;
    646                         // This line will be written in the outer loop.
    647                         break;
    648                     }
    649                     // When deleting a section, leave the comments intact.
    650                     else if (IsComment(pszLine))
    651                     {
    652                         WriteLine(hOutputFile , pszLine);
    653                         continue;
    654                     }
    655                     // Got the value we want.
    656                     if (!bWrote && IsKey(pszLine , lpKeyName, &pValue, &dwValLen))
    657                     {
    658                         RETAILMSG(1, (TEXT("WritePrivateProfileString  8888!")));
    659                         bWrote = TRUE;
    660                         // 如果lpString为NULL,删除健lpKeyName
    661                         if(lpString)
    662                         WriteValue(hOutputFile , NULL, lpKeyName, lpString);
    663                     }
    664                     else
    665                     {
    666                         if (lpKeyName)
    667                         WriteLine(hOutputFile , pszLine);
    668                     }
    669 
    670                     if(dwOffset >= dwSize)
    671                     break ;
    672                 }
    673 
    674                 // 如果在段名lpAppName下键名lpKeyName不存在,则新建键名lpKeyName和键值lpString
    675                 if (!bWrote)
    676                 {
    677                     RETAILMSG(1, (TEXT("WritePrivateProfileString  9999!")));
    678                 bWrote = TRUE;
    679                 WriteValue(hOutputFile, NULL, lpKeyName, lpString);
    680                 }
    681             }
    682             else
    683             WriteLine(hOutputFile , pszLine);
    684         }
    685         else
    686         WriteLine(hOutputFile , pszLine);
    687 
    688         if(dwOffset ==0) 
    689         break;
    690     }
    691 
    692 
    693      // 如果指定的段名lpAppName不存在,则新建段名lpAppName及键名lpKeyName和键值lpString
    694     if (!bWrote && lpKeyName && lpString)
    695     {
    696          RETAILMSG(1, (TEXT("WritePrivateProfileString  aaaaaa!  \n")));
    697         WriteValue(hOutputFile , lpAppName, lpKeyName, lpString);
    698     }
    699 
    700      // 用临时ini文件覆盖原来的ini文件并删除临时ini文件
    701     if (INVALID_HANDLE_VALUE != hOutputFile)
    702     {
    703          RETAILMSG(1, (TEXT("WritePrivateProfileString bbbbbb!  \n")));
    704         SetEndOfFile(hOutputFile );
    705         CloseHandle(hOutputFile);
    706         RETAILMSG(1, (TEXT("WritePrivateProfileString  cccccc! %d \n"),CopyFile(pszIniFileTemp,lpFileName,0)));
    707         DeleteFile(pszIniFileTemp);
    708         return 1;
    709          
    710     }
    711 
    712     // 释放ReadIniFile函数的全局内存
    713     if (NULL != g_pData)
    714     {
    715         free( g_pData ) ;
    716         g_pData = NULL ;
    717     }
    718      RETAILMSG(1, (TEXT("WritePrivateProfileString  OK!")));
    719      return TRUE;
    720 }
    721 
    722 /************************************************************************
    723 ** 函数:WritePrivateProfileInt
    724 **功能:WCE环境下,向指定INI文件指定段名写入整型数据
    725 **参数:参考 WritePrivateProfileString函数
    726 **返回:
    727 FALSE - fail
    728 TRUE  - success
    729 **备注:
    730 ************************************************************************/
    731 BOOL WINAPI WritePrivateProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName,INT Value,LPCTSTR lpFileName)
    732 {   
    733     TCHAR ValBuf[16]={0};    
    734     wsprintf( ValBuf, TEXT( "%i" ), Value);    
    735     RETAILMSG(1, (TEXT("ValBuf is %i"),ValBuf));
    736     return( WritePrivateProfileString(lpAppName, lpKeyName, ValBuf, lpFileName) ); 
    737 }
    738 
    739 /************************************************************************
    740 ** 函数:WriteValue
    741 **功能:向指定INI文件中写入段名、键名和键值
    742 **参数:
    743 m_hOutput[in]
    744 pointer to the handle of ini file.
    745 pAppName[in]
    746 Pointer to a null-terminated string containing the name of the section
    747 in which data is written. If this parameter is NULL, the WriteValue
    748 function just wirte the pKeyName and pString.
    749 pKeyName[in]
    750 Pointer to a null-terminated string containing the name of the key in
    751 which data is writtern. 
    752 pString[in]
    753 Pointer to a null-terminated string to be written to the file. 
    754 **返回:NONE
    755 **作者:XZP
    756 **日 期:08.1.18
    757 **备注:
    758 1). 要成功写入INI文件,键名pKeyName和键值pString都不能为NULL。
    759 2). 如果段名pAppName为NULL,则只写入键名pKeyName和键值pString。
    760 3). 注意往INI文件写入字符串时,不要写入结束符。
    761 ************************************************************************/
    762 void WriteValue(HANDLE m_hOutput, LPCWSTR pAppName, LPCWSTR pKeyName, LPCWSTR pString)
    763 {
    764     char buffer[MAX_PATH] = {0} ;
    765     DWORD dwWrote;
    766     BOOL m_bUnicode = FALSE ;
    767 
    768     if (pKeyName && pString)
    769     {
    770         //RETAILMSG( 1 , (TEXT("pKeyName : %s , pString : %s"), pKeyName , pString) ) ;
    771 
    772         if (pAppName)  // 写入段名
    773         {
    774             if (m_bUnicode)
    775             {
    776                 WriteFile(m_hOutput, L"[", sizeof(WCHAR), &dwWrote, NULL);
    777                 WriteFile(m_hOutput, pAppName, wcslen(pAppName)*sizeof(WCHAR), &dwWrote, NULL);
    778                 WriteFile(m_hOutput, L"]\r\n", 3*sizeof(WCHAR), &dwWrote, NULL);
    779             }
    780             else
    781             {
    782                 int bsize , iRetLen;
    783                 TCHAR szTempLine[256] ={0} ;
    784 
    785                 wcscpy( szTempLine , TEXT("[") ) ;
    786                 wcscat( szTempLine , pAppName ) ;
    787                 wcscat( szTempLine , TEXT("]") ) ;
    788 
    789                 bsize=WideCharToMultiByte(CP_ACP,0,szTempLine,-1,NULL,0,NULL,NULL);
    790                 iRetLen = WideCharToMultiByte(CP_ACP,0,szTempLine,-1,buffer, bsize,NULL,NULL);
    791                 buffer[bsize] = 0 ;
    792 
    793                 WriteFile(m_hOutput, buffer , bsize-1 , &dwWrote, NULL);
    794                 WriteFile(m_hOutput, "\r\n", 2, &dwWrote, NULL);
    795 
    796             }
    797         }
    798 
    799         if (m_bUnicode)  // 写入健名和键值
    800         {
    801             WriteFile(m_hOutput, pKeyName, wcslen(pKeyName)*sizeof(WCHAR), &dwWrote, NULL);
    802             WriteFile(m_hOutput, L"=", sizeof(WCHAR), &dwWrote, NULL);
    803             WriteFile(m_hOutput, pString, wcslen(pString)*sizeof(WCHAR), &dwWrote, NULL);
    804             WriteFile(m_hOutput, L"\r\n", 2*sizeof(WCHAR), &dwWrote, NULL);
    805         }
    806         else
    807         {   
    808             int bsize , iRetLen;
    809             TCHAR szTempLine[256] ={0} ;
    810 
    811             wcscpy( szTempLine , pKeyName ) ;
    812             wcscat( szTempLine , TEXT("=") ) ;
    813             wcscat( szTempLine , pString )  ;
    814 
    815             bsize=WideCharToMultiByte(CP_ACP,0,szTempLine,-1,NULL,0,NULL,NULL);
    816             iRetLen = WideCharToMultiByte(CP_ACP,0,szTempLine,-1,buffer, bsize,NULL,NULL);
    817             buffer[bsize] = 0 ;
    818 
    819             WriteFile(m_hOutput, buffer, bsize-1 , &dwWrote, NULL);
    820             WriteFile(m_hOutput, "\r\n", 2, &dwWrote, NULL);
    821         }
    822     }
    823 }


     

    下面是我所使用的过程:首先是初始化时读:

      1 CString name[38];
      2     CString APPname1,APPname2;
      3     CString lpFileName;
      4     APPname1.Format(_T("radio"));
      5     lpFileName.Format(_T("\\My Documents\\MMC.ini"));
      6     name[0].Format(_T("m_first_run_flag"));
      7     name[1].Format(_T("m_current_freq"));
      8     name[2].Format(_T("m_current_band"));
      9     name[3].Format(_T("m_current_channel"));
     10     name[4].Format(_T("m_current_ST"));
     11     name[5].Format(_T("m_channel_freq0"));
     12     name[6].Format(_T("m_channel_freq1"));
     13     name[7].Format(_T("m_channel_freq2"));
     14     name[8].Format(_T("m_channel_freq3"));
     15     name[9].Format(_T("m_channel_freq4"));
     16     name[10].Format(_T("m_channel_freq5"));
     17     name[11].Format(_T("m_channel_freq6"));
     18     name[12].Format(_T("m_channel_freq7"));
     19     name[13].Format(_T("m_channel_freq8"));
     20     name[14].Format(_T("m_channel_freq9"));
     21     name[15].Format(_T("m_channel_freq10"));
     22     name[16].Format(_T("m_channel_freq11"));
     23     name[17].Format(_T("m_channel_freq12"));
     24     name[18].Format(_T("m_channel_freq13"));
     25     name[19].Format(_T("m_channel_freq14"));
     26     name[20].Format(_T("m_channel_freq15"));
     27     name[21].Format(_T("m_channel_freq16"));
     28     name[22].Format(_T("m_channel_freq17"));
     29     name[23].Format(_T("m_SW_LOC"));
     30     name[24].Format(_T("m_cur_PTY"));
     31     name[25].Format(_T("m_set_PTY"));
     32     name[26].Format(_T("m_manual_flag"));
     33     name[27].Format(_T("m_NO_PTY_flag"));
     34     name[28].Format(_T("m_SW_ST"));
     35     name[29].Format(_T("m_SW_TA"));
     36     name[30].Format(_T("m_SW_AF"));
     37     name[31].Format(_T("m_SW_EON"));
     38     name[32].Format(_T("m_SW_REG"));
     39     name[33].Format(_T("m_cur_TA"));
     40     name[34].Format(_T("m_cur_TP"));
     41     name[35].Format(_T("m_cur_EON"));
     42     name[36].Format(_T("m_flick_TA"));
     43     name[37].Format(_T("m_flick_AF"));
     44     CRadioData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname1,name[0],0,lpFileName ), CRadioData::DATA_CUR_STATE);
     45     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[1],0,lpFileName ), CRadioData::DATA_CUR_FREQ);
     46     CRadioData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname1,name[2],0,lpFileName ), CRadioData::DATA_CUR_BAND);
     47     CRadioData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname1,name[3],0,lpFileName ), CRadioData::DATA_CUR_CH);
     48     CRadioData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname1,name[4],0,lpFileName ), CRadioData::DATA_CUR_ST);
     49     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[5],0,lpFileName ), CRadioData::DATA_CH1);
     50     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[6],0,lpFileName ), CRadioData::DATA_CH2);
     51     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[7],0,lpFileName ), CRadioData::DATA_CH3);
     52     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[8],0,lpFileName ), CRadioData::DATA_CH4);
     53     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[9],0,lpFileName ), CRadioData::DATA_CH5);
     54     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[10],0,lpFileName ), CRadioData::DATA_CH6);
     55     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[11],0,lpFileName ), CRadioData::DATA_CH7);
     56     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[12],0,lpFileName ), CRadioData::DATA_CH8);
     57     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[13],0,lpFileName ), CRadioData::DATA_CH9);
     58     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[14],0,lpFileName ), CRadioData::DATA_CH10);
     59     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[15],0,lpFileName ), CRadioData::DATA_CH11);
     60     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[16],0,lpFileName ), CRadioData::DATA_CH12);
     61     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[17],0,lpFileName ), CRadioData::DATA_CH13);
     62     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[18],0,lpFileName ), CRadioData::DATA_CH14);
     63     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[19],0,lpFileName ), CRadioData::DATA_CH15);
     64     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[20],0,lpFileName ), CRadioData::DATA_CH16);
     65     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[21],0,lpFileName ), CRadioData::DATA_CH17);
     66     CRadioData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname1,name[22],0,lpFileName ), CRadioData::DATA_CH18);
     67     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[23],0,lpFileName ), CRadioData::DATA_SW_LOC);
     68     CRadioData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname1,name[24],0,lpFileName ), CRadioData::DATA_CUR_PTY);
     69     CRadioData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname1,name[25],0,lpFileName ), CRadioData::DATA_SET_PTY);
     70     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[26],0,lpFileName ), CRadioData::DATA_MANUAL_FLAG);
     71     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[27],0,lpFileName ), CRadioData::DATA_NO_PTY);
     72     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[28],0,lpFileName ), CRadioData::DATA_SW_ST);
     73     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[29],0,lpFileName ), CRadioData::DATA_SW_TA);
     74     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[30],0,lpFileName ), CRadioData::DATA_SW_AF);
     75     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[31],0,lpFileName ), CRadioData::DATA_SW_EON);
     76     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[32],0,lpFileName ), CRadioData::DATA_SW_REG);
     77     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[33],0,lpFileName ), CRadioData::DATA_CUR_TA);
     78     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[34],0,lpFileName ), CRadioData::DATA_CUR_TP);
     79     CRadioData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname1,name[35],0,lpFileName ), CRadioData::DATA_CUR_EON);
     80     CRadioData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname1,name[36],0,lpFileName ), CRadioData::DATA_FLICK_TA);
     81     CRadioData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname1,name[37],0,lpFileName ), CRadioData::DATA_FLICK_AF);
     82 
     83     APPname2.Format(_T("setup"));
     84     name[0].Format(_T("m_MCU_version"));
     85     name[1].Format(_T("m_disp_loud"));
     86     name[2].Format(_T("m_disp_eq"));
     87     name[3].Format(_T("m_disp_volume"));
     88     name[4].Format(_T("m_disp_mixvol"));
     89     name[5].Format(_T("m_disp_balance"));
     90     name[6].Format(_T("m_disp_fader"));
     91     name[7].Format(_T("m_disp_bass"));
     92     name[8].Format(_T("m_disp_middle"));
     93     name[9].Format(_T("m_disp_treble"));
     94     name[10].Format(_T("m_disp_subwoof"));
     95     name[11].Format(_T("m_mcu_time"));
     96     name[12].Format(_T("m_disp_mixact"));
     97     name[13].Format(_T("m_disp_loudc"));
     98     name[14].Format(_T("m_disp_bassc"));
     99     name[15].Format(_T("m_disp_middlec"));
    100     name[16].Format(_T("m_disp_treblec"));
    101     name[17].Format(_T("m_disp_bassq"));
    102     name[18].Format(_T("m_disp_middleq"));
    103     name[19].Format(_T("m_contrast"));
    104     name[20].Format(_T("m_brightness"));
    105     name[21].Format(_T("m_color"));
    106 
    107 
    108     CCommonData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname2,name[1],0,lpFileName ), CCommonData::DATA_DISP_LOUD);
    109     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[2],0,lpFileName ), CCommonData::DATA_DISP_EQ);
    110     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[3],0,lpFileName ), CCommonData::DATA_DISP_VOLUME);
    111     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[4],0,lpFileName ), CCommonData::DATA_DISP_MIXVOL);
    112     CCommonData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname2,name[5],0,lpFileName ), CCommonData::DATA_DISP_BALANCE);
    113     CCommonData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname2,name[6],0,lpFileName ), CCommonData::DATA_DISP_FADER);
    114     CCommonData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname2,name[7],0,lpFileName ), CCommonData::DATA_DISP_BASS);
    115     CCommonData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname2,name[8],0,lpFileName ), CCommonData::DATA_DISP_MIDDLE);
    116     CCommonData::getInstance()->set_unchar_data(GetPrivateProfileInt(APPname2,name[9],0,lpFileName ), CCommonData::DATA_DISP_TREBLE);
    117     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[10],0,lpFileName ), CCommonData::DATA_DISP_SUBWOOF);
    118     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[11],0,lpFileName ), CCommonData::DATA_MCU_TIME);
    119     CCommonData::getInstance()->set_bool_data(GetPrivateProfileInt(APPname2,name[12],0,lpFileName ), CCommonData::DATA_DISP_MIXACT);
    120     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[13],0,lpFileName ), CCommonData::DATA_DISP_LOUDC);
    121     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[14],0,lpFileName ), CCommonData::DATA_DISP_BASSC);
    122     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[15],0,lpFileName ), CCommonData::DATA_DISP_MIDC);
    123     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[16],0,lpFileName ), CCommonData::DATA_DISP_TREBLEC);
    124     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[17],0,lpFileName ), CCommonData::DATA_DISP_BASSQ);
    125     CCommonData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[18],0,lpFileName ), CCommonData::DATA_DISP_MIDDLEQ);
    126     CSetupData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[19],0,lpFileName ), CSetupData::DATA_VIDEO_CONTRAST);
    127     CSetupData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[20],0,lpFileName ), CSetupData::DATA_VIDEO_BRIGHTNESS);
    128     CSetupData::getInstance()->set_unint_data(GetPrivateProfileInt(APPname2,name[21],0,lpFileName ), CSetupData::DATA_VIDEO_COLOR);
    129     char *buff;
    130     GetPrivateProfileString(APPname2,name[0],0, (LPWSTR)buff,20,lpFileName);
    131     CCommonData::getInstance()->set_Cstring_data((LPWSTR)buff, CCommonData::DATA_MCU_VERSION);

    其次是反初始化的写:

    HANDLE hOutputFile ;
        CString pszIniFileTemp;
        pszIniFileTemp="\\My Documents\\MMC.ini.tmp";
        CString lpFileName;
        lpFileName.Format(_T("\\My Documents\\MMC.ini"));
        hOutputFile = CreateFile(pszIniFileTemp,GENERIC_WRITE,0,(LPSECURITY_ATTRIBUTES)0, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
        CString APPname;
        APPname.Format(_T("radio"));
        CString name[38];
        CString keyval[38];
        int val[38];
        name[0].Format(_T("m_first_run_flag"));
        name[1].Format(_T("m_current_freq"));
        name[2].Format(_T("m_current_band"));
        name[3].Format(_T("m_current_channel"));
        name[4].Format(_T("m_current_ST"));
        name[5].Format(_T("m_channel_freq0"));
        name[6].Format(_T("m_channel_freq1"));
        name[7].Format(_T("m_channel_freq2"));
        name[8].Format(_T("m_channel_freq3"));
        name[9].Format(_T("m_channel_freq4"));
        name[10].Format(_T("m_channel_freq5"));
        name[11].Format(_T("m_channel_freq6"));
        name[12].Format(_T("m_channel_freq7"));
        name[13].Format(_T("m_channel_freq8"));
        name[14].Format(_T("m_channel_freq9"));
        name[15].Format(_T("m_channel_freq10"));
        name[16].Format(_T("m_channel_freq11"));
        name[17].Format(_T("m_channel_freq12"));
        name[18].Format(_T("m_channel_freq13"));
        name[19].Format(_T("m_channel_freq14"));
        name[20].Format(_T("m_channel_freq15"));
        name[21].Format(_T("m_channel_freq16"));
        name[22].Format(_T("m_channel_freq17"));
        name[23].Format(_T("m_SW_LOC"));
        name[24].Format(_T("m_cur_PTY"));
        name[25].Format(_T("m_set_PTY"));
        name[26].Format(_T("m_manual_flag"));
        name[27].Format(_T("m_NO_PTY_flag"));
        name[28].Format(_T("m_SW_ST"));
        name[29].Format(_T("m_SW_TA"));
        name[30].Format(_T("m_SW_AF"));
        name[31].Format(_T("m_SW_EON"));
        name[32].Format(_T("m_SW_REG"));
        name[33].Format(_T("m_cur_TA"));
        name[34].Format(_T("m_cur_TP"));
        name[35].Format(_T("m_cur_EON"));
        name[36].Format(_T("m_flick_TA"));
        name[37].Format(_T("m_flick_AF"));
        val[0]=CRadioData::getInstance()->get_unchar_data( CRadioData::DATA_CUR_STATE);
        val[1]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CUR_FREQ);
        val[2]=CRadioData::getInstance()->get_unchar_data(CRadioData::DATA_CUR_BAND);
        val[3]=CRadioData::getInstance()->get_unchar_data(CRadioData::DATA_CUR_CH);
        val[4]=CRadioData::getInstance()->get_unchar_data(CRadioData::DATA_CUR_ST);
        val[5]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH1);
        val[6]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH2);
        val[7]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH3);
        val[8]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH4);
        val[9]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH5);
        val[10]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH6);
        val[11]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH7);
        val[12]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH8);
        val[13]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH9);
        val[14]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH10);
        val[15]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH11);
        val[16]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH12);
        val[17]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH13);
        val[18]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH14);
        val[19]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH15);
        val[20]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH16);
        val[21]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH17);
        val[22]=CRadioData::getInstance()->get_unint_data(CRadioData::DATA_CH18);
        val[23]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_SW_LOC);
        val[24]=CRadioData::getInstance()->get_unchar_data(CRadioData::DATA_CUR_PTY);
        val[25]=CRadioData::getInstance()->get_unchar_data(CRadioData::DATA_SET_PTY);
        val[26]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_MANUAL_FLAG);
        val[27]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_NO_PTY);
        val[28]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_SW_ST);
        val[29]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_SW_TA);
        val[30]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_SW_AF);
        val[31]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_SW_EON);
        val[32]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_SW_REG);
        val[33]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_CUR_TA);
        val[34]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_CUR_TP);
        val[35]=CRadioData::getInstance()->get_bool_data(CRadioData::DATA_CUR_EON);
        val[36]=CRadioData::getInstance()->get_unchar_data(CRadioData::DATA_FLICK_TA);
        val[37]=CRadioData::getInstance()->get_unchar_data(CRadioData::DATA_FLICK_AF);
        int i;
        for(i=0;i<38;i++)
        {
        keyval[i].Format(_T("%d"),val[i]);
        }
        
        WriteValue(hOutputFile,APPname,name[0],keyval[0]);
        for(i=1;i<38;i++)
        {
        WriteValue(hOutputFile,0,name[i],keyval[i]);
        }
    
        APPname.Format(_T("setup"));
        name[0].Format(_T("m_MCU_version"));
        name[1].Format(_T("m_disp_loud"));
        name[2].Format(_T("m_disp_eq"));
        name[3].Format(_T("m_disp_volume"));
        name[4].Format(_T("m_disp_mixvol"));
        name[5].Format(_T("m_disp_balance"));
        name[6].Format(_T("m_disp_fader"));
        name[7].Format(_T("m_disp_bass"));
        name[8].Format(_T("m_disp_middle"));
        name[9].Format(_T("m_disp_treble"));
        name[10].Format(_T("m_disp_subwoof"));
        name[11].Format(_T("m_mcu_time"));
        name[12].Format(_T("m_disp_mixact"));
        name[13].Format(_T("m_disp_loudc"));
        name[14].Format(_T("m_disp_bassc"));
        name[15].Format(_T("m_disp_middlec"));
        name[16].Format(_T("m_disp_treblec"));
        name[17].Format(_T("m_disp_bassq"));
        name[18].Format(_T("m_disp_middleq"));
        name[19].Format(_T("m_contrast"));
        name[20].Format(_T("m_brightness"));
        name[21].Format(_T("m_color"));
    
    
    
    
    
        val[1]=CCommonData::getInstance()->get_bool_data(CCommonData::DATA_DISP_LOUD);
        val[2]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_EQ);
        val[3]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_VOLUME);
        val[4]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_MIXVOL);
        val[5]=CCommonData::getInstance()->get_unchar_data(CCommonData::DATA_DISP_BALANCE);
        val[6]=CCommonData::getInstance()->get_unchar_data(CCommonData::DATA_DISP_FADER);
        val[7]=CCommonData::getInstance()->get_unchar_data(CCommonData::DATA_DISP_BASS);
        val[8]=CCommonData::getInstance()->get_unchar_data(CCommonData::DATA_DISP_MIDDLE);
        val[9]=CCommonData::getInstance()->get_unchar_data(CCommonData::DATA_DISP_TREBLE);
        val[10]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_SUBWOOF);
        val[11]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_MCU_TIME);
        val[12]=CCommonData::getInstance()->get_bool_data(CCommonData::DATA_DISP_MIXACT);
        val[13]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_LOUDC);
        val[14]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_BASSC);
        val[15]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_MIDC);
        val[16]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_TREBLEC);
        val[17]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_BASSQ);
        val[18]=CCommonData::getInstance()->get_unint_data(CCommonData::DATA_DISP_MIDDLEQ);
        val[19]=CSetupData::getInstance()->get_unint_data(CSetupData::DATA_VIDEO_CONTRAST);
        val[20]=CSetupData::getInstance()->get_unint_data(CSetupData::DATA_VIDEO_BRIGHTNESS);
        val[21]=CSetupData::getInstance()->get_unint_data(CSetupData::DATA_VIDEO_COLOR);
    
    
        keyval[0].Format(_T("%s"),CCommonData::getInstance()->get_Cstring_data(CCommonData::DATA_MCU_VERSION));
        for(i=1;i<22;i++)
        {
        keyval[i].Format(_T("%d"),val[i]);
        }
    
    
        WriteValue(hOutputFile,APPname,name[0],keyval[0]);
        for(i=1;i<22;i++)
        {
        WriteValue(hOutputFile,0,name[i],keyval[i]);
        }
        
        SetEndOfFile(hOutputFile );
        CloseHandle(hOutputFile);
        RETAILMSG(1, (TEXT("WritePrivateProfileString  cccccc! %d \n"),CopyFile(pszIniFileTemp,lpFileName,0)));
        DeleteFile(pszIniFileTemp);

     需要注意的是,这样的保存并不适用与断电,因为是在内存文件夹

    所以断电的话,我会把所有数据发送到MCU的eeprom,上电首先MCU会发给arm初始化信息

    呵呵!算是给和我一样的新人一个提醒,不要随便擦写,特别是nand flash

    文中并么有详细讲解代码,大家有何指教或疑问留言,我会一一回复的!共同进步!!!

  • 相关阅读:
    Java学习--泛型
    java学习--自定义类的实例的大小比较和排序
    Java学习--枚举
    java学习--修饰符
    Java学习--变量
    POI--各种样式的XSSFCellStyle的生成
    POI 使用颜色字符串生成XSSFColor对象
    Others # 看到的一些創意 / 知乎不錯的提問/ Android安全
    RO # 不错站点
    Linux # Virtual Box VBoxGuestAdditions.iso
  • 原文地址:https://www.cnblogs.com/lishiqi/p/2628414.html
Copyright © 2020-2023  润新知