How do I iterate through IO Kit's keys? - c++

I am trying to get a list of all devices in the system together with how they are connected. Therefore, I want to essentially clone the structure of the IO Kit services tree (that you can see with IORegistryExplorer). How do I iterate through all the keys? (One of the reasons this is confusing to me is because I dont understand what the difference between io_service, io_registry, and io_object are).

The difference between service, registry and object is only in the circumstances they are used. Otherwise they are completely the same.
From IOTypes.h:
typedef io_object_t io_registry_entry_t;
typedef io_object_t io_service_t;
There is documentation available about Traversing the I/O Registry, which also includes information on traversing the whole registry.
For each entry you then would have to get the properties and save them with your representation of the registry.
So you would use IORegistryGetRootEntry(), print/save its name and properties and then iterate over the children with IORegistryEntryGetChildIterator().
You get the properties with IORegistryEntryCreateCFProperties() following aCFDictionaryGetKeysAndValues(). For the values you then have to check what types these are to print/save them (or use CFSHOW). When you really want to clone this into a different structure (with different types) you have to handle every possible CFTypeID explicitely.
I created a working prototype at
https://gist.github.com/JonnyJD/6126680
EDIT:
In another SO answer the (C) source code of ioreg is linked. That should be a good resource for printing/extracting missing CFTypes.

Related

Accessing MTP vendor extended properties through WPD

I am using Windows Portable Device API to access some MTP devices. I want to read the vendor extended properties from the device, it should look something like this:
"microsoft.com/WPDNA" or "microsoft.com/MTPZ"
It seems like this should be a quite simple task but I cannot figure it out.
I have been able to enumerate objects on a device and transfer files and so on, this was included in the WpdApiSample Application.
I also found this article that I think is what I want to do. But I don't understand how to create those queries.
Without getting into code, the short answer is to scan a range of PIDs for a given FormatID to see what kinds of data are in there. This is a debugging exercise, just for discovery purposes. I basically just write a loop: for example if I want to scan for the first 16 PIDs under the basic extended properties you'd use the WPD_PROPERTIES_MTP_VENDOR_EXTENDED_DEVICE_PROPS as FormatID and then for PID change it on each iteration and scan values 0xD101 through 0xD10F. You can usually tell from the output what may be contained in that PID location.
Once you know the PIDs for the pieces of data you want, you can write that into your code as part of your enumeration routine.

What are SetupAPI,SetupAPI1,and SetupAPI2? And what the difference between them?

Recently,I need to realize a little function in MFC by C++ which is used to get a list of available serial ports. And then I saw this. I need to know what is SetupAPI1 and SetupAPI2? Where can I get the setup.dll when I wanna use this way to realize my function?. During searching the Internet I found there is a SetupAPI but it seems another one. Now, I am really confusing by these SetupAPI, SetupAPI1, and SetupAPI2. What are the differences? Where are they come from? And, how can I get them if I wanna to use these?
There is no such think as SetupAPI1 and SetupAPI2. There is a library named SetupAPI.
This answer you link to refers to a library named EnumSerialPort. From that page:
Internally the code provides 9 different ways (yes you read that right: Nine) of enumerating serial ports: Using CreateFile, QueryDosDevice, GetDefaultCommConfig, two ways using the Setup API, EnumPorts, WMI, Com Database & enumerating the values under the registry key HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM.
...
All of the configuration of the code is controlled by the following preprocessor values: CENUMERATESERIAL_USE_STL, _AFX, NO_ENUMSERIAL_USING_CREATEFILE, NO_ENUMSERIAL_USING_QUERYDOSDEVICE, NO_ENUMSERIAL_USING_GETDEFAULTCOMMCONFIG, NO_ENUMSERIAL_USING_SETUPAPI1, NO_ENUMSERIAL_USING_SETUPAPI2, NO_ENUMSERIAL_USING_ENUMPORTS, NO_ENUMSERIAL_USING_WMI, NO_ENUMSERIAL_USING_COMDB & NO_ENUMSERIAL_USING_REGISTRY.
This library presents two different methods that rely on SetupAPI, and simply numbers them one and two. You can see these details and more by following the links.

How to create a undeletable registry key (or file) in C++

I would like to create time limited version of a game I am developping in C++.
I already meet some program that create files I am still not able to delete, and other that created registry key very hard to delete (because there were several sub key and needed to change owner to the deeper first, delete it go up, change owner...).
Then I think the only way is to create something in the computer that is not (or hardly) deletable to mark the beginning of trial. I know how to create key or file but not how to protect them.
Thanks.
The standard Windows API is unable to deal with null characters in key/value names, whereas the native API can. As such, you can create a key containing one or more NULL characters in the name to store your licensing data, which would not be deletable using standard tools like regedit.
See: http://technet.microsoft.com/en-us/sysinternals/dd581628 for details and source code.

