Today we’re going to look at parsing an INI file with Win32 API’s. Now I’m sure there are thousands of ways to accomplish this task, I just prefer to use P/Invoke, it’s more straightforward for me and is less complicated (in my opinion). One thing to remember is when using a Win32 API you need to release the object as soon as you’re done with it, prevents bad things from happening within your application.



Now I know that INI files are considered outdated by some, but there are situations where working with them is easier than some of the alternatives, so let’s go.

NOTE: This assumes you have a semi-working knowledge of P/Invoke and how to use it.


Not only will this example show you how to parse your INI file, but also on how to release the objects from memory. In order to accomplish this we’re going to need a few namespaces:

    


So let’s get started. First add your references to the top of your class


Then we create our class (Mine is named, oddly enough, INIParse. Our class will implement the IDisposable Interface, allowing us to free up allocated resources.


Now inside our class let’s add some variables that will be needed. Three for handling the disposing of our object and two for read-only properties


Now, as stated, we’re going to be using P/Invoke, and the Win32 API’s we’ll be using are

    


So now let’s set up our three functions. For P/Invoke we will be adding the DLLImportAttribute letting the compiler/system know we are accessing an unmanaged DLL file


Now for our class properties. One will be used to hold the error message returned if a method failed, and the second will hold the path to our INI file that the user provides when the class is initialized. Both properties are read only


In our class we’re going to have a single constructor, that will set the value of the path to our INI file. In this constructor you’re going to see a reference to a method named GetHandle, you’ll see that method later (It’s used to get the handle to our class since, unlike a form, we don’t have a readily available handle so we’ll use Reflection to get it) so don’t worry about it right now


Well we now have our references, Win32 API’s variables & properties set. We have our constructor so the object can be used, now let’s move on to writing to and reading from an INI file.

writeValue

Pretty simple, we use WritePrivateProfileString to write our value to the specified key area. If no key is provided then Windows will create the section. We want to ensure the user provides a value tow rite so we make sure it’s not null or empty before attempting to write it. That’s simple enough, now let’s look at reading a value from your INI file.

for this we will use GetPrivateProfileString and provide it the INI file, the section we’re reading from and the key we want the value from. This method looks like this

readValue

Ok, our class is almost complete, but if you were to try and compile it now you’d get an error message because we haven’t implemented the IDisposable Interface yet, so let’s do that. First is the GetHandle method that is used in our constructor


Now our IDisposable Interface members



If you notice one is private and one is public. The public one is called whever the class is used, which in turns calls the private one, closing us P/Invoke and the handle to our class. Finally we use a Destructor


So now our class for parsing an INI file is created and ready to go. To test it I used my desktop.ini file to read from a form like so


Here’s the complete class in one fail swoop:


So now you know how to parse an INI file with P/Invoke, and it wasn’t nearly as scary and difficult as you thought it would be now was it. I hope you found this useful and thanks for reading 🙂 

What Do You Think on This ? Say Here