CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2007
    Posts
    6

    Writing .ini Files/How to Assign Keys

    Ok i need some help trying to store some information for a "save game" but I Dont know how to write to a .ini or cfg file
    Can you show me how to write and read a ini or cfg file


    and can you also show me how to assign a keyboard Key such as A to do a certain function like attack or something

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Writing .ini Files/How to Assign Keys

    Here's an INI sample.

    As far as assigning keys, you'd use the keypress or keydown function to get the character, then process it however you want.
    Attached Files Attached Files
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: Writing .ini Files/How to Assign Keys

    You'll want to work with the following functions
    Code:
    'profile (INI file) subs & functions
    Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long '_32_BIT_CONVERTED_'
    Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long '_32_BIT_CONVERTED_'
    Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Long, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long '_32_BIT_CONVERTED_'
    Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long '_32_BIT_CONVERTED_'
    Declare Function ErasePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Long, ByVal lpFileName As String) As Long '_32_BIT_CONVERTED_'
    Declare Function ErasePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Long, ByVal lpString As Long, ByVal lpFileName As String) As Long '_32_BIT_CONVERTED_'
    You can use those API functions to read from and write to ini files. By default it reads to and writes from the registry but you can use it to map to your own ini file. I can't remember off the top of my head how to do it, though. Sorry, it's been a while!
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Writing .ini Files/How to Assign Keys

    @David: Nice little tutorial.

    What always inrigued me: does anybody know how these functions work, with respect to the .ini file?
    I mean that:
    If I write a new key to a section of an existing file, is the file completely rewritten then? Must-of somehow, I think, because it's a sequential text file.
    And if I read entries from the file I might read them in any unpredictable order, so: is the file then scanned (opened read and closed) for any entry I read?
    Sorry. Just curiousity.

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