{"id":104574,"date":"2020-12-21T07:00:00","date_gmt":"2020-12-21T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104574"},"modified":"2020-12-22T07:20:23","modified_gmt":"2020-12-22T15:20:23","slug":"20201221-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20201221-00\/?p=104574","title":{"rendered":"How can I emulate the REG_NOTIFY_THREAD_AGNOSTIC flag on systems that don&#8217;t support it? part 1"},"content":{"rendered":"<p>Windows 8 introduced the <code>REG_<wbr \/>NOTIFY_<wbr \/>THREAD_<wbr \/>AGNOSTIC<\/code> flag to the <code>Reg\u00adNotify\u00adChange\u00adKey\u00adValue<\/code> function. But what if you need to run on systems older than that?<\/p>\n<p>One of the recommended workarounds for the lack of support for the <code>REG_<wbr \/>NOTIFY_<wbr \/>THREAD_<wbr \/>AGNOSTIC<\/code> flag is to do the registration from a persistent thread, or at least a thread that is guaranteed to exist for as long as you need the registration. Maybe your program already has such a persistent thread. In that case, you can marshal the request to that thread. For example, if the registration is made by a class whose lifetime is tied to some UI, then you can perform the registration on the UI thread.<\/p>\n<pre>struct RegNotifyChangeKeyValueAsyncArgs\r\n{\r\n  HKEY hkey;\r\n  BOOL bWatchSubtree;\r\n  DWORD dwNotifyFilter;\r\n  HANDLE hEvent;\r\n  LONG result;\r\n};\r\n\r\n#define WM_REGNOTIFYCHANGEKEYVALUEASYNC (WM_APP + somevalue)\r\n\r\nLONG MyClass::RegNotifyChangeKeyValueAsync(\r\n  HWND hwnd,\r\n  HKEY hkey,\r\n  BOOL bWatchSubtree,\r\n  DWORD dwNotifyFilter,\r\n  HANDLE hEvent)\r\n{\r\n  RegNotifyChangeKeyValueAsyncArgs args = \r\n    { hkey, bWatchSubtree, dwNotifyFilter, hEvent,\r\n      ERROR_INVALID_PARAMETER };\r\n  SendMessage(m_hwnd, WM_REGNOTIFYCHANGEKEYVALUEASYNC,\r\n              0, reinterpret_cast&lt;LPARAM&gt;(&amp;args));\r\n  return args.result;\r\n}\r\n\r\n\/\/ in the WndProc\r\ncase WM_REGNOTIFYCHANGEKEYVALUEASYNC:\r\n  {\r\n    auto args = reinterpret_cast&lt;\r\n                 RegNotifyChangeKeyValueAsyncArgs*&gt;(lParam);\r\n    args-&gt;result = RegNotifyChangeKeyValue(\r\n      args-&gt;hkey,\r\n      args-&gt;bWatchSubtree,\r\n      args-&gt;dwNotifyFilter,\r\n      args-&gt;hEvent,\r\n      TRUE);\r\n    return TRUE;\r\n  }\r\n<\/pre>\n<p>This version marshals the call to the UI thread. I&#8217;m assuming the purpose of this class is to implement some UI element, so the UI thread will exist for as long as the corresponding window exists.<\/p>\n<p>Next time, we&#8217;ll discuss what we can do if we don&#8217;t have a UI thread or other persistent thread that we can rely upon to do our bidding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An exercise from long ago.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-104574","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>An exercise from long ago.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=104574"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104574\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=104574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}