xref: /reactos/sdk/include/ddk/kcom.h (revision e419195d)
1 /*
2  * kcom.h
3  *
4  * This file is part of the ReactOS PSDK package.
5  *
6  * Contributors:
7  *   Created by Andrew Greenwood.
8  *
9  * THIS SOFTWARE IS NOT COPYRIGHTED
10  *
11  * This source code is offered for use in the public domain. You may
12  * use, modify or distribute it freely.
13  *
14  * This code is distributed in the hope that it will be useful but
15  * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
16  * DISCLAIMED. This includes but is not limited to warranties of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  */
20 
21 #if !defined(_KS_)
22 #error KS.H must be included before KCOM.H
23 #endif
24 
25 #if !defined(_KCOM_)
26 #define _KCOM_
27 
28 #if defined(__cplusplus)
29 extern "C" {
30 #endif
31 
32 #define STATIC_KoCreateObject 0x72CF721CL, 0x525A, 0x11D1, {0x9A, 0xA1, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96}
33 DEFINE_GUIDSTRUCT("72CF721C-525A-11D1-9AA1-00A0C9223196", KoCreateObject);
34 #define KOSTRING_CreateObject L"{72CF721C-525A-11D1-9AA1-00A0C9223196}"
35 
36 #ifndef CLSCTX_KERNEL_SERVER
37 #define CLSCTX_KERNEL_SERVER    0x00000200
38 #endif
39 
40 #if !defined(__cplusplus) || _MSC_VER < 1100
41 
42 #define STATIC_IID_IKoInitializeParentDeviceObject 0x21B36996, 0x8DE3, 0x11D1, 0x8A, 0xE0, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96
43 DEFINE_GUIDEX(IID_IKoInitializeParentDeviceObject);
44 
45 #else
46 
47 interface __declspec(uuid("21B36996-8DE3-11D1-8AE0-00A0C9223196")) IKoInitializeParentDeviceObject;
48 
49 #endif
50 
51 #ifndef COMDDKMETHOD
52 #ifdef _COMDDK_
53 #define COMDDKMETHOD
54 #else
55 #define COMDDKMETHOD DECLSPEC_IMPORT
56 #endif
57 #endif
58 
59 #ifdef _COMDDK_
60 #define COMDDKAPI
61 #else
62 #define COMDDKAPI DECLSPEC_IMPORT
63 #endif
64 
65 typedef
66 NTSTATUS
67 (*KoCreateObjectHandler)(
68   IN REFCLSID ClassId,
69   IN IUnknown* UnkOuter OPTIONAL,
70   IN REFIID InterfaceId,
71   OUT PVOID* Interface);
72 
73 #undef INTERFACE
74 #define INTERFACE INonDelegatedUnknown
75 DECLARE_INTERFACE(INonDelegatedUnknown) {
76   STDMETHOD(NonDelegatedQueryInterface)(
77     THIS_
78     IN REFIID InterfaceId,
79     OUT PVOID* Interface
80   ) PURE;
81 
82   STDMETHOD_(ULONG,NonDelegatedAddRef)(
83     THIS
84   ) PURE;
85 
86   STDMETHOD_(ULONG,NonDelegatedRelease)(
87     THIS
88   ) PURE;
89 };
90 
91 #undef INTERFACE
92 #define INTERFACE IIndirectedUnknown
93 DECLARE_INTERFACE(IIndirectedUnknown) {
94   STDMETHOD(IndirectedQueryInterface)(
95     THIS_
96     IN REFIID InterfaceId,
97     OUT PVOID* Interface
98   ) PURE;
99 
100   STDMETHOD_(ULONG,IndirectedAddRef)(
101     THIS
102   ) PURE;
103 
104   STDMETHOD_(ULONG,IndirectedRelease)(
105     THIS
106   ) PURE;
107 };
108 
109 #undef INTERFACE
110 #define INTERFACE IKoInitializeParentDeviceObject
111 DECLARE_INTERFACE_(IKoInitializeParentDeviceObject, IUnknown) {
112   STDMETHOD(SetParentDeviceObject)(
113     THIS_
114     IN PDEVICE_OBJECT ParentDeviceObject
115   ) PURE;
116 };
117 
118 #if defined(__cplusplus)
119 
120 class CBaseUnknown : public INonDelegatedUnknown, public IIndirectedUnknown {
121   protected:
122     LONG m_RefCount;
123   private:
124     BOOLEAN m_UsingClassId;
125     CLSID m_ClassId;
126   protected:
127     IUnknown* m_UnknownOuter;
128   public:
129     COMDDKMETHOD CBaseUnknown (IN REFCLSID ClassId, IN IUnknown* UnknownOuter OPTIONAL = NULL);
130     COMDDKMETHOD CBaseUnknown(IN IUnknown* UnknownOuter OPTIONAL = NULL);
131     COMDDKMETHOD virtual ~CBaseUnknown();
132     COMDDKMETHOD STDMETHODIMP_(ULONG) NonDelegatedAddRef();
133     COMDDKMETHOD STDMETHODIMP_(ULONG) NonDelegatedRelease();
134     COMDDKMETHOD STDMETHODIMP NonDelegatedQueryInterface(IN REFIID InterfaceId,OUT PVOID* Interface);
135     COMDDKMETHOD STDMETHODIMP_(ULONG) IndirectedAddRef();
136     COMDDKMETHOD STDMETHODIMP_(ULONG) IndirectedRelease();
137     COMDDKMETHOD STDMETHODIMP IndirectedQueryInterface(IN REFIID InterfaceId, OUT PVOID* Interface);
138 };
139 
140 #if !defined(DEFINE_ABSTRACT_UNKNOWN)
141 #define DEFINE_ABSTRACT_UNKNOWN() \
142   STDMETHOD(QueryInterface)(THIS_ REFIID InterfaceId, PVOID* Interface) PURE; \
143   STDMETHOD_(ULONG,AddRef)(THIS) PURE; \
144   STDMETHOD_(ULONG,Release)(THIS) PURE;
145 #endif
146 
147 #define DEFINE_STD_UNKNOWN() \
148   STDMETHODIMP NonDelegatedQueryInterface( REFIID InterfaceId, PVOID* Interface); \
149   STDMETHODIMP QueryInterface( REFIID InterfaceId, PVOID* Interface); \
150   STDMETHODIMP_(ULONG) AddRef(); \
151   STDMETHODIMP_(ULONG) Release();
152 
153 #define IMPLEMENT_STD_UNKNOWN(Class) \
154   STDMETHODIMP Class::QueryInterface( REFIID InterfaceId, PVOID* Interface) { \
155     return m_UnknownOuter->QueryInterface(InterfaceId, Interface);\
156   } \
157   STDMETHODIMP_(ULONG) Class::AddRef() { \
158     return m_UnknownOuter->AddRef(); \
159   } \
160   STDMETHODIMP_(ULONG) Class::Release() { \
161     return m_UnknownOuter->Release(); \
162   }
163 
164 #else
165 
166 COMDDKAPI
167 void
168 NTAPI
169 KoRelease(
170   IN REFCLSID ClassId);
171 
172 #endif /* !__cplusplus */
173 
174 COMDDKAPI
175 NTSTATUS
176 NTAPI
177 KoCreateInstance(
178   IN REFCLSID ClassId,
179   IN IUnknown* UnkOuter OPTIONAL,
180   IN ULONG ClsContext,
181   IN REFIID InterfaceId,
182   OUT PVOID* Interface);
183 
184 COMDDKAPI
185 NTSTATUS
186 NTAPI
187 KoDeviceInitialize(
188   IN PDEVICE_OBJECT DeviceObject);
189 
190 COMDDKAPI
191 NTSTATUS
192 NTAPI
193 KoDriverInitialize(
194   IN PDRIVER_OBJECT DriverObject,
195   IN PUNICODE_STRING RegistryPathName,
196   IN KoCreateObjectHandler CreateObjectHandler);
197 
198 
199 #if defined(__cplusplus)
200 }
201 #endif
202 
203 #ifdef __cplusplus
204 
205 #ifndef _NEW_DELETE_OPERATORS_
206 #define _NEW_DELETE_OPERATORS_
207 
208 inline PVOID operator new(
209   size_t iSize,
210   POOL_TYPE poolType)
211 {
212   PVOID result = ExAllocatePoolWithTag(poolType,iSize,'wNCK');
213   if (result) {
214     RtlZeroMemory(result,iSize);
215   }
216   return result;
217 }
218 
219 inline PVOID operator new(
220   size_t iSize,
221   POOL_TYPE poolType,
222   ULONG tag)
223 {
224   PVOID result = ExAllocatePoolWithTag(poolType,iSize,tag);
225   if (result) {
226     RtlZeroMemory(result,iSize);
227   }
228   return result;
229 }
230 
231 inline void __cdecl operator delete(
232   PVOID pVoid)
233 {
234   if (pVoid) ExFreePool(pVoid);
235 }
236 
237 inline void __cdecl operator delete(
238   PVOID pVoid, UINT_PTR)
239 {
240   if (pVoid) ExFreePool(pVoid);
241 }
242 
243 #endif /* _NEW_DELETE_OPERATORS_ */
244 
245 #if defined(_SYS_GUID_OPERATOR_EQ_)
246 #define _GUID_OPERATORS_
247 //#pragma message("WARNING: Using system operator==/!= for GUIDs")
248 #endif
249 
250 #ifndef _GUID_OPERATORS_
251 #define _GUID_OPERATORS_
252 
253 __inline BOOL operator==(const GUID& guidOne, const GUID& guidOther) {
254   return IsEqualGUIDAligned(guidOne,guidOther);
255 }
256 
257 __inline BOOL operator!=(const GUID& guidOne, const GUID& guidOther) {
258   return !(guidOne == guidOther);
259 }
260 
261 #endif /* _GUID_OPERATORS_ */
262 
263 #endif /* __cplusplus */
264 
265 #endif /* _KCOM_ */
266