Search This Blog

Showing posts with label INI Files. Show all posts
Showing posts with label INI Files. Show all posts

Monday, September 7, 2009

To Read all section and keys in an Ini File



void ReadSectionsAndKeys(CString csPath)
{
char lpszReturnBuffer[MAX_PATH];
char* pNextSection = NULL;
GetPrivateProfileSectionNames(lpszReturnBuffer,MAX_PATH,csPath);
pNextSection = lpszReturnBuffer;
//TRACE("Section: %s\n", pNextSection);
//printf("Section: %s\n", pNextSection);
// for keys
int nPos=-1;
char lpszKeyNames[8192];
DWORD dSize;
dSize = sizeof(lpszKeyNames);
CString csNameandValue(""), csKey(""),csValue("");
//

while (*pNextSection != 0x00)
{
GetPrivateProfileSection( pNextSection, lpszKeyNames , dSize , csPath );
char *pKeyName = lpszKeyNames;

while (*pKeyName != 0x00)
{
nPos = -1;
if(*pKeyName != 0x00)
{
//TRACE("Keys: %s\n", pKeyName);
csNameandValue = pKeyName;
if((nPos = csNameandValue.Find("=")) > -1)
{
csKey = csNameandValue.Left(nPos);
csValue=csNameandValue.Mid(nPos+1);
}
}
pKeyName = pKeyName + strlen(pKeyName) + 1;
}
pNextSection = pNextSection + strlen(pNextSection) + 1;
if(*pNextSection != 0x00)
{
//TRACE("Section: %s\n", pNextSection);
}
}
}

Thursday, April 16, 2009

Read and write .ini files



void ReadwriteINI()
{
     CString csIniPath,csKey;
     int nPos;

     char lpszKeyNames[4096];
     DWORD dSize;
     dSize = sizeof(lpszKeyNames);
     csIniPath="settings.ini";
     GetPrivateProfileSection( "IPADDRESS" , lpszKeyNames , dSize , csIniPath );
     char *pKeyName = lpszKeyNames;    
     CString csIP(""), csMyKey(""),csSubnet,csProxyIP,csGateway ,csPort;
    
     while (*pKeyName != 0x00)
     {
          nPos = -1;
          if(*pKeyName != 0x00)
          {
               csKey = pKeyName;
               if((nPos = csKey.Find("=")) > -1)
               {    
                    csMyKey = csKey.Left(nPos);
                    if( csMyKey.Find("IPADDR") > - 1 )
                    {
                         csIP= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("SUBNET") > - 1 )
                    {
                         csSubnet= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("GATEWAY") > - 1 )
                    {
                         csGateway= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("PROXYIP") > - 1 )
                    {
                         csProxyIP= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("PORT") > - 1 )
                    {
                         csPort= csKey.Mid(nPos+1);
                    }
               }
          }
          pKeyName = pKeyName + strlen(pKeyName) + 1;
     }


     if(csIP.IsEmpty())
     {
          csIP="192.168.0.2;
          WritePrivateProfileString("IPADDRESS","IPADDR",csIP,csIniPath);
     }
     if(csSubnet.IsEmpty())
     {
          csSubnet="255.255.255.0";
          WritePrivateProfileString("IPADDRESS","SUBNET ",csSubnet,csIniPath);
     }
     if(csGateway.IsEmpty())
     {
          csGateway="192.168.0.3";
          WritePrivateProfileString("IPADDRESS","GATEWAY",csGateway,csIniPath);
     }
     if(csProxyIP.IsEmpty())
     {
          csProxyIP="198.168.0.1";
          WritePrivateProfileString("IPADDRESS","PROXYIP",csProxyIP,csIniPath);
     }
     if(csPort.IsEmpty())
     {
          csPort="3128";
          WritePrivateProfileString("IPADDRESS","PORT",csPort,csIniPath);
     }

    

}

INI file content:
[IPADDRESS] IPADDR= SUBNET= GATEWAY= PROXYIP= PORT=