xref: /reactos/dll/shellext/mydocs/mydocs.cpp (revision 11345aed)
1 /*
2  * PROJECT:     mydocs
3  * LICENSE:     LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
4  * PURPOSE:     MyDocs implementation
5  * COPYRIGHT:   Copyright 2019 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6  */
7 
8 #include "precomp.hpp"
9 
10 WINE_DEFAULT_DEBUG_CHANNEL(mydocs);
11 
12 BEGIN_OBJECT_MAP(ObjectMap)
13     OBJECT_ENTRY(CLSID_MyDocsDropHandler, CMyDocsDropHandler)
14 END_OBJECT_MAP()
15 
16 CComModule gModule;
17 LONG g_ModuleRefCnt = 0;
18 
19 static BOOL
20 CreateEmptyFile(LPCWSTR pszFile)
21 {
22     HANDLE hFile;
23     hFile = CreateFileW(pszFile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
24                         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
25     if (hFile != INVALID_HANDLE_VALUE)
26     {
27         CloseHandle(hFile);
28         return TRUE;
29     }
30     return FALSE;
31 }
32 
33 static HRESULT
34 CreateSendToMyDocuments(LPCWSTR pszSendTo)
35 {
36     WCHAR szTarget[MAX_PATH], szSendToFile[MAX_PATH];
37 
38     SHGetSpecialFolderPathW(NULL, szTarget, CSIDL_MYDOCUMENTS, FALSE);
39 
40     StringCbCopyW(szSendToFile, sizeof(szSendToFile), pszSendTo);
41     PathAppendW(szSendToFile, PathFindFileNameW(szTarget));
42     StringCbCatW(szSendToFile, sizeof(szSendToFile), L".mydocs");
43 
44     if (!CreateEmptyFile(szSendToFile))
45     {
46         ERR("CreateEmptyFile(%S, %S) failed!\n", szSendToFile, szTarget);
47         return E_FAIL;
48     }
49 
50     return S_OK;
51 }
52 
53 static HRESULT
54 GetDefaultUserSendTo(LPWSTR pszPath)
55 {
56     return SHGetFolderPathW(NULL, CSIDL_SENDTO, INVALID_HANDLE_VALUE,
57                             SHGFP_TYPE_DEFAULT, pszPath);
58 }
59 
60 STDAPI DllCanUnloadNow(void)
61 {
62     if (g_ModuleRefCnt)
63         return S_FALSE;
64     return gModule.DllCanUnloadNow();
65 }
66 
67 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
68 {
69     TRACE("CLSID:%s,IID:%s\n", wine_dbgstr_guid(&rclsid), wine_dbgstr_guid(&riid));
70 
71     HRESULT hr = gModule.DllGetClassObject(rclsid, riid, ppv);
72 
73     TRACE("-- pointer to class factory: %p\n", *ppv);
74 
75     return hr;
76 }
77 
78 STDAPI DllRegisterServer(void)
79 {
80     HRESULT hr = gModule.DllRegisterServer(FALSE);
81     if (FAILED_UNEXPECTEDLY(hr))
82         return hr;
83 
84     WCHAR szSendTo[MAX_PATH];
85     hr = GetDefaultUserSendTo(szSendTo);
86     if (SUCCEEDED(hr))
87         CreateSendToMyDocuments(szSendTo);
88 
89     return S_OK;
90 }
91 
92 STDAPI DllUnregisterServer(void)
93 {
94     HRESULT hr = gModule.DllUnregisterServer(FALSE);
95     if (FAILED_UNEXPECTEDLY(hr))
96         return hr;
97 
98     return S_OK;
99 }
100 
101 STDAPI_(BOOL) DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID fImpLoad)
102 {
103     TRACE("%p 0x%x %p\n", hInstance, dwReason, fImpLoad);
104     if (dwReason == DLL_PROCESS_ATTACH)
105     {
106         gModule.Init(ObjectMap, hInstance, NULL);
107         DisableThreadLibraryCalls(hInstance);
108     }
109     else if (dwReason == DLL_PROCESS_DETACH)
110     {
111         gModule.Term();
112     }
113     return TRUE;
114 }
115