1

I am working on a piece of code in the old application, using C++ in Visual Studio 2008. It attempts to read some values from an INI file, and I'm trying to do some error handling in the case of the section or key I am trying to read does not exist in the file.

Here is the code snippet:

int nValue = GetPrivateProfileInt (SECTION, KEY, -1, sINIFile);
if(nValue == -1)  // default value
{
    int nLastError = GetLastError();
    if(nLastError != 0)
    {
        // do something
    }
}

sINIFile contains the full path to my required INI file.

After some testing, I've found that GetLastError() returns 2 (ERROR_FILE_NOT_FOUND) if:

  1. the file doesn't exist OR,

  2. the section doesn't exist OR,

  3. the key doesn't exist.

I would like to know the specific reason the GetPrivateProfileInt returned the default value - is this possible?

Note: I've looked into checking the value of 'errno' (or errorno, as it says on the MSDN page) but this is always 0 for any of the above cases.

8
  • 1
    I think that when you use this function you should already know that the section exist, because it is intended to retrive the value associated with the key. Maybe there is another function that check for the section? Commented Dec 23, 2015 at 14:20
  • As Martin Bonner reminded me below, there is a function that can be used to read in the full section. I'll try us this and see if it helps. Commented Dec 23, 2015 at 14:47
  • 1
    Yes, I think is the correct way to do this, see also GetPrivateProfileSectionNames that returns all the section names. Commented Dec 23, 2015 at 14:50
  • The documentation doesn't say, that the last error code is set to any specific value in case a key cannot be retrieved. Don't rely on it, as the behavior can change without prior notice. Commented Dec 23, 2015 at 15:53
  • @IInspectable, it mentions it in the documentation for GetPrivateProfileString() which I am also using. Anyway, neither work and I'm going with the solution suggested by others. Commented Dec 23, 2015 at 16:09

1 Answer 1

2

If GetLastError() won't help, then you need to do some detective work. You should check yourself if the registry key or file exists (you know that most ini files are mapped to the registry, right?), or if the section doesn't exist (GetPrivateProfileSection), and if neither of those apply, then the key doesn't exist.

Sign up to request clarification or add additional context in comments.

2 Comments

The values of this INI file are not mapped to the registry. It is located in application directory, and contains information relating to the application but it is not initialisation information. These keys or sections will only be there if the user enables a specific function of the app (I know, bad design, but it's what I have to work with). So I would like to find out programmatically what is causing the default value to be returned so I can handle it.
The tip to use GetPrivateProfileSection to check if the section exists first is a good one. I know the file should exist, otherwise there would be a much, much bigger issue. I'll try this. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.