]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/library/com_custom.cpp
791d8df1be37034cf1900679f58239de98923aa5
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / library / com_custom.cpp
1 /*******************************************************************************
2  * Copyright (c) 2000, 2019 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *    IBM Corporation - initial API and implementation
13  *    Paul Pazderski - Bug 547634: PathToPIDL
14  *******************************************************************************/
15
16 #include "swt.h"
17 #include "com_structs.h"
18 #include "com_stats.h"
19 #include <Shlobj.h>
20
21 #define COM_NATIVE(func) Java_org_eclipse_swt_internal_ole_win32_COM_##func
22
23 class CFileSysBindData : public IFileSystemBindData
24 {
25 public:
26         CFileSysBindData() : refCount(1)
27         {
28                 ZeroMemory(&findData, sizeof(findData));
29         }
30
31         IFACEMETHODIMP QueryInterface(REFIID riid, void **ppv)
32         {
33                 *ppv = nullptr;
34                 HRESULT hr = E_NOINTERFACE;
35                 if (riid == IID_IUnknown || riid == IID_IFileSystemBindData) {
36                         *ppv = static_cast<IFileSystemBindData*>(this);
37                         AddRef();
38                         hr = S_OK;
39                 }
40                 return hr;
41         }
42
43         IFACEMETHODIMP_(ULONG) AddRef()
44         {
45                 return InterlockedIncrement(&refCount);
46         }
47
48         IFACEMETHODIMP_(ULONG) Release()
49         {
50                 long rc = InterlockedDecrement(&refCount);
51                 if (!rc)
52                         delete this;
53                 return rc;
54         }
55
56         IFACEMETHODIMP SetFindData(const WIN32_FIND_DATAW *pfd)
57         {
58                 findData = *pfd;
59                 return S_OK;
60         }
61
62         IFACEMETHODIMP GetFindData(WIN32_FIND_DATAW *pfd)
63         {
64                 *pfd = findData;
65                 return S_OK;
66         }
67
68 private:
69         long refCount;
70         WIN32_FIND_DATAW findData;
71 };
72
73 /*
74  * An extended version of SHParseDisplayName which use bind context to support
75  * creation of simple PIDLs in case the normal PIDL creation failed.
76  * (most likley due to non existing file/directory)
77  */
78 extern "C" HRESULT PathToPIDL(PCWSTR pszName, PIDLIST_ABSOLUTE *ppidl)
79 {
80         if (!ppidl) return E_FAIL;
81         *ppidl = nullptr;
82         
83         SFGAOF sfgao = 0;
84         HRESULT hr = SHParseDisplayName(pszName, nullptr, ppidl, sfgao, &sfgao);
85         if (hr == S_OK) return hr;
86
87         IFileSystemBindData *pfsbd = new CFileSysBindData();
88         if (!pfsbd) return E_OUTOFMEMORY;
89
90         WIN32_FIND_DATAW data = {};
91         pfsbd->SetFindData(&data);
92
93         IBindCtx* pbc;
94
95         hr = CreateBindCtx(0, &pbc);
96         if (hr == S_OK)
97         {
98                 BIND_OPTS bo = { sizeof(bo), 0, STGM_CREATE, 0 };
99                 hr = pbc->SetBindOptions(&bo);
100                 if (hr == S_OK)
101                 {
102                         hr = pbc->RegisterObjectParam(STR_FILE_SYS_BIND_DATA, pfsbd);
103                         if (hr == S_OK)
104                         {
105                                 sfgao = 0;
106                                 hr = SHParseDisplayName(pszName, pbc, ppidl, sfgao, &sfgao);
107                         }
108                 }
109                 pbc->Release();
110         }
111         pfsbd->Release();
112         return hr;
113 }