I am currently working on an add-in for Visual Studio to expose some functionality available in the “Debugger Engine and Extension APIs”. The development is in the very early stages, but I have completed already one feature required by the Add-in, which is the ability to read XML configuration files similarly to how .NET and the CLR do but in this case using Visual C++. This Add-in comprises native and managed code, where .NET helps me to interact with the IDE and the core functionality of the Add-in is encapsulated in a DLL. This blog entry describes the ConfigReader class.
A sample configuration file is shown below
The configuration file must reside in the same folder of the library, and it’s located in the constructor of the class
Since the DLL can be anywhere (unlike .NET) that it’s either in the GAC or the bin folder of the application, I needed to find the configuration file in the same path of the library, and this is done via enumerating the loaded modules.
A few things worth mentioning:
- Always use STL containers and algorithms when possible (e.g.: std::vector instead of a C/C++ type array.
- To query information about a process, this one has to be opened and most of the times (depending on what it’s required to do) a few flags must be set. Also remember to close any handle after it’s been used.
- Use std::wstring (UNICODE) instead of wchar_t*. They’re safer and better to use.
- Use lambdas in conjunction with algorithms (e.g. s td::find_if)
- Get to learn and know the functions to convert from Multibyte (ANSI) to wide characters (UNICODE) (e.g.: wcstombs_s)
- Pass references (and use const whenever is possible)
Our ConfigReader object has two main fields (Extensions and Properties) – Depicted below
and we can also retrieve any property from the configuration file, in a similar way we do it in .NET. This is accomplished via the GetSetting method