ini文件访问工具类 WinCfgUtil

来源:互联网 发布:淘宝6s官换机靠谱吗 编辑:程序博客网 时间:2024/04/30 06:45

1、 WinCfgUtil.h

#ifndef__WINCFG_UTIL_H#define__WINCFG_UTIL_H#include <Windows.h>DWORD INI_Create(LPCTSTR lpINIFilePath, LPCTSTR lpSectionName, DWORD dwSectionLen){DWORD dwRet = -1;HANDLE hIniFile = ::CreateFile(lpINIFilePath, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (INVALID_HANDLE_VALUE == hIniFile){return dwRet;}//write the sectionDWORD dwWriten = 0;BOOL boRet = FALSE;boRet = ::WriteFile(hIniFile, lpSectionName, dwSectionLen, &dwWriten, NULL);if (dwSectionLen == dwWriten){dwRet = 0;}CloseHandle(hIniFile);return dwRet;}DWORD INI_AppendPair(LPCTSTR lpINIFilePath, LPCTSTR lpFieldName, DWORD dwFieldNameLen, LPTSTR lpValue, DWORD dwValueLen ){DWORD dwRet = -1;HANDLE hFile = ::CreateFile(lpINIFilePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (INVALID_HANDLE_VALUE != hFile){::SetFilePointer(hFile, 0, NULL, FILE_END);TCHAR szLine[MAX_PATH] = {0};lstrcpy(szLine, "\r\n");lstrcat(szLine, lpFieldName);LPTSTR lpEq = "=";lstrcat(szLine, lpEq);lstrcat(szLine, lpValue);DWORD dwWritten = 0;BOOL boRet = ::WriteFile(hFile, szLine, lstrlen(szLine), &dwWritten, NULL);if (boRet){CloseHandle(hFile);dwRet = 0;}}return dwRet;}DWORD INI_GetValue(LPCTSTR lpINIFilePath, LPCTSTR lpSectionName,    LPCTSTR lpFieldName, LPTSTR lpValue, DWORD dwValueLen){DWORD dwLen = -1;dwLen = ::GetPrivateProfileString(lpSectionName,lpFieldName,  NULL,lpValue,  dwValueLen,lpINIFilePath);return dwLen;}//lpINIFilePath 取相对或绝对路径,不能直接输入当前的文件名DWORD INI_SetValue(LPCTSTR lpINIFilePath, LPCTSTR lpSectionName,    LPCTSTR lpFieldName, LPCTSTR lpNewValue){DWORD dwRet = -1;BOOL boRet = FALSE;boRet = ::WritePrivateProfileString(lpSectionName,lpFieldName,lpNewValue,lpINIFilePath);if (boRet){dwRet = 0;}return dwRet;}#endif


0 0
原创粉丝点击