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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <malloc.h>
  23. #include <rfb_win32/RegConfig.h>
  24. #include <rfb/LogWriter.h>
  25. #include <rfb/util.h>
  26. //#include <rdr/HexOutStream.h>
  27. using namespace rfb;
  28. using namespace rfb::win32;
  29. static LogWriter vlog("RegConfig");
  30. RegConfig::RegConfig(EventManager* em) : eventMgr(em), event(CreateEvent(0, TRUE, FALSE, 0)), callback(0) {
  31. if (em->addEvent(event, this))
  32. eventMgr = em;
  33. }
  34. RegConfig::~RegConfig() {
  35. if (eventMgr)
  36. eventMgr->removeEvent(event);
  37. }
  38. bool RegConfig::setKey(const HKEY rootkey, const TCHAR* keyname) {
  39. try {
  40. key.createKey(rootkey, keyname);
  41. processEvent(event);
  42. return true;
  43. } catch (rdr::Exception& e) {
  44. vlog.debug("%s", e.str());
  45. return false;
  46. }
  47. }
  48. void RegConfig::loadRegistryConfig(RegKey& key) {
  49. DWORD i = 0;
  50. try {
  51. while (1) {
  52. TCharArray name(tstrDup(key.getValueName(i++)));
  53. if (!name.buf) break;
  54. TCharArray value(key.getRepresentation(name.buf));
  55. if (!value.buf || !Configuration::setParam(CStr(name.buf), CStr(value.buf)))
  56. vlog.info("unable to process %s", name.buf);
  57. }
  58. } catch (rdr::SystemException& e) {
  59. if (e.err != 6)
  60. vlog.error("%s", e.str());
  61. }
  62. }
  63. void RegConfig::processEvent(HANDLE /*event*/) {
  64. vlog.info("registry changed");
  65. // Reinstate the registry change notifications
  66. ResetEvent(event);
  67. key.awaitChange(true, REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET, event);
  68. // Load settings
  69. loadRegistryConfig(key);
  70. // Notify the callback, if supplied
  71. if (callback)
  72. callback->regConfigChanged();
  73. }
  74. RegConfigThread::RegConfigThread() : config(&eventMgr), thread_id(-1) {
  75. }
  76. RegConfigThread::~RegConfigThread() {
  77. PostThreadMessage(thread_id, WM_QUIT, 0, 0);
  78. wait();
  79. }
  80. bool RegConfigThread::start(const HKEY rootKey, const TCHAR* keyname) {
  81. if (config.setKey(rootKey, keyname)) {
  82. Thread::start();
  83. while (thread_id == (DWORD)-1)
  84. Sleep(0);
  85. return true;
  86. }
  87. return false;
  88. }
  89. void RegConfigThread::worker() {
  90. BOOL result = 0;
  91. MSG msg;
  92. thread_id = GetCurrentThreadId();
  93. while ((result = eventMgr.getMessage(&msg, 0, 0, 0)) > 0) {}
  94. if (result < 0)
  95. throw rdr::SystemException("RegConfigThread failed", GetLastError());
  96. }