Is there some common way to write and read config files?

I need my program to create and edit a config file, which would contain information about set of objects, and than read it at every execution. Is there some sort of guideline for config style that i can use?
I'm using C++ on windows.
I recommend checking out boost::property_tree.
The Property Tree library provides a data structure that stores an arbitrarily deeply nested tree of values, indexed at each level by some key. Each node of the tree stores its own value, plus an ordered list of its subnodes and their keys. The tree allows easy access to any of its nodes by means of a path, which is a concatenation of multiple keys.
Additionally, it contains parsers and generators for XML, INI, and JSON, so you can save/load in the format of your choice.
It largely depends on the language, platform and the scope of your config files. There's for example the properties files in Java world for configuration, and others already mentioned here such as YAML.
XML is generally frowned upon for configuration, since it's very verbose. You still find it in many applications, Web frameworks, etc.
I think the best practice would be to choose the right configuration format for the job at hand. You can evaluate and try them for yourself, while considering these pointers:
What's the standard? (e.g. ini files in Windows, properties files in Java)
Is there native support in my language, or do I have to roll my own implementation?
Can my configuration format easily describe what I want to store as configuration?
I'm sure you could think of other considerations. If you update your question to clarify the scope, you'll get more useful answers.
YAML is a very popular solution for creating configuration files. It is used prominently by Ruby on Rails and Google AppEngine, for instance. YAML libraries are available for many if not most languages.
If you're working with Qt (based on your other questions),
look at the QSettings class. I've been using these to set a lot of persistent info, such as last visited directory, and which options were last used.
boost::program_options has some capacity to read ini files.
What is nice is that is allow to unify reading ini file and command line options. As Windows program goes, that may not be seen as such an interresting future, but for my unix app it was a killer feature.
However, boost::po does not allow editing or writing of ini files, hence you'll probably be better off with boost::property_tree.
The most common format is the INI File format. Most mdoern languages should have something built in to handle these, otherwise I'm sure you'd be able to find a third-party library.
Failing this, you could use XML, and if your language supports it, serializing to and deserializing from these XML files. If you don't want to/can't use XML, most languages will have a default format they use, such as a pure binary dump of the data in an object.
There is a free, cross-platform library for configuration file management, called libconfig. It supports very nice Yaml-based grammar, and supports user-defined grammar too. It also has many other computing language bindings. See it's homepage: http://www.hyperrealm.com/libconfig/ .
In the Java world, a properties file is pretty easy to use:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
As the others have said, you would need to describe your environment to get the best recommendation.

Web Service to return complex object with optional parts

I'm trying to think of the correct design for a web service. Essentially, this service is going to perform a client search in a number of disparate systems, and return the results.
Now, a client can have various pieces of information attached - e.g. various pieces of contact information, their address(es), personal information. Some of this information may be complex to retrieve from some systems, so if the consumer isn't going to use it, I'd like them to have some way of indicating that to the web service.
One obvious approach would be to have different methods for different combinations of wanted detail - but as the combinations grow, so too do the number of methods. Another approach I've looked at is to add two string array parameters to the method call, where one array is a list of required items (e.g. I require contact information), and the other is optional items (e.g. if you're going to pull in their names anyway, you might as well return that to me).
A third approach would be to add additional methods to retrieve the detail. But that's going to explode the number of round trips if I need all the details for potentially hundreds of clients who make up the result.
To be honest, I'm not sure I like any of the above approaches. So how would you design such a generic client search service?
(Considered CW since there might not be a single "right" answer, but I'll wait and see what sort of answers arrive)
Create a "criteria" object and use that as a parameter. Such an object should have a bunch of properties to indicate the information you want. For example "IncludeAddresses" or "IncludeFullContactInformation".
The consumer is then responsible to set the right properties to true, and all combinations are possible. This will also make the code in the service easier to do. You can simply write if(criteria.IncludeAddresses){response.Addresses = GetAddresses;}
Any non-structured or semi-structured data is best handled by XML. You might pass XML data via a string or wrap it up in a class adding some functionality to it. Use XPathNavigator to go through XML. You can also use XMLDocument class although it is not too friendly to use. Anyway, you will need some kind of class to handle XML content of course.
That's why XML was invented - to handle data which structure is not clearly defined.
Regards,
Maciej