You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegConfig.cxx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // -=- RegConfig.cxx
  19. #include <malloc.h>
  20. #include <rfb_win32/RegConfig.h>
  21. #include <rfb/LogWriter.h>
  22. #include <rfb/util.h>
  23. //#include <rdr/HexOutStream.h>
  24. using namespace rfb;
  25. using namespace rfb::win32;
  26. static LogWriter vlog("RegConfig");
  27. RegConfig::RegConfig(EventManager* em) : eventMgr(em), event(CreateEvent(0, TRUE, FALSE, 0)), callback(0) {
  28. if (em->addEvent(event, this))
  29. eventMgr = em;
  30. }
  31. RegConfig::~RegConfig() {
  32. if (eventMgr)
  33. eventMgr->removeEvent(event);
  34. }
  35. bool RegConfig::setKey(const HKEY rootkey, const TCHAR* keyname) {
  36. try {
  37. key.createKey(rootkey, keyname);
  38. processEvent(event);
  39. return true;
  40. } catch (rdr::Exception& e) {
  41. vlog.debug("%s", e.str());
  42. return false;
  43. }
  44. }
  45. void RegConfig::loadRegistryConfig(RegKey& key) {
  46. DWORD i = 0;
  47. try {
  48. while (1) {
  49. TCharArray name(tstrDup(key.getValueName(i++)));
  50. if (!name.buf) break;
  51. TCharArray value(key.getRepresentation(name.buf));
  52. if (!value.buf || !Configuration::setParam(CStr(name.buf), CStr(value.buf)))
  53. vlog.info("unable to process %s", name.buf);
  54. }
  55. } catch (rdr::SystemException& e) {
  56. if (e.err != 6)
  57. vlog.error("%s", e.str());
  58. }
  59. }
  60. void RegConfig::processEvent(HANDLE event_) {
  61. vlog.info("registry changed");
  62. // Reinstate the registry change notifications
  63. ResetEvent(event);
  64. key.awaitChange(true, REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET, event);
  65. // Load settings
  66. loadRegistryConfig(key);
  67. // Notify the callback, if supplied
  68. if (callback)
  69. callback->regConfigChanged();
  70. }
  71. RegConfigThread::RegConfigThread() : config(&eventMgr), thread_id(-1) {
  72. }
  73. RegConfigThread::~RegConfigThread() {
  74. PostThreadMessage(thread_id, WM_QUIT, 0, 0);
  75. wait();
  76. }
  77. bool RegConfigThread::start(const HKEY rootKey, const TCHAR* keyname) {
  78. if (config.setKey(rootKey, keyname)) {
  79. Thread::start();
  80. while (thread_id == (DWORD)-1)
  81. Sleep(0);
  82. return true;
  83. }
  84. return false;
  85. }
  86. void RegConfigThread::worker() {
  87. DWORD result = 0;
  88. MSG msg;
  89. thread_id = GetCurrentThreadId();
  90. while ((result = eventMgr.getMessage(&msg, 0, 0, 0)) > 0) {}
  91. if (result < 0)
  92. throw rdr::SystemException("RegConfigThread failed", GetLastError());
  93. }