xref: /reactos/dll/shellext/acppage/ACPPage.cpp (revision da5f10af)
1 /*
2  * PROJECT:     ReactOS Compatibility Layer Shell Extension
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     acppage entrypoint
5  * COPYRIGHT:   Copyright 2015-2018 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 #include <shellutils.h>
11 
12 HMODULE g_hModule = NULL;
13 LONG g_ModuleRefCnt = 0;
14 
15 class CLayerUIPropPageModule : public CComModule
16 {
17 public:
18 };
19 
20 BEGIN_OBJECT_MAP(ObjectMap)
21     OBJECT_ENTRY(CLSID_CLayerUIPropPage, CLayerUIPropPage)
22 END_OBJECT_MAP()
23 
24 CLayerUIPropPageModule gModule;
25 
26 EXTERN_C
27 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
28 {
29     switch (dwReason)
30     {
31     case DLL_PROCESS_ATTACH:
32         DisableThreadLibraryCalls(hInstance);
33         g_hModule = hInstance;
34         gModule.Init(ObjectMap, hInstance, NULL);
35         break;
36     case DLL_PROCESS_DETACH:
37         gModule.Term();
38         break;
39     }
40 
41     return(TRUE);
42 }
43 
44 STDAPI DllCanUnloadNow()
45 {
46     if (g_ModuleRefCnt)
47         return S_FALSE;
48     return gModule.DllCanUnloadNow();
49 }
50 
51 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
52 {
53     return gModule.DllGetClassObject(rclsid, riid, ppv);
54 }
55 
56 STDAPI DllRegisterServer()
57 {
58     HRESULT hr;
59 
60     hr = gModule.DllRegisterServer(FALSE);
61     if (FAILED(hr))
62         return hr;
63 
64     return S_OK;
65 }
66 
67 STDAPI DllUnregisterServer()
68 {
69     HRESULT hr;
70 
71     hr = gModule.DllUnregisterServer(FALSE);
72     if (FAILED(hr))
73         return hr;
74 
75     return S_OK;
76 }
77 
78 struct CCoInit
79 {
80     CCoInit() { hres = CoInitialize(NULL); }
81     ~CCoInit() { if (SUCCEEDED(hres)) { CoUninitialize(); } }
82     HRESULT hres;
83 };
84 
85 EXTERN_C
86 inline ULONG
87 Win32DbgPrint(const char *filename, int line, const char *lpFormat, ...)
88 {
89     char Buffer[512];
90     char* Current = Buffer;
91     size_t Length = _countof(Buffer);
92     const char* fname = strrchr(filename, '\\');
93     if (fname == NULL)
94     {
95         fname = strrchr(filename, '/');
96         if (fname != NULL)
97             fname++;
98     }
99     else
100         fname++;
101 
102     if (fname == NULL)
103         fname = filename;
104 
105     StringCchPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, "%s:%d: ", fname, line);
106     va_list ArgList;
107     va_start(ArgList, lpFormat);
108     StringCchVPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, lpFormat, ArgList);
109     va_end(ArgList);
110     OutputDebugStringA(Buffer);
111     return 0;
112 }
113 
114 
115 EXTERN_C
116 BOOL WINAPI GetExeFromLnk(PCWSTR pszLnk, PWSTR pszExe, size_t cchSize)
117 {
118     CCoInit init;
119     if (FAILED_UNEXPECTEDLY(init.hres))
120         return FALSE;
121 
122     CComPtr<IShellLinkW> spShellLink;
123     if (FAILED_UNEXPECTEDLY(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IShellLinkW, &spShellLink))))
124         return FALSE;
125 
126     CComPtr<IPersistFile> spPersistFile;
127     if (FAILED_UNEXPECTEDLY(spShellLink->QueryInterface(IID_PPV_ARG(IPersistFile, &spPersistFile))))
128         return FALSE;
129 
130     if (FAILED_UNEXPECTEDLY(spPersistFile->Load(pszLnk, STGM_READ)) || FAILED_UNEXPECTEDLY(spShellLink->Resolve(NULL, SLR_NO_UI | SLR_NOUPDATE | SLR_NOSEARCH)))
131         return FALSE;
132 
133     return !FAILED_UNEXPECTEDLY(spShellLink->GetPath(pszExe, cchSize, NULL, SLGP_RAWPATH));
134 }
135