pEpCOMServerAdapter.cpp
author Volker Birk <vb@pep-project.org>
Sun, 19 Jun 2016 01:04:41 +0200
changeset 87 f0ce6a1a7118
parent 86 e18b34f4b9cc
child 88 659d0fc5bc30
permissions -rw-r--r--
keep_plugin
     1 // pEpCOMServerAdapter.cpp : Implementation of WinMain
     2 
     3 
     4 #include "stdafx.h"
     5 #include "resource.h"
     6 #include "pEpCOMServerAdapter_i.h"
     7 #include "xdlldata.h"
     8 
     9 
    10 using namespace ATL;
    11 using namespace std;
    12 
    13 class CpEpCOMServerAdapterModule;
    14 
    15 static class GateKeeper {
    16 public:
    17     static const time_t cycle = 7200;   // 7200 sec is 2 h
    18     static const DWORD waiting = 10000; // 10000 ms is 10 sec
    19 
    20     static time_t time_diff()
    21     {
    22         try {
    23             static random_device rd;
    24             static mt19937 gen(rd());
    25 
    26             uniform_int_distribution<time_t> dist(0, cycle);
    27 
    28             return dist(gen);
    29         }
    30         catch (exception& e) {
    31             assert(0);
    32             return 0;
    33         }
    34     }
    35 
    36     GateKeeper(CpEpCOMServerAdapterModule * const self) : _self(self), now(time(NULL)), next(now + time_diff())
    37     {
    38         LONG lResult = RegOpenCurrentUser(KEY_READ, &cu);
    39         assert(lResult == ERROR_SUCCESS);
    40     }
    41 
    42     ~GateKeeper()
    43     {
    44         RegCloseKey(cu);
    45     }
    46 
    47     void keep();
    48 
    49     void keep_plugin();
    50     void keep_updated();
    51 
    52 protected:
    53     static const LPCTSTR plugin_reg_path;
    54     static const LPCTSTR plugin_reg_value_name;
    55 
    56 private:
    57     time_t now;
    58     time_t next;
    59     HKEY cu;
    60 
    61     CpEpCOMServerAdapterModule * const _self;
    62 };
    63 
    64 const LPCTSTR GateKeeper::plugin_reg_path = _T("Software\\Microsoft\\Office\\Outlook\\Addins\\pEp");
    65 const LPCTSTR GateKeeper::plugin_reg_value_name = _T("LoadBehavior");
    66 
    67 void GateKeeper::keep()
    68 {
    69     while (1) {
    70         keep_plugin();
    71 
    72         now = time(NULL);
    73         assert(now != -1);
    74         
    75         if (now > next) {
    76             next = now + GateKeeper::cycle;
    77             keep_updated();
    78         }
    79 
    80         Sleep(10000);
    81     }
    82 }
    83 
    84 void GateKeeper::keep_plugin()
    85 {
    86     DWORD value;
    87     DWORD size;
    88     LONG lResult = RegGetValue(cu, plugin_reg_path, plugin_reg_value_name, RRF_RT_REG_DWORD, NULL, &value, &size);
    89 
    90     assert(lResult == ERROR_SUCCESS);
    91     if (lResult != ERROR_SUCCESS)
    92         return;
    93 
    94     if (value != 3) {
    95         lResult = RegSetValue(cu, plugin_reg_path, RRF_RT_REG_DWORD, plugin_reg_value_name, 3);
    96         assert(lResult == ERROR_SUCCESS);
    97     }
    98 }
    99 
   100 void GateKeeper::keep_updated()
   101 {
   102 
   103 }
   104 
   105 class CpEpCOMServerAdapterModule : public ATL::CAtlExeModuleT< CpEpCOMServerAdapterModule >
   106 {
   107 public:
   108     CpEpCOMServerAdapterModule() : ATL::CAtlExeModuleT< CpEpCOMServerAdapterModule >(), gatekeeper_thread(NULL)
   109     {
   110         gatekeeper_thread = new thread(gatekeeper, this);
   111         gatekeeper_thread->detach();
   112     }
   113 
   114 	DECLARE_LIBID(LIBID_pEpCOMServerAdapterLib)
   115 	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_PEPCOMSERVERADAPTER, "{B3480081-82C0-4EE4-9AA1-3F513C9D78DD}")
   116 
   117 private:
   118     thread *gatekeeper_thread;
   119 
   120 protected:
   121     static void gatekeeper(CpEpCOMServerAdapterModule * const self);
   122 };
   123 
   124 void CpEpCOMServerAdapterModule::gatekeeper(CpEpCOMServerAdapterModule * const self)
   125 {
   126     GateKeeper keeper(self);
   127     keeper.keep();
   128 }
   129 
   130 CpEpCOMServerAdapterModule _AtlModule;
   131 
   132 //
   133 extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, 
   134 								LPTSTR /*lpCmdLine*/, int nShowCmd)
   135 {
   136     return _AtlModule.WinMain(nShowCmd);
   137 }
   138