类定义
// ini.h: interface for the Cini class. #if !defined(AFX_INI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_) #define AFX_INI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 //#include <afxwin.h> class __declspec(dllexport)Cini { public: static DWORD ReadString (char *section, char * key, char* stringtoread , char * filename); static BOOL WriteString(LPCTSTR section, LPCTSTR key,char* stringtoadd, char *filename); Cini(); virtual ~Cini(); }; #endif // !defined(AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)
类实现
// ini.cpp: implementation of the Cini class. #include "stdafx.h" #include "ini.h" //#include "vld.h" ///////////////////////////////////////////////////////////////////////// //Cini类的构造函数和析构函数 Cini::Cini() { } Cini::~Cini() { } ///////////////////////////////////////////////////////////////////////// //写字符串到INI文件,GetLastError()函数用于返回写入失败 void error(LPSTR lpszFunction) { CHAR szBuf [80] ; DWORD dw = GetLastError(); sprintf_s(szBuf, 80, "%s failed: GetLastError returned %u\n",lpszFunction, dw); MessageBox(NULL, szBuf, "Error", MB_OK); ExitProcess(dw); } BOOL Cini::WriteString(LPCTSTR section, LPCTSTR key, char *stringtoadd, char *filename) { CHAR FilePath[255]; memset(FilePath, 0 ,255); HMODULE hModule = ::GetModuleHandle(NULL); if (hModule != NULL) { ::GetModuleFileName(hModule,FilePath,255); //GetProccessImageFileName(PROCESS_QUERY_INFORMATION ,FilePath,255); (strrchr(FilePath,'\\'))[1] = 0; strcat(FilePath,filename); } return ::WritePrivateProfileString(section,key,stringtoadd,FilePath); } ///////////////////////////////////////////////////////////////////////// //从INI文件中读取字符串 DWORD Cini::ReadString(char *section, char * key, char* stringtoread , char * filename) { CHAR FilePath[255]; memset(FilePath, 0 ,255); HMODULE hModule = ::GetModuleHandle(NULL); if (hModule != NULL) { ::GetModuleFileName(hModule,FilePath,255); //GetProccessImageFileName(PROCESS_QUERY_INFORMATION ,FilePath,255); (strrchr(FilePath,'\\'))[1] = 0; strcat(FilePath,filename); } return ::GetPrivateProfileString(section, key,NULL,stringtoread,255,FilePath); }