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