1 /**
2  * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 2.1 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  */
14 
15 #include <windows.h>
16 #include <new>
17 
18 #include "OCOverlayFactory.h"
19 #include "OCOverlay.h"
20 
21 extern long dllReferenceCount;
22 
OCOverlayFactory(int state)23 OCOverlayFactory::OCOverlayFactory(int state)
24     : _referenceCount(1), _state(state)
25 {
26     InterlockedIncrement(&dllReferenceCount);
27 }
28 
~OCOverlayFactory()29 OCOverlayFactory::~OCOverlayFactory()
30 {
31     InterlockedDecrement(&dllReferenceCount);
32 }
33 
QueryInterface(REFIID riid,void ** ppv)34 IFACEMETHODIMP OCOverlayFactory::QueryInterface(REFIID riid, void **ppv)
35 {
36     HRESULT hResult = S_OK;
37 
38     if (IsEqualIID(IID_IUnknown, riid) ||
39         IsEqualIID(IID_IClassFactory, riid))
40     {
41         *ppv = static_cast<IUnknown *>(this);
42         AddRef();
43     }
44     else
45     {
46         hResult = E_NOINTERFACE;
47         *ppv = NULL;
48     }
49 
50     return hResult;
51 }
52 
IFACEMETHODIMP_(ULONG)53 IFACEMETHODIMP_(ULONG) OCOverlayFactory::AddRef()
54 {
55     return InterlockedIncrement(&_referenceCount);
56 }
57 
IFACEMETHODIMP_(ULONG)58 IFACEMETHODIMP_(ULONG) OCOverlayFactory::Release()
59 {
60     ULONG cRef = InterlockedDecrement(&_referenceCount);
61 
62     if (0 == cRef)
63     {
64         delete this;
65     }
66     return cRef;
67 }
68 
CreateInstance(IUnknown * pUnkOuter,REFIID riid,void ** ppv)69 IFACEMETHODIMP OCOverlayFactory::CreateInstance(
70     IUnknown *pUnkOuter, REFIID riid, void **ppv)
71 {
72     HRESULT hResult = CLASS_E_NOAGGREGATION;
73 
74     if (pUnkOuter != NULL) { return hResult; }
75 
76     hResult = E_OUTOFMEMORY;
77     OCOverlay *lrOverlay = new (std::nothrow) OCOverlay(_state);
78     if (!lrOverlay) { return hResult; }
79 
80     hResult = lrOverlay->QueryInterface(riid, ppv);
81     lrOverlay->Release();
82 
83     return hResult;
84 }
85 
LockServer(BOOL fLock)86 IFACEMETHODIMP OCOverlayFactory::LockServer(BOOL fLock)
87 {
88     if (fLock) {
89         InterlockedIncrement(&dllReferenceCount);
90     } else {
91         InterlockedDecrement(&dllReferenceCount);
92     }
93     return S_OK;
94 }
95