Upon system startup I have a Windows service that is being loaded along with its System and User processes. Right after the User process is being loaded (as an admin), I call the QueryDisplayConfig to check display specific info. The problem is that sometimes, only sometimes QueryDisplayConfig returns a value that is neither of its possible return values.
The work around is before calling QueryDisplayConfig to open and close the Windows registry. It's interesting but somehow it fixes things. I am wondering what is it that regedit.exe does that forces Windows to update the registry.
It's possible that there is no solution for this problem, and I would have to resort to the following solution, which is also a solution:
LONG lResult = QueryDisplayConfig(QDC_DATABASE_CURRENT, &PathArraySize, PathArray.data(), &ModeArraySize, ModeArray.data(), &CurrentTopology);
while (lResult != ERROR_SUCCESS &&
lResult != ERROR_INVALID_PARAMETER &&
lResult != ERROR_NOT_SUPPORTED &&
lResult != ERROR_ACCESS_DENIED &&
lResult != ERROR_GEN_FAILURE &&
lResult != ERROR_INSUFFICIENT_BUFFER)
{
Sleep(300);
lResult = QueryDisplayConfig(QDC_DATABASE_CURRENT, &PathArraySize, PathArray.data(), &ModeArraySize, ModeArray.data(), &CurrentTopology);
}
I'd rather not resort to this ugly workaround and have Windows update/refresh the registry programmatically.
I get different results sometimes when searching the registry via REGEDIT if I close/reopen REGEDIT, vs if I don't. This is on WS08R2. I had assumed that was a problem with REGEDIT itself. Now it sounds like a possible Windows bug. If that's the case there won't be any pretty workaround.
Based on Windows Internals the registry is operated on in memory. So any queries of values/keys will always be from memory. If the value/key is not currently in memory, it will first be read, but the code path after the read is the same as if it had already been in memory. There shouldn't be a case of the on disk hive being more current than that part of the hive in memory.
So "refreshing" the registry doesn't really make sense, based on the documented design. Therefore I wouldn't expect there to be an API to "refresh".
I think the least ugly way to do this (depending on your sense of aesthetics) is to try to imitate what REGEDIT does. When you close it, it saves where it was last browsing in the registry, so the next time you run REGEDIT, it will (at least) open and read whatever hives, keys, & values are necessary to fill REGEDIT's window.
So you can run REGEDIT the next time you get an unexpected value from QueryDisplayConfig
and see what keys & values are being displayed. Then code that into your program. You don't actually have to display the values of course, but just open & read them.