1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2018 Nathan Osman
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  **/
24 
25 #include <windows.h>
26 
27 #include "classfactory.h"
28 #include "contextmenu.h"
29 #include "nitroshell.h"
30 
ClassFactory()31 ClassFactory::ClassFactory()
32     : mRefCount(1)
33 {
34 }
35 
QueryInterface(REFIID riid,LPVOID * ppvObject)36 STDMETHODIMP ClassFactory::QueryInterface(REFIID riid, LPVOID *ppvObject)
37 {
38     if (!ppvObject) {
39         return E_INVALIDARG;
40     }
41 
42     *ppvObject = NULL;
43 
44     if (IsEqualIID(riid, IID_IUnknown)) {
45         OutputDebugString(TEXT("ClassFactory queried for IUnknown"));
46         *ppvObject = this;
47     } else if (IsEqualIID(riid, IID_IClassFactory)) {
48         OutputDebugString(TEXT("ClassFactory queried for IClassFactory"));
49         *ppvObject = (IClassFactory*) this;
50     } else {
51         return E_NOINTERFACE;
52     }
53 
54     AddRef();
55     return S_OK;
56 }
57 
STDMETHODIMP_(ULONG)58 STDMETHODIMP_(ULONG) ClassFactory::AddRef()
59 {
60     InterlockedIncrement(&mRefCount);
61     return mRefCount;
62 }
63 
STDMETHODIMP_(ULONG)64 STDMETHODIMP_(ULONG) ClassFactory::Release()
65 {
66     ULONG refCount = InterlockedDecrement(&mRefCount);
67     if (!mRefCount) {
68         delete this;
69     }
70     return refCount;
71 }
72 
CreateInstance(IUnknown * pUnkOuter,REFIID riid,LPVOID * ppvObject)73 STDMETHODIMP ClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppvObject)
74 {
75     if (pUnkOuter) {
76         return CLASS_E_NOAGGREGATION;
77     }
78 
79     if (!ppvObject) {
80         return E_INVALIDARG;
81     }
82 
83     *ppvObject = NULL;
84 
85     ContextMenu *contextMenu = new ContextMenu;
86     HRESULT hResult = contextMenu->QueryInterface(riid, ppvObject);
87     contextMenu->Release();
88     return hResult;
89 }
90 
LockServer(BOOL fLock)91 STDMETHODIMP ClassFactory::LockServer(BOOL fLock)
92 {
93     if (fLock) {
94         InterlockedIncrement(&gLockCount);
95     } else {
96         InterlockedDecrement(&gLockCount);
97     }
98 
99     return NOERROR;
100 }
101