ReactOS  0.4.14-dev-593-g1793dcc
enumlist.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Shell
3  * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4  * PURPOSE: CNetworkConnections Shell Folder items enumerator
5  * COPYRIGHT: Copyright 2008 Johannes Anderwald (johannes.anderwald@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
11 {
12  if (!pidl || !pidl->mkid.cb || pidl->mkid.abID[0] != 0x99)
13  return NULL;
14  return (PNETCONIDSTRUCT)(&pidl->mkid.abID[0]);
15 }
16 
18 {
20  if (!pdata)
21  return NULL;
22  return (PWCHAR)&pidl->mkid.abID[pdata->uNameOffset];
23 }
24 
26 {
28  if (!pdata)
29  return NULL;
30  return (PWCHAR)&pidl->mkid.abID[pdata->uDeviceNameOffset];
31 }
32 
34 {
35  PITEMID_CHILD pidl;
37  NETCON_PROPERTIES * pProperties;
38  PNETCONIDSTRUCT pnetid;
39  PWCHAR pwchName;
40 
41  if (pItem->GetProperties(&pProperties) != S_OK)
42  return NULL;
43 
44  size = sizeof(WORD); /* nr of bytes in this item */
45  size += sizeof(NETCONIDSTRUCT);
46  size += (wcslen(pProperties->pszwName) + 1) * sizeof(WCHAR);
47  size += (wcslen(pProperties->pszwDeviceName) + 1) * sizeof(WCHAR);
48 
49  /* Allocate enough memory for the trailing id which will indicate that this is a simple id */
50  pidl = static_cast<LPITEMIDLIST>(SHAlloc(size + sizeof(SHITEMID)));
51  pidl->mkid.cb = (WORD)size;
52  pidl->mkid.abID[0] = 0x99;
53 
54  /* Copy the connection properties */
55  pnetid = ILGetConnData(pidl);
56  pnetid->guidId = pProperties->guidId;
57  pnetid->Status = pProperties->Status;
58  pnetid->MediaType = pProperties->MediaType;
59  pnetid->dwCharacter = pProperties->dwCharacter;
60  pnetid->uNameOffset = sizeof(NETCONIDSTRUCT);
61  pnetid->uDeviceNameOffset = pnetid->uNameOffset + (wcslen(pProperties->pszwName) + 1) * sizeof(WCHAR);
62 
63  pwchName = ILGetConnName(pidl);
64  wcscpy(pwchName, pProperties->pszwName);
65 
66  pwchName = ILGetDeviceName(pidl);
67  wcscpy(pwchName, pProperties->pszwDeviceName);
68 
69  /* Set the trailing id to null */
70  memset((void*)((ULONG_PTR)pidl + size), 0, sizeof(SHITEMID));
71 
72  NcFreeNetconProperties(pProperties);
73 
74  return pidl;
75 }
76 
78 {
79  HRESULT hr;
83  ULONG Count;
84  NETCON_PROPERTIES * pProperties;
85 
87  if (!pdata)
88  return E_FAIL;
89 
90  /* get an instance to of IConnectionManager */
91  hr = CNetConnectionManager_CreateInstance(IID_PPV_ARG(INetConnectionManager, &pNetConMan));
93  return hr;
94 
95  hr = pNetConMan->EnumConnections(NCME_DEFAULT, &pEnumCon);
97  return hr;
98 
99  while (TRUE)
100  {
101  hr = pEnumCon->Next(1, &INetCon, &Count);
102  if (hr != S_OK)
103  return E_FAIL;
104 
105  hr = INetCon->GetProperties(&pProperties);
106  if (FAILED_UNEXPECTEDLY(hr))
107  continue;
108 
109  BOOL bSame = !memcmp(&pProperties->guidId, &pdata->guidId, sizeof(GUID));
110 
111  NcFreeNetconProperties(pProperties);
112 
113  if (bSame)
114  {
115  *pItem = INetCon.Detach();
116  return S_OK;
117  }
118  }
119 
120  return E_FAIL;
121 }
122 
123 typedef struct tagENUMLIST
124 {
127 } ENUMLIST, *LPENUMLIST;
128 
130  public CComObjectRootEx<CComMultiThreadModelNoCS>,
131  public IEnumIDList
132 {
133  public:
134  CEnumIDList();
135  ~CEnumIDList();
136 
138 
139  // IEnumIDList
140  virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt, PITEMID_CHILD *rgelt, ULONG *pceltFetched);
141  virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt);
142  virtual HRESULT STDMETHODCALLTYPE Reset();
143  virtual HRESULT STDMETHODCALLTYPE Clone(IEnumIDList **ppenum);
144 
145  private:
147 
151 
152  public:
155 
157  COM_INTERFACE_ENTRY_IID(IID_IEnumIDList, IEnumIDList)
158  END_COM_MAP()
159 };
160 
161 /**************************************************************************
162  * AddToEnumList()
163  */
164 BOOL
166 {
167  LPENUMLIST pNew;
168 
169  if (!pidl)
170  return FALSE;
171 
172  pNew = static_cast<LPENUMLIST>(SHAlloc(sizeof(ENUMLIST)));
173  if (pNew)
174  {
175  pNew->pNext = NULL;
176  pNew->pidl = pidl;
177 
178  if (!m_pFirst)
179  {
180  m_pFirst = pNew;
181  m_pCurrent = pNew;
182  }
183 
184  if (m_pLast)
185  {
186  /*add the new item to the end of the list */
187  m_pLast->pNext = pNew;
188  }
189 
190  /*update the last item pointer */
191  m_pLast = pNew;
192  return TRUE;
193  }
194  return FALSE;
195 }
196 
198  m_pFirst(NULL),
199  m_pLast(NULL),
201 {
202 }
203 
205 {
206  LPENUMLIST pDelete;
207 
208  while (m_pFirst)
209  {
210  pDelete = m_pFirst;
211  m_pFirst = pDelete->pNext;
212  SHFree(pDelete->pidl);
213  SHFree(pDelete);
214  }
215 }
216 
217 HRESULT
219 {
220  HRESULT hr;
223  ULONG Count;
224  PITEMID_CHILD pidl;
225 
226  /* get an instance to of IConnectionManager */
227  hr = CNetConnectionManager_CreateInstance(IID_PPV_ARG(INetConnectionManager, &pNetConMan));
228  if (FAILED_UNEXPECTEDLY(hr))
229  return S_OK;
230 
231  hr = pNetConMan->EnumConnections(NCME_DEFAULT, &pEnumCon);
232  if (FAILED_UNEXPECTEDLY(hr))
233  return S_OK;
234 
235  while (TRUE)
236  {
237  CComPtr<INetConnection> INetCon;
238 
239  hr = pEnumCon->Next(1, &INetCon, &Count);
240  if (hr != S_OK)
241  break;
242 
243  pidl = ILCreateNetConnectItem(INetCon);
244  if (pidl)
245  {
246  AddToEnumList(pidl);
247  }
248  }
249 
250  return S_OK;
251 }
252 
253 HRESULT
254 WINAPI
256  ULONG celt,
257  PITEMID_CHILD *rgelt,
258  ULONG *pceltFetched)
259 {
260  ULONG i;
261  HRESULT hr = S_OK;
263 
264  if (pceltFetched)
265  *pceltFetched = 0;
266 
267  if (celt > 1 && !pceltFetched)
268  {
269  return E_INVALIDARG;
270  }
271 
272  if (celt > 0 && !m_pCurrent)
273  {
274  return S_FALSE;
275  }
276 
277  for (i = 0; i < celt; i++)
278  {
279  if (!m_pCurrent)
280  break;
281 
283  rgelt[i] = temp;
285  }
286 
287  if (pceltFetched)
288  *pceltFetched = i;
289 
290  return hr;
291 }
292 
293 HRESULT
294 WINAPI
296 {
297  DWORD dwIndex;
298  HRESULT hr = S_OK;
299 
300  for (dwIndex = 0; dwIndex < celt; dwIndex++)
301  {
302  if (!m_pCurrent)
303  {
304  hr = S_FALSE;
305  break;
306  }
308  }
309 
310  return hr;
311 }
312 
313 HRESULT
314 WINAPI
316 {
318  return S_OK;
319 }
320 
321 HRESULT
322 WINAPI
324  LPENUMIDLIST * ppenum)
325 {
326  return E_NOTIMPL;
327 }
328 
330 {
331  return ShellObjectCreatorInit<CEnumIDList>(riid, ppv);
332 }
PITEMID_CHILD pidl
Definition: enumlist.cpp:126
HRESULT ILGetConnection(PCITEMID_CHILD pidl, INetConnection **pItem)
Definition: enumlist.cpp:77
NETCON_MEDIATYPE MediaType
Definition: precomp.h:63
struct tagENUMLIST * pNext
Definition: enumlist.cpp:125
#define REFIID
Definition: guiddef.h:118
#define TRUE
Definition: types.h:120
LPWSTR pszwDeviceName
Definition: netcon.h:85
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt, PITEMID_CHILD *rgelt, ULONG *pceltFetched)
Definition: enumlist.cpp:255
NETCON_STATUS Status
Definition: precomp.h:62
ULONG_PTR uDeviceNameOffset
Definition: precomp.h:66
HRESULT hr
Definition: shlfolder.c:183
REFIID riid
Definition: precomp.h:44
virtual HRESULT STDMETHODCALLTYPE Clone(IEnumIDList **ppenum)
Definition: enumlist.cpp:323
struct tagENUMLIST * LPENUMLIST
HRESULT GetProperties([out] NETCON_PROPERTIES **ppProps)
REFIID LPVOID * ppv
Definition: atlbase.h:39
PWCHAR ILGetConnName(PCITEMID_CHILD pidl)
Definition: enumlist.cpp:17
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define DECLARE_NOT_AGGREGATABLE(x)
Definition: atlcom.h:611
uint16_t * PWCHAR
Definition: typedefs.h:54
#define IID_PPV_ARG(Itype, ppType)
#define E_FAIL
Definition: ddrawi.h:102
VOID WINAPI NcFreeNetconProperties(NETCON_PROPERTIES *pProps)
Definition: netshell.cpp:119
WORD cb
Definition: shtypes.idl:27
DWORD dwCharacter
Definition: precomp.h:64
uint32_t ULONG_PTR
Definition: typedefs.h:63
BYTE abID[1]
Definition: shtypes.idl:28
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
LPENUMLIST m_pFirst
Definition: enumlist.cpp:148
unsigned int BOOL
Definition: ntddk_ex.h:94
virtual HRESULT STDMETHODCALLTYPE Reset()
Definition: enumlist.cpp:315
#define S_FALSE
Definition: winerror.h:2357
#define E_INVALIDARG
Definition: ddrawi.h:101
smooth NULL
Definition: ftsmooth.c:416
PWCHAR ILGetDeviceName(PCITEMID_CHILD pidl)
Definition: enumlist.cpp:25
PNETCONIDSTRUCT ILGetConnData(PCITEMID_CHILD pidl)
Definition: enumlist.cpp:10
static PROTOCOLDATA * pdata
Definition: protocol.c:158
#define BEGIN_COM_MAP(x)
Definition: atlcom.h:541
GLsizeiptr size
Definition: glext.h:5919
__wchar_t WCHAR
Definition: xmlstorage.h:180
LONG HRESULT
Definition: typedefs.h:77
#define FAILED_UNEXPECTEDLY(hr)
Definition: shellutils.h:71
#define WINAPI
Definition: msvc.h:6
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
ULONG_PTR uNameOffset
Definition: precomp.h:65
struct tagNETCONIDSTRUCT NETCONIDSTRUCT
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
struct tagENUMLIST ENUMLIST
PITEMID_CHILD ILCreateNetConnectItem(INetConnection *pItem)
Definition: enumlist.cpp:33
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt)
Definition: enumlist.cpp:295
#define S_OK
Definition: intsafe.h:59
static calc_node_t temp
Definition: rpn_ieee.c:38
HRESULT WINAPI CNetConnectionManager_CreateInstance(REFIID riid, LPVOID *ppv)
LPITEMIDLIST WINAPI ILClone(LPCITEMIDLIST pidl)
Definition: pidl.c:228
#define COM_INTERFACE_ENTRY_IID(iid, x)
Definition: atlcom.h:561
#define E_NOTIMPL
Definition: ddrawi.h:99
NETCON_MEDIATYPE MediaType
Definition: netcon.h:87
BOOL AddToEnumList(PITEMID_CHILD pidl)
Definition: enumlist.cpp:165
#define DECLARE_PROTECT_FINAL_CONSTRUCT()
Definition: atlcom.h:639
void WINAPI SHFree(LPVOID pv)
Definition: shellole.c:331
NETCON_STATUS Status
Definition: netcon.h:86
HRESULT CEnumIDList_CreateInstance(HWND hwndOwner, DWORD dwFlags, REFIID riid, LPVOID *ppv)
Definition: enumlist.cpp:329
unsigned int ULONG
Definition: retypes.h:1
SHITEMID mkid
Definition: shtypes.idl:34
#define END_COM_MAP()
Definition: atlcom.h:552
LPENUMLIST m_pLast
Definition: enumlist.cpp:149
T * Detach()
Definition: atlcomcli.h:156
#define memset(x, y, z)
Definition: compat.h:39
LPVOID WINAPI SHAlloc(SIZE_T len)
Definition: shellole.c:309
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
HRESULT Initialize()
Definition: enumlist.cpp:218
LPENUMLIST m_pCurrent
Definition: enumlist.cpp:150