#pragma once
// WAC.com Class Liblary for Visual C++
// Copyright (C) WAC.com Inc. All rights reserved.
//
// This file is a part of the WAC.com Class Liblary.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
#include <vector>
#include <objbase.h> // for IID_PPV_ARGS
#include <windef.h> // for MAX_PATH
//#include <ntdef.h> // for ARRAYSIZE
#include <icontact.h>
#include <icontactproperties.h>
//#include <smarttypes.h>
_COM_SMARTPTR_TYPEDEF(IContactManager, __uuidof(IContactManager));
_COM_SMARTPTR_TYPEDEF(IContactCollection, __uuidof(IContactCollection));
_COM_SMARTPTR_TYPEDEF(IContactProperties, __uuidof(IContactProperties));
_COM_SMARTPTR_TYPEDEF(IContact, __uuidof(IContact));
_COM_SMARTPTR_TYPEDEF(IContactPropertyCollection, __uuidof(IContactPropertyCollection));
/////////////////////////////////////////////////////////////////////////////
// CContactHelper
class CContactHelper
{
protected:
typedef CContactHelper thisClass;
protected:
CContactHelper();
virtual ~CContactHelper();
public:
static IContactManagerPtr GetContactManager(LPCWSTR szAppName, LPCWSTR szAppVersion)
{
HRESULT hr = S_OK;
IContactManagerPtr spContactManager;
hr = spContactManager.CreateInstance(CLSID_ContactManager);
if (FAILED(hr))
{
return NULL;
}
hr = spContactManager->Initialize(szAppName, szAppVersion);
if (FAILED(hr))
{
return NULL;
}
return spContactManager;
}
static HRESULT EnumContacts(IContactManagerPtr spContactManager, std::vector<IContactPtr>& spContacts)
{
if (spContactManager == NULL)
{
return E_FAIL;
}
HRESULT hr = S_OK;
spContacts.clear();
IContactCollectionPtr spContactCollection;
hr = spContactManager->GetContactCollection(&spContactCollection);
if (FAILED(hr))
{
return hr;
}
while (S_FALSE != spContactCollection->Next())
{
IContactPtr spContact;
hr = spContactCollection->GetCurrent(&spContact);
if (FAILED(hr))
{
ATLASSERT(0);
continue;
}
spContacts.push_back(spContact);
}
return S_OK;
}
static HRESULT EnumContactIDs(IContactManagerPtr spContactManager, std::vector<CStringW>& strContactIDs)
{
if (spContactManager == NULL)
{
ATLASSERT(0);
return E_FAIL;
}
HRESULT hr = S_OK;
strContactIDs.clear();
IContactCollectionPtr spContactCollection;
hr = spContactManager->GetContactCollection(&spContactCollection);
if (FAILED(hr))
{
ATLASSERT(0);
return hr;
}
while (S_FALSE != spContactCollection->Next())
{
IContactPtr spContact;
hr = spContactCollection->GetCurrent(&spContact);
if (FAILED(hr))
{
ATLASSERT(0);
continue;
}
// ID
WCHAR szContactID[1024] = { 0 };
hr = spContact->GetContactID(szContactID, ARRAYSIZE(szContactID), NULL);
if (FAILED(hr))
{
ATLASSERT(0);
continue;
}
strContactIDs.push_back(szContactID);
}
return S_OK;
}
static HRESULT EnumContactPaths(IContactManagerPtr spContactManager, std::vector<CStringW>& strPaths)
{
if (spContactManager == NULL)
{
return E_FAIL;
}
HRESULT hr = S_OK;
strPaths.clear();
IContactCollectionPtr spContactCollection;
hr = spContactManager->GetContactCollection(&spContactCollection);
if (FAILED(hr))
{
return hr;
}
while (S_FALSE != spContactCollection->Next())
{
IContactPtr spContact;
hr = spContactCollection->GetCurrent(&spContact);
if (FAILED(hr))
{
ATLASSERT(0);
continue;
}
// パス
WCHAR szPath[_MAX_PATH] = { 0 };
hr = spContact->GetContactID(szPath, ARRAYSIZE(szPath), NULL);
if (FAILED(hr))
{
ATLASSERT(0);
continue;
}
strPaths.push_back(szPath);
}
return S_OK;
}
};