CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2015
    Posts
    7

    Reading ini files in Visual Studio C++

    I have the following ini file

    [DB_SETTINGS]
    USER_NUM_MAX = 399 ; Maximum number of users
    USER_NAME = "Mark"

    and the following c++ code:

    Code:
    	LPCTSTR path = L".\\setup.ini";
    	LPTSTR protocolChar1;
    
    	Int16 a = GetPrivateProfileInt(L"DB_SETTINGS", L"USER_NUM_MAX", -1, path);
    	Int16 b = GetPrivateProfileString(L"DB_SETTINGS", L"USER_NAME",L"", protocolChar1, 255, path);
    The program will get the first value from the ini (399), but crashes with a System.AccessViolationException. Would anyone be able to point me in the right direction to fix this!

    Much appreciated!
    Last edited by cybernetuk; February 13th, 2015 at 04:58 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reading ini files in Visual Studio C++

    The issue is with GetPrivateProfileString. You haven't allocated any memory to the buffer to where the returned data is to be stored. Try replacing
    Code:
    LPTSTR protocolChar1;
    with
    Code:
    TCHAR protocolChar1[256];
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2015
    Posts
    7

    Re: Reading ini files in Visual Studio C++

    Thank you, appreciate it!!


    Quote Originally Posted by 2kaud View Post
    The issue is with GetPrivateProfileString. You haven't allocated any memory to the buffer to where the returned data is to be stored. Try replacing
    Code:
    LPTSTR protocolChar1;
    with
    Code:
    TCHAR protocolChar1[256];

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured