#include #include #include "guid.h" #include "ClassFactory.h" extern HMODULE g_hModule; extern long g_cRef; static HRESULT GuidToString(REFGUID guid, wchar_t* buffer, DWORD cchBuffer) { if (StringFromGUID2(guid, buffer, cchBuffer) == 0) return E_FAIL; return S_OK; } static HRESULT SetStringValue(HKEY key, const wchar_t* name, const wchar_t* value) { const DWORD bytes = (DWORD)((wcslen(value) + 1) * sizeof(wchar_t)); return HRESULT_FROM_WIN32(RegSetValueExW(key, name, 0, REG_SZ, reinterpret_cast(value), bytes)); } STDAPI DllCanUnloadNow() { return g_cRef == 0 ? S_OK : S_FALSE; } STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void** ppv) { if (rclsid != CLSID_DuoRdpCredentialProvider && rclsid != CLSID_DuoRdpCredentialFilter) return CLASS_E_CLASSNOTAVAILABLE; DuoClassFactory* factory = new DuoClassFactory(rclsid); if (!factory) return E_OUTOFMEMORY; HRESULT hr = factory->QueryInterface(riid, ppv); factory->Release(); return hr; } static HRESULT RegisterComClass(REFCLSID classId, const wchar_t* name, const wchar_t* modulePath) { wchar_t clsid[64] = {}; HRESULT hr = GuidToString(classId, clsid, ARRAYSIZE(clsid)); if (FAILED(hr)) return hr; wchar_t clsidKeyPath[256] = {}; hr = StringCchPrintfW(clsidKeyPath, ARRAYSIZE(clsidKeyPath), L"CLSID\\%s", clsid); if (FAILED(hr)) return hr; HKEY clsidKey = nullptr; LSTATUS status = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsidKeyPath, 0, nullptr, 0, KEY_WRITE, nullptr, &clsidKey, nullptr); if (status != ERROR_SUCCESS) return HRESULT_FROM_WIN32(status); hr = SetStringValue(clsidKey, nullptr, name); HKEY inprocKey = nullptr; if (SUCCEEDED(hr)) { status = RegCreateKeyExW(clsidKey, L"InprocServer32", 0, nullptr, 0, KEY_WRITE, nullptr, &inprocKey, nullptr); if (status != ERROR_SUCCESS) hr = HRESULT_FROM_WIN32(status); } if (SUCCEEDED(hr)) hr = SetStringValue(inprocKey, nullptr, modulePath); if (SUCCEEDED(hr)) hr = SetStringValue(inprocKey, L"ThreadingModel", L"Apartment"); if (inprocKey) RegCloseKey(inprocKey); RegCloseKey(clsidKey); return hr; } STDAPI DllRegisterServer() { wchar_t modulePath[MAX_PATH] = {}; if (!GetModuleFileNameW(g_hModule, modulePath, ARRAYSIZE(modulePath))) { return HRESULT_FROM_WIN32(GetLastError()); } HRESULT hr = RegisterComClass(CLSID_DuoRdpCredentialProvider, L"DuoRdp Credential Provider", modulePath); if (FAILED(hr)) return hr; return RegisterComClass(CLSID_DuoRdpCredentialFilter, L"DuoRdp Credential Filter", modulePath); } STDAPI DllUnregisterServer() { wchar_t providerClsid[64] = {}; wchar_t filterClsid[64] = {}; wchar_t clsidKeyPath[256] = {}; if (SUCCEEDED(GuidToString(CLSID_DuoRdpCredentialProvider, providerClsid, ARRAYSIZE(providerClsid))) && SUCCEEDED(StringCchPrintfW(clsidKeyPath, ARRAYSIZE(clsidKeyPath), L"CLSID\\%s", providerClsid))) { RegDeleteTreeW(HKEY_CLASSES_ROOT, clsidKeyPath); } if (SUCCEEDED(GuidToString(CLSID_DuoRdpCredentialFilter, filterClsid, ARRAYSIZE(filterClsid))) && SUCCEEDED(StringCchPrintfW(clsidKeyPath, ARRAYSIZE(clsidKeyPath), L"CLSID\\%s", filterClsid))) { RegDeleteTreeW(HKEY_CLASSES_ROOT, clsidKeyPath); } return S_OK; }