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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (C) 2002-2003 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. class rfb::win32::RegReaderThread : public Thread {
  28. public:
  29. RegReaderThread(RegistryReader& reader, const HKEY key);
  30. ~RegReaderThread();
  31. virtual void run();
  32. virtual Thread* join();
  33. protected:
  34. RegistryReader& reader;
  35. RegKey key;
  36. HANDLE event;
  37. };
  38. RegReaderThread::RegReaderThread(RegistryReader& reader_, const HKEY key_) : Thread("RegConfig"), reader(reader_), key(key_) {
  39. }
  40. RegReaderThread::~RegReaderThread() {
  41. }
  42. void
  43. RegReaderThread::run() {
  44. vlog.debug("RegReaderThread started");
  45. while (key) {
  46. // - Wait for changes
  47. vlog.debug("waiting for changes");
  48. key.awaitChange(true, REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET);
  49. // - Load settings
  50. RegistryReader::loadRegistryConfig(key);
  51. // - Notify specified thread of changes
  52. if (reader.notifyThread)
  53. PostThreadMessage(reader.notifyThread->getThreadId(),
  54. reader.notifyMsg.message,
  55. reader.notifyMsg.wParam,
  56. reader.notifyMsg.lParam);
  57. else if (reader.notifyWindow)
  58. PostMessage(reader.notifyWindow,
  59. reader.notifyMsg.message,
  60. reader.notifyMsg.wParam,
  61. reader.notifyMsg.lParam);
  62. }
  63. }
  64. Thread*
  65. RegReaderThread::join() {
  66. RegKey old_key = key;
  67. key.close();
  68. if ((HKEY)old_key) {
  69. // *** Closing the key doesn't always seem to work
  70. // Writing to it always will, instead...
  71. vlog.debug("closing key");
  72. old_key.setString(_T("dummy"), _T(""));
  73. }
  74. return Thread::join();
  75. }
  76. RegistryReader::RegistryReader() : thread(0), notifyThread(0) {
  77. memset(&notifyMsg, 0, sizeof(notifyMsg));
  78. }
  79. RegistryReader::~RegistryReader() {
  80. if (thread) delete thread->join();
  81. }
  82. bool RegistryReader::setKey(const HKEY rootkey, const TCHAR* keyname) {
  83. if (thread) delete thread->join();
  84. thread = 0;
  85. RegKey key;
  86. try {
  87. key.createKey(rootkey, keyname);
  88. loadRegistryConfig(key);
  89. } catch (rdr::Exception& e) {
  90. vlog.debug(e.str());
  91. return false;
  92. }
  93. thread = new RegReaderThread(*this, key);
  94. if (thread) thread->start();
  95. return true;
  96. }
  97. void
  98. RegistryReader::loadRegistryConfig(RegKey& key) {
  99. DWORD i = 0;
  100. try {
  101. while (1) {
  102. TCharArray name = tstrDup(key.getValueName(i++));
  103. if (!name.buf) break;
  104. TCharArray value = key.getRepresentation(name.buf);
  105. if (!value.buf || !Configuration::setParam(CStr(name.buf), CStr(value.buf)))
  106. vlog.info("unable to process %s", CStr(name.buf));
  107. }
  108. } catch (rdr::SystemException& e) {
  109. if (e.err != 6)
  110. vlog.error(e.str());
  111. }
  112. }
  113. bool RegistryReader::setNotifyThread(Thread* thread, UINT winMsg, WPARAM wParam, LPARAM lParam) {
  114. notifyMsg.message = winMsg;
  115. notifyMsg.wParam = wParam;
  116. notifyMsg.lParam = lParam;
  117. notifyThread = thread;
  118. notifyWindow = 0;
  119. return true;
  120. }
  121. bool RegistryReader::setNotifyWindow(HWND window, UINT winMsg, WPARAM wParam, LPARAM lParam) {
  122. notifyMsg.message = winMsg;
  123. notifyMsg.wParam = wParam;
  124. notifyMsg.lParam = lParam;
  125. notifyWindow = window;
  126. notifyThread = 0;
  127. return true;
  128. }