2018-11-16 11:24:03 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Shell
|
|
|
|
* LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
|
|
|
|
* PURPOSE: CNetworkConnections Shell Folder items enumerator
|
|
|
|
* COPYRIGHT: Copyright 2008 Johannes Anderwald (johannes.anderwald@reactos.org)
|
|
|
|
*/
|
|
|
|
|
2013-01-24 23:00:42 +00:00
|
|
|
#include "precomp.h"
|
2012-01-20 21:50:54 +00:00
|
|
|
|
2018-11-12 20:52:57 +00:00
|
|
|
PNETCONIDSTRUCT ILGetConnData(PCITEMID_CHILD pidl)
|
|
|
|
{
|
|
|
|
if (!pidl || !pidl->mkid.cb || pidl->mkid.abID[0] != 0x99)
|
|
|
|
return NULL;
|
|
|
|
return (PNETCONIDSTRUCT)(&pidl->mkid.abID[0]);
|
|
|
|
}
|
2018-11-12 11:23:38 +00:00
|
|
|
|
2018-11-12 20:52:57 +00:00
|
|
|
PWCHAR ILGetConnName(PCITEMID_CHILD pidl)
|
2018-11-12 11:23:38 +00:00
|
|
|
{
|
2018-11-12 20:52:57 +00:00
|
|
|
PNETCONIDSTRUCT pdata = ILGetConnData(pidl);
|
|
|
|
if (!pdata)
|
|
|
|
return NULL;
|
|
|
|
return (PWCHAR)&pidl->mkid.abID[pdata->uNameOffset];
|
|
|
|
}
|
2018-11-12 11:23:38 +00:00
|
|
|
|
2018-11-12 20:52:57 +00:00
|
|
|
PWCHAR ILGetDeviceName(PCITEMID_CHILD pidl)
|
|
|
|
{
|
|
|
|
PNETCONIDSTRUCT pdata = ILGetConnData(pidl);
|
|
|
|
if (!pdata)
|
|
|
|
return NULL;
|
|
|
|
return (PWCHAR)&pidl->mkid.abID[pdata->uDeviceNameOffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
PITEMID_CHILD ILCreateNetConnectItem(INetConnection * pItem)
|
|
|
|
{
|
|
|
|
PITEMID_CHILD pidl;
|
|
|
|
ULONG_PTR size;
|
|
|
|
NETCON_PROPERTIES * pProperties;
|
|
|
|
PNETCONIDSTRUCT pnetid;
|
|
|
|
PWCHAR pwchName;
|
|
|
|
|
|
|
|
if (pItem->GetProperties(&pProperties) != S_OK)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
size = sizeof(WORD); /* nr of bytes in this item */
|
|
|
|
size += sizeof(NETCONIDSTRUCT);
|
|
|
|
size += (wcslen(pProperties->pszwName) + 1) * sizeof(WCHAR);
|
|
|
|
size += (wcslen(pProperties->pszwDeviceName) + 1) * sizeof(WCHAR);
|
|
|
|
|
|
|
|
/* Allocate enough memory for the trailing id which will indicate that this is a simple id */
|
|
|
|
pidl = static_cast<LPITEMIDLIST>(SHAlloc(size + sizeof(SHITEMID)));
|
|
|
|
pidl->mkid.cb = (WORD)size;
|
|
|
|
pidl->mkid.abID[0] = 0x99;
|
|
|
|
|
|
|
|
/* Copy the connection properties */
|
|
|
|
pnetid = ILGetConnData(pidl);
|
|
|
|
pnetid->guidId = pProperties->guidId;
|
|
|
|
pnetid->Status = pProperties->Status;
|
|
|
|
pnetid->MediaType = pProperties->MediaType;
|
|
|
|
pnetid->dwCharacter = pProperties->dwCharacter;
|
|
|
|
pnetid->uNameOffset = sizeof(NETCONIDSTRUCT);
|
|
|
|
pnetid->uDeviceNameOffset = pnetid->uNameOffset + (wcslen(pProperties->pszwName) + 1) * sizeof(WCHAR);
|
|
|
|
|
|
|
|
pwchName = ILGetConnName(pidl);
|
|
|
|
wcscpy(pwchName, pProperties->pszwName);
|
|
|
|
|
|
|
|
pwchName = ILGetDeviceName(pidl);
|
|
|
|
wcscpy(pwchName, pProperties->pszwDeviceName);
|
|
|
|
|
|
|
|
/* Set the trailing id to null */
|
|
|
|
memset((void*)((ULONG_PTR)pidl + size), 0, sizeof(SHITEMID));
|
|
|
|
|
|
|
|
NcFreeNetconProperties(pProperties);
|
|
|
|
|
|
|
|
return pidl;
|
|
|
|
}
|
2018-11-12 11:23:38 +00:00
|
|
|
|
2018-11-12 20:52:57 +00:00
|
|
|
HRESULT ILGetConnection(PCITEMID_CHILD pidl, INetConnection ** pItem)
|
2018-11-12 11:23:38 +00:00
|
|
|
{
|
2018-11-12 20:52:57 +00:00
|
|
|
HRESULT hr;
|
|
|
|
CComPtr<INetConnectionManager> pNetConMan;
|
|
|
|
CComPtr<IEnumNetConnection> pEnumCon;
|
|
|
|
CComPtr<INetConnection> INetCon;
|
|
|
|
ULONG Count;
|
|
|
|
NETCON_PROPERTIES * pProperties;
|
|
|
|
|
|
|
|
PNETCONIDSTRUCT pdata = ILGetConnData(pidl);
|
|
|
|
if (!pdata)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
/* get an instance to of IConnectionManager */
|
|
|
|
hr = CNetConnectionManager_CreateInstance(IID_PPV_ARG(INetConnectionManager, &pNetConMan));
|
|
|
|
if (FAILED_UNEXPECTEDLY(hr))
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
hr = pNetConMan->EnumConnections(NCME_DEFAULT, &pEnumCon);
|
|
|
|
if (FAILED_UNEXPECTEDLY(hr))
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
while (TRUE)
|
2018-11-12 11:23:38 +00:00
|
|
|
{
|
2018-11-12 20:52:57 +00:00
|
|
|
hr = pEnumCon->Next(1, &INetCon, &Count);
|
|
|
|
if (hr != S_OK)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
hr = INetCon->GetProperties(&pProperties);
|
|
|
|
if (FAILED_UNEXPECTEDLY(hr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BOOL bSame = !memcmp(&pProperties->guidId, &pdata->guidId, sizeof(GUID));
|
|
|
|
|
|
|
|
NcFreeNetconProperties(pProperties);
|
|
|
|
|
|
|
|
if (bSame)
|
|
|
|
{
|
|
|
|
*pItem = INetCon.Detach();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2018-11-12 11:23:38 +00:00
|
|
|
|
|
|
|
typedef struct tagENUMLIST
|
|
|
|
{
|
|
|
|
struct tagENUMLIST *pNext;
|
|
|
|
PITEMID_CHILD pidl;
|
|
|
|
} ENUMLIST, *LPENUMLIST;
|
|
|
|
|
|
|
|
class CEnumIDList:
|
|
|
|
public CComObjectRootEx<CComMultiThreadModelNoCS>,
|
|
|
|
public IEnumIDList
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CEnumIDList();
|
|
|
|
~CEnumIDList();
|
|
|
|
|
|
|
|
HRESULT Initialize();
|
|
|
|
|
|
|
|
// IEnumIDList
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt, PITEMID_CHILD *rgelt, ULONG *pceltFetched);
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt);
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE Reset();
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE Clone(IEnumIDList **ppenum);
|
|
|
|
|
|
|
|
private:
|
|
|
|
BOOL AddToEnumList(PITEMID_CHILD pidl);
|
|
|
|
|
|
|
|
LPENUMLIST m_pFirst;
|
|
|
|
LPENUMLIST m_pLast;
|
|
|
|
LPENUMLIST m_pCurrent;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DECLARE_NOT_AGGREGATABLE(CEnumIDList)
|
|
|
|
DECLARE_PROTECT_FINAL_CONSTRUCT()
|
|
|
|
|
|
|
|
BEGIN_COM_MAP(CEnumIDList)
|
|
|
|
COM_INTERFACE_ENTRY_IID(IID_IEnumIDList, IEnumIDList)
|
|
|
|
END_COM_MAP()
|
|
|
|
};
|
|
|
|
|
2012-01-20 21:50:54 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* AddToEnumList()
|
|
|
|
*/
|
2015-04-19 20:18:25 +00:00
|
|
|
BOOL
|
2015-04-19 20:56:42 +00:00
|
|
|
CEnumIDList::AddToEnumList(PITEMID_CHILD pidl)
|
2012-01-20 21:50:54 +00:00
|
|
|
{
|
|
|
|
LPENUMLIST pNew;
|
|
|
|
|
|
|
|
if (!pidl)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-04-19 20:18:25 +00:00
|
|
|
pNew = static_cast<LPENUMLIST>(SHAlloc(sizeof(ENUMLIST)));
|
|
|
|
if (pNew)
|
2012-01-20 21:50:54 +00:00
|
|
|
{
|
|
|
|
pNew->pNext = NULL;
|
|
|
|
pNew->pidl = pidl;
|
|
|
|
|
2015-04-19 20:18:25 +00:00
|
|
|
if (!m_pFirst)
|
2012-01-20 21:50:54 +00:00
|
|
|
{
|
2015-04-19 20:18:25 +00:00
|
|
|
m_pFirst = pNew;
|
|
|
|
m_pCurrent = pNew;
|
2012-01-20 21:50:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 20:18:25 +00:00
|
|
|
if (m_pLast)
|
2012-01-20 21:50:54 +00:00
|
|
|
{
|
|
|
|
/*add the new item to the end of the list */
|
2015-04-19 20:18:25 +00:00
|
|
|
m_pLast->pNext = pNew;
|
2012-01-20 21:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*update the last item pointer */
|
2015-04-19 20:18:25 +00:00
|
|
|
m_pLast = pNew;
|
2012-01-20 21:50:54 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-19 20:18:25 +00:00
|
|
|
CEnumIDList::CEnumIDList() :
|
|
|
|
m_pFirst(NULL),
|
|
|
|
m_pLast(NULL),
|
|
|
|
m_pCurrent(NULL)
|
2012-01-20 21:50:54 +00:00
|
|
|
{
|
2015-04-19 20:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CEnumIDList::~CEnumIDList()
|
|
|
|
{
|
|
|
|
LPENUMLIST pDelete;
|
|
|
|
|
|
|
|
while (m_pFirst)
|
|
|
|
{
|
|
|
|
pDelete = m_pFirst;
|
|
|
|
m_pFirst = pDelete->pNext;
|
|
|
|
SHFree(pDelete->pidl);
|
|
|
|
SHFree(pDelete);
|
|
|
|
}
|
2012-01-20 21:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
2018-11-12 11:23:38 +00:00
|
|
|
CEnumIDList::Initialize()
|
2012-01-20 21:50:54 +00:00
|
|
|
{
|
2018-11-12 11:23:38 +00:00
|
|
|
HRESULT hr;
|
2018-11-12 14:46:45 +00:00
|
|
|
CComPtr<INetConnectionManager> pNetConMan;
|
|
|
|
CComPtr<IEnumNetConnection> pEnumCon;
|
2018-11-12 11:23:38 +00:00
|
|
|
ULONG Count;
|
|
|
|
PITEMID_CHILD pidl;
|
2012-01-20 21:50:54 +00:00
|
|
|
|
2018-11-12 11:23:38 +00:00
|
|
|
/* get an instance to of IConnectionManager */
|
2018-11-12 14:46:45 +00:00
|
|
|
hr = CNetConnectionManager_CreateInstance(IID_PPV_ARG(INetConnectionManager, &pNetConMan));
|
|
|
|
if (FAILED_UNEXPECTEDLY(hr))
|
2018-11-12 11:23:38 +00:00
|
|
|
return S_OK;
|
|
|
|
|
|
|
|
hr = pNetConMan->EnumConnections(NCME_DEFAULT, &pEnumCon);
|
2018-11-12 14:46:45 +00:00
|
|
|
if (FAILED_UNEXPECTEDLY(hr))
|
2012-01-20 21:50:54 +00:00
|
|
|
return S_OK;
|
|
|
|
|
2018-11-12 14:46:45 +00:00
|
|
|
while (TRUE)
|
2018-11-12 11:23:38 +00:00
|
|
|
{
|
2018-11-12 14:46:45 +00:00
|
|
|
CComPtr<INetConnection> INetCon;
|
|
|
|
|
2018-11-12 11:23:38 +00:00
|
|
|
hr = pEnumCon->Next(1, &INetCon, &Count);
|
2018-11-12 14:46:45 +00:00
|
|
|
if (hr != S_OK)
|
2018-11-12 11:23:38 +00:00
|
|
|
break;
|
2012-01-20 21:50:54 +00:00
|
|
|
|
2018-11-12 14:46:45 +00:00
|
|
|
pidl = ILCreateNetConnectItem(INetCon);
|
|
|
|
if (pidl)
|
|
|
|
{
|
|
|
|
AddToEnumList(pidl);
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 20:18:25 +00:00
|
|
|
|
2018-11-12 14:46:45 +00:00
|
|
|
return S_OK;
|
2012-01-20 21:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
WINAPI
|
|
|
|
CEnumIDList::Next(
|
|
|
|
ULONG celt,
|
2015-04-19 20:56:42 +00:00
|
|
|
PITEMID_CHILD *rgelt,
|
2012-01-20 21:50:54 +00:00
|
|
|
ULONG *pceltFetched)
|
|
|
|
{
|
|
|
|
ULONG i;
|
|
|
|
HRESULT hr = S_OK;
|
2015-04-19 20:56:42 +00:00
|
|
|
PITEMID_CHILD temp;
|
2012-01-20 21:50:54 +00:00
|
|
|
|
2015-04-19 20:18:25 +00:00
|
|
|
if (pceltFetched)
|
2012-01-20 21:50:54 +00:00
|
|
|
*pceltFetched = 0;
|
|
|
|
|
|
|
|
if (celt > 1 && !pceltFetched)
|
|
|
|
{
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2015-04-19 20:18:25 +00:00
|
|
|
if (celt > 0 && !m_pCurrent)
|
2012-01-20 21:50:54 +00:00
|
|
|
{
|
|
|
|
return S_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < celt; i++)
|
|
|
|
{
|
2015-04-19 20:18:25 +00:00
|
|
|
if (!m_pCurrent)
|
2012-01-20 21:50:54 +00:00
|
|
|
break;
|
|
|
|
|
2015-04-19 20:18:25 +00:00
|
|
|
temp = ILClone(m_pCurrent->pidl);
|
2012-01-20 21:50:54 +00:00
|
|
|
rgelt[i] = temp;
|
2015-04-19 20:18:25 +00:00
|
|
|
m_pCurrent = m_pCurrent->pNext;
|
2012-01-20 21:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pceltFetched)
|
|
|
|
*pceltFetched = i;
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
WINAPI
|
|
|
|
CEnumIDList::Skip(ULONG celt)
|
|
|
|
{
|
|
|
|
DWORD dwIndex;
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
|
|
|
|
for (dwIndex = 0; dwIndex < celt; dwIndex++)
|
|
|
|
{
|
2015-04-19 20:18:25 +00:00
|
|
|
if (!m_pCurrent)
|
2012-01-20 21:50:54 +00:00
|
|
|
{
|
|
|
|
hr = S_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-19 20:18:25 +00:00
|
|
|
m_pCurrent = m_pCurrent->pNext;
|
2012-01-20 21:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
WINAPI
|
|
|
|
CEnumIDList::Reset()
|
|
|
|
{
|
2015-04-19 20:18:25 +00:00
|
|
|
m_pCurrent = m_pFirst;
|
2012-01-20 21:50:54 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
WINAPI
|
|
|
|
CEnumIDList::Clone(
|
|
|
|
LPENUMIDLIST * ppenum)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
2018-11-12 11:23:38 +00:00
|
|
|
HRESULT CEnumIDList_CreateInstance(HWND hwndOwner, DWORD dwFlags, REFIID riid, LPVOID * ppv)
|
|
|
|
{
|
|
|
|
return ShellObjectCreatorInit<CEnumIDList>(riid, ppv);
|
|
|
|
}
|