66306902fd1f4f73000aa20f60d52c464344edf6
[reactos.git] / reactos / dll / shellext / acppage / ACPPage.cpp
1 /*
2 * Copyright 2015 Mark Jansen
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "precomp.h"
20
21 #include <shellutils.h>
22
23 HMODULE g_hModule = NULL;
24 LONG g_ModuleRefCnt = 0;
25
26 class CLayerUIPropPageModule : public CComModule
27 {
28 public:
29 void Term()
30 {
31 CComModule::Term();
32 }
33 };
34
35 BEGIN_OBJECT_MAP(ObjectMap)
36 OBJECT_ENTRY(CLSID_CLayerUIPropPage, CLayerUIPropPage)
37 END_OBJECT_MAP()
38
39 CLayerUIPropPageModule gModule;
40
41 EXTERN_C
42 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
43 {
44 switch (dwReason)
45 {
46 case DLL_PROCESS_ATTACH:
47 DisableThreadLibraryCalls(hInstance);
48 g_hModule = hInstance;
49 gModule.Init(ObjectMap, hInstance, NULL);
50 break;
51 }
52
53 return(TRUE);
54 }
55
56 STDAPI DllCanUnloadNow()
57 {
58 if (g_ModuleRefCnt)
59 return S_FALSE;
60 return gModule.DllCanUnloadNow();
61 }
62
63 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
64 {
65 return gModule.DllGetClassObject(rclsid, riid, ppv);
66 }
67
68 STDAPI DllRegisterServer()
69 {
70 HRESULT hr;
71
72 hr = gModule.DllRegisterServer(FALSE);
73 if (FAILED(hr))
74 return hr;
75
76 hr = gModule.UpdateRegistryFromResource(IDR_ACPPAGE, TRUE, NULL);
77 if (FAILED(hr))
78 return hr;
79
80 return S_OK;
81 }
82
83 STDAPI DllUnregisterServer()
84 {
85 HRESULT hr;
86
87 hr = gModule.DllUnregisterServer(FALSE);
88 if (FAILED(hr))
89 return hr;
90
91 hr = gModule.UpdateRegistryFromResource(IDR_ACPPAGE, FALSE, NULL);
92 if (FAILED(hr))
93 return hr;
94
95 return S_OK;
96 }
97
98 struct CCoInit
99 {
100 CCoInit() { hres = CoInitialize(NULL); }
101 ~CCoInit() { if (SUCCEEDED(hres)) { CoUninitialize(); } }
102 HRESULT hres;
103 };
104
105 EXTERN_C
106 BOOL WINAPI GetExeFromLnk(PCWSTR pszLnk, PWSTR pszExe, size_t cchSize)
107 {
108 CCoInit init;
109 if (FAILED(init.hres))
110 return FALSE;
111
112 CComPtr<IShellLinkW> spShellLink;
113 if (FAILED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IShellLinkW, &spShellLink))))
114 return FALSE;
115
116 CComPtr<IPersistFile> spPersistFile;
117 if (FAILED(spShellLink->QueryInterface(IID_PPV_ARG(IPersistFile, &spPersistFile))))
118 return FALSE;
119
120 if (FAILED(spPersistFile->Load(pszLnk, STGM_READ)) || FAILED(spShellLink->Resolve(NULL, SLR_NO_UI | SLR_NOUPDATE | SLR_NOSEARCH)))
121 return FALSE;
122
123 return SUCCEEDED(spShellLink->GetPath(pszExe, cchSize, NULL, SLGP_RAWPATH));
124 }