GetPrivateProfileString for CE
在 Pocket PC 上加载 ini 文件需要不可用的 GetPrivateProfileString 函数。
引言
这个函数原本应该在 PocketPC / CE 平台上存在,但由于 ini 文件被认为已经过时,所以没有包含。然而,在移植 C++ 应用程序时,你经常会遇到这个问题。
声明:此代码绝不处理 GetPrivateProfileString
的所有功能,也不是没有 bug 或经过充分测试的。它可能会毁灭宇宙等等。目前它仅读取基本的 ini 设置,这正是我需要的。我还在此基础上添加了封装,处理了许多应该在这里实现的功能。
请参阅 GetPrivateProfileString
以获取更多详细信息。
我希望其他人能够完善它,最终使其成为 Win32 的等效版本。以下是代码:
static DWORD GetPrivateProfileString(LPCTSTR lpSection, LPCTSTR lpKey,
LPTSTR lpDefault, LPTSTR lpBuffer, DWORD dwBufSize, LPCTSTR szPathAndFile)
{
fstream clFileOp(szPathAndFile,ios::in); // File
string clline; // line buffer
CStringA clkey( lpKey); // key in ansii
if (clFileOp.is_open() == FALSE )
{
return 1;
}
while(!clFileOp.eof())
{
getline ( clFileOp, clline );
if ( clline.find( clkey ) != -1 )
{
int nEquals = clline.find( "=" );
if ( nEquals == -1 )
{
continue;
}
string clres = clline.substr( nEquals +1, clline.length() - nEquals );
CString clret ( clres.c_str() );
_tcscpy ( lpBuffer,clret );
clFileOp.close();
return 1;
}
}
clFileOp.close();
return 0;
}