FreeCAD
215 строк · 6.3 Кб
1/***************************************************************************
2* Copyright (c) 2011 Werner Mayer <wmayer@users.sourceforge.net> *
3* *
4* This file is part of the FreeCAD CAx development system. *
5* *
6* This library is free software; you can redistribute it and/or *
7* modify it under the terms of the GNU Library General Public *
8* License as published by the Free Software Foundation; either *
9* version 2 of the License, or (at your option) any later version. *
10* *
11* This library is distributed in the hope that it will be useful, *
12* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14* GNU Library General Public License for more details. *
15* *
16* You should have received a copy of the GNU Library General Public *
17* License along with this library; see the file COPYING.LIB. If not, *
18* write to the Free Software Foundation, Inc., 59 Temple Place, *
19* Suite 330, Boston, MA 02111-1307, USA *
20* *
21***************************************************************************/
22
23
24#define INITGUID25#include "Common.h"26
27HINSTANCE g_hinstDll = NULL;28LONG g_cRef = 0;29
30
31typedef struct _REGKEY_DELETEKEY32{
33HKEY hKey;34LPCWSTR lpszSubKey;35} REGKEY_DELETEKEY;36
37
38typedef struct _REGKEY_SUBKEY_AND_VALUE39{
40HKEY hKey;41LPCWSTR lpszSubKey;42LPCWSTR lpszValue;43DWORD dwType;44DWORD_PTR dwData;45;46} REGKEY_SUBKEY_AND_VALUE;47
48STDAPI CreateRegistryKeys(REGKEY_SUBKEY_AND_VALUE* aKeys, ULONG cKeys);49STDAPI DeleteRegistryKeys(REGKEY_DELETEKEY* aKeys, ULONG cKeys);50
51
52BOOL APIENTRY DllMain(HINSTANCE hinstDll, DWORD dwReason, LPVOID pvReserved)53{
54switch (dwReason) {55case DLL_PROCESS_ATTACH:56g_hinstDll = hinstDll;57break;58}59return TRUE;60}
61
62STDAPI_(HINSTANCE) DllInstance()63{
64return g_hinstDll;65}
66
67STDAPI DllCanUnloadNow()68{
69return g_cRef ? S_FALSE : S_OK;70}
71
72STDAPI_(ULONG) DllAddRef()73{
74LONG cRef = InterlockedIncrement(&g_cRef);75return cRef;76}
77
78STDAPI_(ULONG) DllRelease()79{
80LONG cRef = InterlockedDecrement(&g_cRef);81if (0 > cRef) {82cRef = 0;83}84return cRef;85}
86
87STDAPI DllRegisterServer()88{
89// This tells the shell to invalidate the thumbnail cache. This is important because any90// .recipe files viewed before registering this handler would otherwise show cached blank91// thumbnails.92SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);93
94WCHAR szModule[MAX_PATH];95
96ZeroMemory(szModule, sizeof(szModule));97GetModuleFileName(g_hinstDll, szModule, ARRAYSIZE(szModule));98
99// uncomment the following100REGKEY_SUBKEY_AND_VALUE keys[] = {101{HKEY_CLASSES_ROOT,102L"CLSID\\" szCLSID_SampleThumbnailProvider,103NULL,104REG_SZ,105(DWORD_PTR)L"FCStd Thumbnail Provider"},106#if 1107//{HKEY_CLASSES_ROOT, L"CLSID\\DisableProcessIsolation", NULL, REG_DWORD, (DWORD) 1},108{HKEY_CLASSES_ROOT,109L"CLSID\\" szCLSID_SampleThumbnailProvider,110L"DisableProcessIsolation",111REG_DWORD,112(DWORD)1},113#endif114{HKEY_CLASSES_ROOT,115L"CLSID\\" szCLSID_SampleThumbnailProvider L"\\InprocServer32",116NULL,117REG_SZ,118(DWORD_PTR)szModule},119{HKEY_CLASSES_ROOT,120L"CLSID\\" szCLSID_SampleThumbnailProvider L"\\InprocServer32",121L"ThreadingModel",122REG_SZ,123(DWORD_PTR)L"Apartment"},124//{HKEY_CLASSES_ROOT, L".FCStd\\shellex", L"Trick only here to create shellex when not125// existing",REG_DWORD, 1},126{HKEY_CLASSES_ROOT,127L".FCStd\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}",128NULL,129REG_SZ,130(DWORD_PTR)szCLSID_SampleThumbnailProvider},131{HKEY_CLASSES_ROOT,132L".FCBak\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}",133NULL,134REG_SZ,135(DWORD_PTR)szCLSID_SampleThumbnailProvider}136};137
138return CreateRegistryKeys(keys, ARRAYSIZE(keys));139}
140
141STDAPI DllUnregisterServer()142{
143REGKEY_DELETEKEY keys[] = {{HKEY_CLASSES_ROOT, L"CLSID\\" szCLSID_SampleThumbnailProvider}};144return DeleteRegistryKeys(keys, ARRAYSIZE(keys));145}
146
147
148STDAPI CreateRegistryKey(REGKEY_SUBKEY_AND_VALUE* pKey)149{
150size_t cbData;151LPVOID pvData = NULL;152HRESULT hr = S_OK;153
154switch (pKey->dwType) {155case REG_DWORD:156pvData = (LPVOID)(LPDWORD)&pKey->dwData;157cbData = sizeof(DWORD);158break;159
160case REG_SZ:161case REG_EXPAND_SZ:162hr = StringCbLength((LPCWSTR)pKey->dwData, STRSAFE_MAX_CCH, &cbData);163if (SUCCEEDED(hr)) {164pvData = (LPVOID)(LPCWSTR)pKey->dwData;165cbData += sizeof(WCHAR);166}167break;168
169default:170hr = E_INVALIDARG;171}172
173if (SUCCEEDED(hr)) {174LSTATUS status = SHSetValue(pKey->hKey,175pKey->lpszSubKey,176pKey->lpszValue,177pKey->dwType,178pvData,179(DWORD)cbData);180if (NOERROR != status) {181hr = HRESULT_FROM_WIN32(status);182}183}184
185return hr;186}
187
188
189STDAPI CreateRegistryKeys(REGKEY_SUBKEY_AND_VALUE* aKeys, ULONG cKeys)190{
191HRESULT hr = S_OK;192
193for (ULONG iKey = 0; iKey < cKeys; iKey++) {194HRESULT hrTemp = CreateRegistryKey(&aKeys[iKey]);195if (FAILED(hrTemp)) {196hr = hrTemp;197}198}199return hr;200}
201
202
203STDAPI DeleteRegistryKeys(REGKEY_DELETEKEY* aKeys, ULONG cKeys)204{
205HRESULT hr = S_OK;206LSTATUS status;207
208for (ULONG iKey = 0; iKey < cKeys; iKey++) {209status = RegDeleteTree(aKeys[iKey].hKey, aKeys[iKey].lpszSubKey);210if (NOERROR != status) {211hr = HRESULT_FROM_WIN32(status);212}213}214return hr;215}
216