From 682dc048e93d6ca4f1192271bf61d108c5acffac Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 29 Jun 2020 15:15:49 +0200 Subject: [PATCH] Migrate manual DLL registration to DllRegisterServer() Signed-off-by: Simon Rozman --- EAPMethods/EAP-TTLS.vcxproj.filters | 3 + EAPMethods/EAP-TTLS_UI.vcxproj.filters | 3 + EAPMethods/EAPMethod.def | Bin 2160 -> 2280 bytes EAPMethods/EAPMethod.props | 1 + EAPMethods/EAPMethod_UI.def | Bin 1920 -> 2040 bytes EAPMethods/EAPMethod_UI.props | 1 + EAPMethods/PCH.h | 2 + EAPMethods/PCH_UI.h | 2 + EAPMethods/Register.cpp | 99 ++++++++++++++ EAPMethods/Register_UI.cpp | 100 +++++++++++++++ Makefile | Bin 38560 -> 22504 bytes MakefileEAPMethod.mak | Bin 5336 -> 1922 bytes include/ResourceDLL.props | 22 ---- lib/Events/build/Events.vcxproj | 74 +++++++++-- lib/Events/build/Events.vcxproj.filters | 20 ++- lib/Events/src/Events.def | Bin 0 -> 1692 bytes lib/Events/src/PCH.cpp | 21 +++ lib/Events/src/PCH.h | 31 +++++ lib/Events/src/Register.cpp | 164 ++++++++++++++++++++++++ lib/WinStd | 2 +- 20 files changed, 513 insertions(+), 32 deletions(-) create mode 100644 EAPMethods/Register.cpp create mode 100644 EAPMethods/Register_UI.cpp delete mode 100644 include/ResourceDLL.props create mode 100644 lib/Events/src/Events.def create mode 100644 lib/Events/src/PCH.cpp create mode 100644 lib/Events/src/PCH.h create mode 100644 lib/Events/src/Register.cpp diff --git a/EAPMethods/EAP-TTLS.vcxproj.filters b/EAPMethods/EAP-TTLS.vcxproj.filters index ebf0b23..5a3cd0e 100644 --- a/EAPMethods/EAP-TTLS.vcxproj.filters +++ b/EAPMethods/EAP-TTLS.vcxproj.filters @@ -26,6 +26,9 @@ Source Files + + Source Files + diff --git a/EAPMethods/EAP-TTLS_UI.vcxproj.filters b/EAPMethods/EAP-TTLS_UI.vcxproj.filters index cd27167..d4de84b 100644 --- a/EAPMethods/EAP-TTLS_UI.vcxproj.filters +++ b/EAPMethods/EAP-TTLS_UI.vcxproj.filters @@ -30,6 +30,9 @@ Source Files + + Source Files + diff --git a/EAPMethods/EAPMethod.def b/EAPMethods/EAPMethod.def index 4fb97d7f0f20989e171899a342862b1097330ea6..7d259ebac79204ffbbfd54cab915e615903ab8d8 100644 GIT binary patch delta 128 zcmew$@Ir7y0Y?Qd0~Z4)g9}3r5C$=%GNdzPG88kE09i#0!C=0OdUy!WbMG SLKs{bkoAQ! Create + diff --git a/EAPMethods/EAPMethod_UI.def b/EAPMethods/EAPMethod_UI.def index 84d60a90e1edeb478c7c8d3938c97fa0972e2898..eef9b7acb4931734496172172e87423cb91c67d0 100644 GIT binary patch delta 128 zcmZqR|G~eZfxUv4fs28Y!G$3Q2!j|>8PXXt8HyQ7fUF{hU@%z*COH`bfbyOUVGNE8 SAq=hz$ofJV@_?#I(+dDFtQL3x delta 7 Ocmeyt-@w12fgJ!0`~uDZ diff --git a/EAPMethods/EAPMethod_UI.props b/EAPMethods/EAPMethod_UI.props index a38a2db..d5d6974 100644 --- a/EAPMethods/EAPMethod_UI.props +++ b/EAPMethods/EAPMethod_UI.props @@ -21,6 +21,7 @@ Create + diff --git a/EAPMethods/PCH.h b/EAPMethods/PCH.h index d3db77c..08b82fd 100644 --- a/EAPMethods/PCH.h +++ b/EAPMethods/PCH.h @@ -27,3 +27,5 @@ #else #error Unknown EAP Method type. #endif + +extern EAPMETHOD_PEER g_peer; diff --git a/EAPMethods/PCH_UI.h b/EAPMethods/PCH_UI.h index 820e989..e179c00 100644 --- a/EAPMethods/PCH_UI.h +++ b/EAPMethods/PCH_UI.h @@ -29,3 +29,5 @@ #else #error Unknown EAP Method type. #endif + +extern EAPMETHOD_PEER_UI g_peer; diff --git a/EAPMethods/Register.cpp b/EAPMethods/Register.cpp new file mode 100644 index 0000000..11f3033 --- /dev/null +++ b/EAPMethods/Register.cpp @@ -0,0 +1,99 @@ +/* + Copyright 2020-2020 Amebis + Copyright 2016 GÉANT + + This file is part of GÉANTLink. + + GÉANTLink is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + GÉANTLink is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GÉANTLink. If not, see . +*/ + +#include "PCH.h" + +using namespace std; +using namespace winstd; + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ LPCTSTR sValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, reinterpret_cast(sValue), (DWORD)((_tcslen(sValue) + 1) * sizeof(tstring::value_type))); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ const tstring &sValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, reinterpret_cast(sValue.c_str()), (DWORD)((sValue.length() + 1) * sizeof(tstring::value_type))); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ DWORD dwValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, reinterpret_cast(&dwValue), sizeof(dwValue)); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +/// +/// Registers method in EapHost registry +/// +/// \returns S_OK if successful; E_FAIL otherwise +/// +STDAPI DllRegisterServer() +{ + try { + tstring sz, sz2; + reg_key key_methods, key_author, key_method; + if (!key_methods.open(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\services\\EapHost\\Methods"), 0, KEY_CREATE_SUB_KEY)) throw win_runtime_error(); + sprintf(sz, _T("%u"), EAPMETHOD_AUTHOR_ID); + if (!key_author.create(key_methods, sz.c_str(), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + set_value(key_author, NULL, _T(PRODUCT_NAME_STR)); + sprintf(sz, _T("%u"), EAPMETHOD_TYPE); + if (!key_method.create(key_author, sz.c_str(), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + if (!GetModuleFileName(g_peer.m_instance, sz)) throw win_runtime_error("GetModuleFileName failed."); + sprintf(sz2, _T("@%s,-1"), sz.c_str()); + set_value(key_method, _T("PeerDllPath") , sz); + set_value(key_method, _T("PeerFriendlyName"), sz2); + set_value(key_method, _T("Properties") , (DWORD)389871807); + + return S_OK; + } catch(win_runtime_error &err) { + OutputDebugStr(_T("%hs (error %u)\n"), err.what(), err.number()); + return E_FAIL; + } catch(...) { + OutputDebugStr(_T("Registering DLL failed.\n")); + return E_FAIL; + } +} + + +/// +/// Unregisters method from EapHost registry +/// +/// \returns Always S_OK +/// +STDAPI DllUnregisterServer() +{ + try { + tstring sz; + reg_key key_methods; + if (!key_methods.open(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\services\\EapHost\\Methods"), 0, KEY_READ)) throw win_runtime_error(); + sprintf(sz, _T("%u\\%u"), EAPMETHOD_AUTHOR_ID, EAPMETHOD_TYPE); + if (!key_methods.delete_subkey(sz.c_str())) throw win_runtime_error(); + } catch(...) {} + return S_OK; +} diff --git a/EAPMethods/Register_UI.cpp b/EAPMethods/Register_UI.cpp new file mode 100644 index 0000000..59abc1e --- /dev/null +++ b/EAPMethods/Register_UI.cpp @@ -0,0 +1,100 @@ +/* + Copyright 2020-2020 Amebis + Copyright 2016 GÉANT + + This file is part of GÉANTLink. + + GÉANTLink is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + GÉANTLink is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GÉANTLink. If not, see . +*/ + +#include "PCH_UI.h" + +using namespace std; +using namespace winstd; + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ LPCTSTR sValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, reinterpret_cast(sValue), (DWORD)((_tcslen(sValue) + 1) * sizeof(tstring::value_type))); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ const tstring &sValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, reinterpret_cast(sValue.c_str()), (DWORD)((sValue.length() + 1) * sizeof(tstring::value_type))); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ DWORD dwValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, reinterpret_cast(&dwValue), sizeof(dwValue)); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +/// +/// Registers method UI in EapHost registry +/// +/// \returns S_OK if successful; E_FAIL otherwise +/// +STDAPI DllRegisterServer() +{ + try { + tstring sz; + reg_key key_methods, key_author, key_method; + if (!key_methods.open(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\services\\EapHost\\Methods"), 0, KEY_CREATE_SUB_KEY)) throw win_runtime_error(); + sprintf(sz, _T("%u"), EAPMETHOD_AUTHOR_ID); + if (!key_author.create(key_methods, sz.c_str(), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + set_value(key_author, NULL, _T(PRODUCT_NAME_STR)); + sprintf(sz, _T("%u"), EAPMETHOD_TYPE); + if (!key_method.create(key_author, sz.c_str(), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + if (!GetModuleFileName(g_peer.m_instance, sz)) throw win_runtime_error("GetModuleFileName failed."); + set_value(key_method, _T("PeerConfigUIPath") , sz); + set_value(key_method, _T("PeerIdentityPath") , sz); + set_value(key_method, _T("PeerInteractiveUIPath") , sz); + set_value(key_method, _T("PeerInvokePasswordDialog"), (DWORD)0); + set_value(key_method, _T("PeerInvokeUsernameDialog"), (DWORD)0); + + return S_OK; + } catch(win_runtime_error &err) { + OutputDebugStr(_T("%hs (error %u)\n"), err.what(), err.number()); + return E_FAIL; + } catch(...) { + OutputDebugStr(_T("Registering DLL failed.\n")); + return E_FAIL; + } +} + + +/// +/// Unregisters method UI from EapHost registry +/// +/// \returns Always S_OK +/// +STDAPI DllUnregisterServer() +{ + try { + tstring sz; + reg_key key_methods; + if (!key_methods.open(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\services\\EapHost\\Methods"), 0, KEY_READ)) throw win_runtime_error(); + sprintf(sz, _T("%u\\%u"), EAPMETHOD_AUTHOR_ID, EAPMETHOD_TYPE); + key_methods.delete_subkey(sz.c_str()); + } catch(...) {} + return S_OK; +} diff --git a/Makefile b/Makefile index 1af10ae5fb90dd1e408a80c69b9f00be55d9188d..920aef3588203b2143655b7a10cb8151126c2291 100644 GIT binary patch delta 94 zcmZ3mmg&WM#tm#-n`dw}X^9szlra=B7&90#=rN=+Q~>o36}YCekA1LlY9k^3{lK1&TcN&Ysz>}J*0Hdf)j%YG^^*n!k^u8+28i5V0pdnTEx+K69$w1{Ur`)uoD z`|dIJR&HUF%0>3X)~Ws1!vDoQnxG}xz_matXpK>X*6LIZKGcph>Xbl@QG(VfoCmkDINXLbu8*Ry?4Gc3k-d?-fKy#RN^^+76!=d2FN2jJ zt5L48fYOxm{gAzPwOCZIgY*bU5!@??V?rQCz``nQT?ekP?=MD;&IDN3vMCVjRm8lO z&E;CwPFF}eHXjPeEG9d{TCW;a8~pGf25uH;6TZbL3QsIL4B7P#%M8U`! z@-r;rh)e-yGmqYGCM!f91!)Lb@1g5>hmgyocuVLHE#t{Ar}#1~=2(x+NRM!o z{39VmIWw{=U zO}vITaYz3pn|f61JgBivWaJ3^4%2<~;t;nOsxPfL8_=wWk~vQ;>t^bC6?>Wa{=5?!?bM zBVF@2iD`ExZs;;sI-ykjtg%7!WXCRLMRnAjJ?Vrrj#GhMb2=(|1*57$e&Xj5zGem2 zpoUzUyq7)b+;1KEERNV=H?q)fA|l#jq$B%;FZ?u}Mrln-nH9QyJ13aH}hw LoWLu<0#XYA=Ry!R literal 5336 zcmeI0Z%-3J5XR@NCVq#D`a%+HMH9t8F(L(QEZ9H^!H|%a9;Km_=I#pe5&cr?@0qq_( z|3$W7M;2QPsq_~#Xx*GWMc0abX6>3a@>NCa4J=AyWcxgOScvlez?xtZnq%h1_0*8) z+o6>vYP_&Fk{fB=HYH zr2|J2_&Uf@22M|r>^UzjkN=P;#ALvbvDxqZ3)vIdmQ|^QclH5(8(2|gZ1ddWZJib> zW!qjdCjCMo78IMwI%Ei z5q`%+r|Ddzm+T4K9>WNJLNt3GEwQN#3bn3E>Uv+1Z1US4?V6{dsLiY>LpIaX74l85(HZANzNzCA!@flN~-hgz!GE+eWa zRn|JaDn86yA>9CJo%J)|kBG^Mds4?%@LzRZ2Z=08;__BTCc@?tyY()9eLR;f=kXEZ zRq(w>rUJ6{b7Wx`P~|jOTb!$VWi^m3F{6`r2uGLuzlH@xMDlIW_X@bno<*<0rjw=i zA95PCM=CtInr(;U*`rnAI6Q zxnifB-V9Flm7zPTzLL{GRpaXQt=rIH#NNsa?3A z*XoqhoAxcEFRg)ds8ckC&q@33cwH6u-XH7qiOutW!FhYeE;!GB_@4chx}8*~-v9#J B4FmuH diff --git a/include/ResourceDLL.props b/include/ResourceDLL.props deleted file mode 100644 index 9db83be..0000000 --- a/include/ResourceDLL.props +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - true - - - - Windows - true - Default - - - false - - - AFX_RESOURCE_DLL;%(PreprocessorDefinitions) - - - - diff --git a/lib/Events/build/Events.vcxproj b/lib/Events/build/Events.vcxproj index e1fdc9a..2f8ea46 100644 --- a/lib/Events/build/Events.vcxproj +++ b/lib/Events/build/Events.vcxproj @@ -81,7 +81,6 @@ - @@ -96,15 +95,57 @@ temp\Events.$(Platform).$(Configuration).$(PlatformToolset);%(AdditionalIncludeDirectories) - AFX_TARG_NEU;AFX_TARG_ENU;%(PreprocessorDefinitions) + AFX_TARG_NEU;AFX_TARG_ENU;AFX_RESOURCE_DLL;%(PreprocessorDefinitions) - - - - - - + + + $(IntDir);..\..\WinStd\include;%(AdditionalIncludeDirectories) + + + ..\src\Events.def + + + + + $(IntDir);..\..\WinStd\include;%(AdditionalIncludeDirectories) + + + ..\src\Events.def + + + + + $(IntDir);..\..\WinStd\include;%(AdditionalIncludeDirectories) + + + ..\src\Events.def + + + + + $(IntDir);..\..\WinStd\include;%(AdditionalIncludeDirectories) + + + ..\src\Events.def + + + + + $(IntDir);..\..\WinStd\include;%(AdditionalIncludeDirectories) + + + ..\src\Events.def + + + + + $(IntDir);..\..\WinStd\include;%(AdditionalIncludeDirectories) + + + ..\src\Events.def + + Document @@ -117,6 +158,23 @@ + + + Create + + + + + + + + + {47399d91-7eb9-41de-b521-514ba5db0c43} + + + + + diff --git a/lib/Events/build/Events.vcxproj.filters b/lib/Events/build/Events.vcxproj.filters index 7aad60d..46aef80 100644 --- a/lib/Events/build/Events.vcxproj.filters +++ b/lib/Events/build/Events.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -24,4 +24,22 @@ Resource Files + + + Source Files + + + Source Files + + + + + Header Files + + + + + Source Files + + \ No newline at end of file diff --git a/lib/Events/src/Events.def b/lib/Events/src/Events.def new file mode 100644 index 0000000000000000000000000000000000000000..340550baeb0b6b79962843fbc379182b0c82e06b GIT binary patch literal 1692 zcmbW1T~8B16o$_&CjN)H)I?i95-)_9V5QZh0--I~%YJO>#}f*c7Yc zKO?-g55(%(Exv~~th}1phh$X5U<>9I8A7#Q+XPHPbIIBLIDITK`)RGM5jXj2yE@{6 zc!_5yJZ5Rvo+Gi3#J^$hih0If#w^>pV=0}RDW_$HPpD)yw>Dlms|#jXFNmXj^ zds9#~d9O2dK@d3^IED=;!Mf~3+!L^-tTKB=w8ZOE4XXT?&qZ|}>^*zUYMM9|L7X-5 zya2seja^V&sj|-clC}62w=1{Ymiw`K*w$UKC3UGgu}e!w68p8RP^!NH^+>Vm%M=a* zT&WKW=9Tjl71ONq%)W6GzSx2<+%GmFx;tPekpnQpQeIHwjde&r1 zITP}z=iIp}oh@~uiQu~(kf#HNm}}oYgQ@Shge2;@>+3^o2MlT92ruE43WaruMTaW- zzS8c9-DB(qc(t$#d_`?VE}J->rtw9EPxnpA4b+{$&cu6B{Nqg+JBNY(L^fB>mbxkR z#=EYYl=@msPWa96(#!Zz&rd<#tX;QRx9ObNb(golZ~>O}`eoRct}1CvnD?lqWUP6; z+Kg)-En;TqrG. +*/ + +#include "PCH.h" diff --git a/lib/Events/src/PCH.h b/lib/Events/src/PCH.h new file mode 100644 index 0000000..b42ce38 --- /dev/null +++ b/lib/Events/src/PCH.h @@ -0,0 +1,31 @@ +/* + Copyright 2020-2020 Amebis + Copyright 2016 GÉANT + + This file is part of GÉANTLink. + + GÉANTLink is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + GÉANTLink is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GÉANTLink. If not, see . +*/ + +#pragma once + +#include "../../../include/Version.h" + +#include +#include +#include + +#include + +#include diff --git a/lib/Events/src/Register.cpp b/lib/Events/src/Register.cpp new file mode 100644 index 0000000..3143c03 --- /dev/null +++ b/lib/Events/src/Register.cpp @@ -0,0 +1,164 @@ +/* + Copyright 2020-2020 Amebis + Copyright 2016 GÉANT + + This file is part of GÉANTLink. + + GÉANTLink is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + GÉANTLink is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GÉANTLink. If not, see . +*/ + +#include "PCH.h" + +using namespace std; +using namespace winstd; + +HINSTANCE g_hInstance; + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ LPCTSTR sValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, reinterpret_cast(sValue), (DWORD)((_tcslen(sValue) + 1) * sizeof(tstring::value_type))); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ const tstring &sValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, reinterpret_cast(sValue.c_str()), (DWORD)((sValue.length() + 1) * sizeof(tstring::value_type))); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +static void set_value(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpValueName, _In_ DWORD dwValue) +{ + LSTATUS s = RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, reinterpret_cast(&dwValue), sizeof(dwValue)); + if (s != ERROR_SUCCESS) + throw win_runtime_error(s, "RegSetValueEx failed."); +} + + +BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved) +{ + UNREFERENCED_PARAMETER(lpvReserved); + + if (fdwReason == DLL_PROCESS_ATTACH) { +#ifdef _DEBUG + //Sleep(10000); +#endif + g_hInstance = hinstDLL; + } else if (fdwReason == DLL_PROCESS_DETACH) + assert(!_CrtDumpMemoryLeaks()); + + return TRUE; +} + + +/// +/// Registers event source in Windows registry +/// +/// \returns S_OK if successful; E_FAIL otherwise +/// +STDAPI DllRegisterServer() +{ + try { + tstring sz, event_provider_name(_T(VENDOR_NAME_STR) _T("-") _T(PRODUCT_NAME_STR) _T("-EAPMethod")); + tstring_guid event_provider_guid(EAPMETHOD_TRACE_EVENT_PROVIDER); + + // Register event channels. + reg_key key_channels, key_channels_operational, key_channels_analytic; + if (!key_channels.open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WINEVT\\Channels"), 0, KEY_CREATE_SUB_KEY)) throw win_runtime_error(); + sprintf(sz, _T("%s/Operational"), event_provider_name.c_str()); + if (!key_channels_operational.create(key_channels, sz.c_str(), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + set_value(key_channels_operational, _T("OwningPublisher") , event_provider_guid); + set_value(key_channels_operational, _T("Enabled") , (DWORD)0); + set_value(key_channels_operational, _T("Isolation") , (DWORD)0); + set_value(key_channels_operational, _T("ChannelAccess") , _T("O:BAG:SYD:(A;;0xf0007;;;SY)(A;;0x7;;;BA)(A;;0x7;;;SO)(A;;0x3;;;IU)(A;;0x3;;;SU)(A;;0x3;;;S-1-5-3)(A;;0x3;;;S-1-5-33)(A;;0x1;;;S-1-5-32-573)")); + set_value(key_channels_operational, _T("MaxSize") , (DWORD)1048576); + set_value(key_channels_operational, _T("MaxSizeUpper") , (DWORD)0); + set_value(key_channels_operational, _T("Retention") , (DWORD)0); + set_value(key_channels_operational, _T("AutoBackupLogFiles"), (DWORD)0); + set_value(key_channels_operational, _T("Type") , (DWORD)1); + sprintf(sz, _T("%s/Analytic"), event_provider_name.c_str()); + if (!key_channels_analytic.create(key_channels, sz.c_str(), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + set_value(key_channels_analytic, _T("OwningPublisher"), event_provider_guid); + set_value(key_channels_analytic, _T("Enabled") , (DWORD)0); + set_value(key_channels_analytic, _T("Isolation") , (DWORD)0); + set_value(key_channels_analytic, _T("ChannelAccess") , _T("O:BAG:SYD:(A;;0xf0007;;;SY)(A;;0x7;;;BA)(A;;0x7;;;SO)(A;;0x3;;;IU)(A;;0x3;;;SU)(A;;0x3;;;S-1-5-3)(A;;0x3;;;S-1-5-33)(A;;0x1;;;S-1-5-32-573)")); + set_value(key_channels_analytic, _T("MaxSize") , (DWORD)1048576); + set_value(key_channels_analytic, _T("MaxSizeUpper") , (DWORD)0); + set_value(key_channels_analytic, _T("Retention") , (DWORD)4294967295); + set_value(key_channels_analytic, _T("Type") , (DWORD)2); + + // Register event publishers. + reg_key key_publishers, key_event_source; + if (!key_publishers.open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WINEVT\\Publishers"), 0, KEY_CREATE_SUB_KEY)) throw win_runtime_error(); + if (!key_event_source.create(key_publishers, event_provider_guid.c_str(), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + set_value(key_event_source, NULL , event_provider_name); + if (!GetModuleFileName(g_hInstance, sz)) throw win_runtime_error("GetModuleFileName failed."); + set_value(key_event_source, _T("MessageFileName") , sz); + set_value(key_event_source, _T("ResourceFileName"), sz); + set_value(key_event_source, _T("Enabled") , (DWORD)1); + + // Bind channels and publishers. + reg_key key_channel_refs, key_channel_refs_operational, key_channel_refs_analytic; + if (!key_channel_refs.create(key_event_source, _T("ChannelReferences"), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + if (!key_channel_refs_operational.create(key_channel_refs, _T("0"), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + sprintf(sz, _T("%s/Operational"), event_provider_name.c_str()); + set_value(key_channel_refs_operational, NULL , sz); + set_value(key_channel_refs_operational, _T("Id") , (DWORD)16); + set_value(key_channel_refs_operational, _T("Flags"), (DWORD)0); + if (!key_channel_refs_analytic.create(key_channel_refs, _T("1"), NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE)) throw win_runtime_error(); + sprintf(sz, _T("%s/Analytic"), event_provider_name.c_str()); + set_value(key_channel_refs_analytic, NULL , sz); + set_value(key_channel_refs_analytic, _T("Id") , (DWORD)17); + set_value(key_channel_refs_analytic, _T("Flags"), (DWORD)0); + set_value(key_channel_refs, _T("Count"), (DWORD)2); + + return S_OK; + } catch(win_runtime_error &err) { + OutputDebugStr(_T("%hs (error %u)\n"), err.what(), err.number()); + return E_FAIL; + } catch(...) { + OutputDebugStr(_T("Registering DLL failed.\n")); + return E_FAIL; + } +} + + +/// +/// Unregisters event source from Windows registry +/// +/// \returns Always S_OK +/// +STDAPI DllUnregisterServer() +{ + // Unregister event publishers. + try { + reg_key key_publishers; + if (!key_publishers.open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WINEVT\\Publishers"), 0, KEY_READ)) throw win_runtime_error(); + key_publishers.delete_subkey(tstring_guid(EAPMETHOD_TRACE_EVENT_PROVIDER).c_str()); + } catch(...) {} + + // Unregister event channels. + try { + reg_key key_channels; + if (!key_channels.open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WINEVT\\Channels"), 0, KEY_READ)) throw win_runtime_error(); + key_channels.delete_subkey(_T(VENDOR_NAME_STR) _T("-") _T(PRODUCT_NAME_STR) _T("-EAPMethod/Operational")); + key_channels.delete_subkey(_T(VENDOR_NAME_STR) _T("-") _T(PRODUCT_NAME_STR) _T("-EAPMethod/Analytic")); + } catch(...) {} + + return S_OK; +} diff --git a/lib/WinStd b/lib/WinStd index 7c5f20d..85075cd 160000 --- a/lib/WinStd +++ b/lib/WinStd @@ -1 +1 @@ -Subproject commit 7c5f20d7563a6ab966164c0cdb48f8e1f64c4742 +Subproject commit 85075cd419df9488e13f5ebc8aaaf1d65c72ed66