//加载信息
bool CUserInformation::LoadInformation()
{
	//变量定义
	IStream * pIStreamSub=NULL;
	IStorage * pIStorageSub=NULL;
	IStorage * pIStorageRoot=NULL;
	IEnumSTATSTG * pIEnumSTATSTG=NULL;

	//读取数据
	try
	{
		//变量定义
		STATSTG Statstg;
		tagCompanionInfo CompanionInfo;
		ZeroMemory(&Statstg,sizeof(Statstg));
		ZeroMemory(&CompanionInfo,sizeof(CompanionInfo));

		//工作目录
		TCHAR szDirectory[MAX_PATH]=TEXT("");
		CWHService::GetWorkDirectory(szDirectory,CountArray(szDirectory));

		//打开文件
		WCHAR szInfomationFile[MAX_PATH]=L"";
		_snwprintf(szInfomationFile,CountArray(szInfomationFile),L"%s\\%s",(LPCWSTR)(CT2CW(szDirectory)),INFORMATION_FILE);

		//打开文件
		StgOpenStorage(szInfomationFile,NULL,STGM_READ|STGM_SHARE_EXCLUSIVE,0,0,&pIStorageRoot);
		if (pIStorageRoot==NULL) return false;

		//创建枚举
		pIStorageRoot->EnumElements(0,NULL,0,&pIEnumSTATSTG);
		if (pIEnumSTATSTG==NULL) throw TEXT("EnumElements Create pIEnumSTATSTG Error");

		//枚举对象
		while (pIEnumSTATSTG->Next(1,&Statstg,NULL)==NOERROR)
		{
			//目录类型
			if (Statstg.type==STGTY_STORAGE)
			{
				//打开存储
				pIStorageRoot->OpenStorage(Statstg.pwcsName,NULL,STGM_READ|STGM_SHARE_EXCLUSIVE,0,0,&pIStorageSub);
				if (pIStorageSub==NULL) throw TEXT("CreateStorage Create pIStorageSub Error");

				//打开数据
				pIStorageSub->OpenStream(STREAM_COMPANION,NULL,STGM_READ|STGM_SHARE_EXCLUSIVE,0,&pIStreamSub);

				//读取数据
				if (pIStreamSub!=NULL)
				{
					//读取关系
					ULONG cbReadCount=0L;
					pIStreamSub->Read(&CompanionInfo,sizeof(CompanionInfo),&cbReadCount);

					//读取效验
					ASSERT(cbReadCount==sizeof(CompanionInfo));
					if (cbReadCount!=sizeof(CompanionInfo)) throw TEXT("Read Data CompanionInfo Error");

					//创建对象
					tagCompanionInfo * pCompanionInfo=CreateCompanionItem();
					if (pCompanionInfo==NULL) throw TEXT("CreateCompanionItem Error");

					//设置信息
					CopyMemory(pCompanionInfo,&CompanionInfo,sizeof(CompanionInfo));
				}

				//释放接口
				SafeRelease(pIStreamSub);
				SafeRelease(pIStorageSub);
			}

			//释放内存
			CoTaskMemFree(Statstg.pwcsName);
		}

		//释放接口
		SafeRelease(pIEnumSTATSTG);
		SafeRelease(pIStreamSub);
		SafeRelease(pIStorageSub);
		SafeRelease(pIStorageRoot);

		return true;
	}
	catch (...)
	{
		//错误断言
		ASSERT(FALSE);

		//释放接口
		SafeRelease(pIEnumSTATSTG);
		SafeRelease(pIStreamSub);
		SafeRelease(pIStorageSub);
		SafeRelease(pIStorageRoot);
	}

	return false;
}
//加入关系
bool CUserCompanionList::InsertCompanionInfo(const tagCompanionItem * pCompanionInfo)
{
    //效验参数
    ASSERT(pCompanionInfo!=NULL);
    if (pCompanionInfo==NULL) return false;

    //变量定义
    INT nImageIndex=0;
    HTREEITEM hParentTreeItem=NULL;

    //插入准备
    switch (pCompanionInfo->Companion)
    {
    case enCompanion_Friend:		//游戏好友
    {
        hParentTreeItem=m_hItemFriend;
        nImageIndex=IMAGE_FRIEND_ITEM;
        break;
    }
    case enCompanion_Detest:		//厌恶玩家
    {
        hParentTreeItem=m_hItemDetest;
        nImageIndex=IMAGE_DETEST_ITEM;
        break;
    }
    default:						//默认玩家
    {
        hParentTreeItem=m_hItemNormal;
        nImageIndex=IMAGE_NORMAL_ITEM;
        break;
    }
    }

    //生成标题
    TCHAR szTreeTitle[128]=TEXT("");
    _sntprintf(szTreeTitle,CountArray(szTreeTitle),TEXT("%s [ %ld ]"),pCompanionInfo->szAccounts,pCompanionInfo->dwGameID);

    //变量定义
    TV_INSERTSTRUCT InsertStruct;
    ZeroMemory(&InsertStruct,sizeof(InsertStruct));

    //创建关系
    tagCompanionItem *pNewCompanion=CreateCompanionItem();
    if (pNewCompanion==NULL) return false;

    //设置变量
    CopyMemory(pNewCompanion,pCompanionInfo,sizeof(tagCompanionItem));

    //构造变量
    InsertStruct.hInsertAfter=TVI_LAST;
    InsertStruct.hParent=hParentTreeItem;
    InsertStruct.item.iImage=nImageIndex;
    InsertStruct.item.iSelectedImage=nImageIndex;
    InsertStruct.item.pszText=szTreeTitle;
    InsertStruct.item.lParam=(LPARAM)pNewCompanion;
    InsertStruct.item.cchTextMax=CountArray(szTreeTitle);
    InsertStruct.item.mask=TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT|TVIF_PARAM;

    //插入子项
    InsertItem(&InsertStruct);

    return true;
}
//设置关系
tagCompanionInfo * CUserInformation::InsertCompanionInfo(IClientUserItem * pIClientUserItem, BYTE cbCompanion)
{
	//效验参数
	ASSERT(pIClientUserItem!=NULL);
	if (pIClientUserItem==NULL) return NULL;

	//用户搜索
	DWORD dwUserID=pIClientUserItem->GetUserID();
	tagCompanionInfo * pCompanionInfo=SearchCompanionInfo(dwUserID);

	//设置信息
	if (pCompanionInfo==NULL)
	{
		//创建对象
		pCompanionInfo=CreateCompanionItem();
		if (pCompanionInfo==NULL) return NULL;

		//设置信息
		pCompanionInfo->cbCompanion=cbCompanion;
		pCompanionInfo->dwGameID=pIClientUserItem->GetGameID();
		pCompanionInfo->dwUserID=pIClientUserItem->GetUserID();
		lstrcpyn(pCompanionInfo->szNickName,pIClientUserItem->GetNickName(),CountArray(pCompanionInfo->szNickName));
		lstrcpyn(pCompanionInfo->szUserNote,pIClientUserItem->GetUserNoteInfo(),CountArray(pCompanionInfo->szUserNote));

		//插入通知
		if (m_pIUserCompanionSink!=NULL) m_pIUserCompanionSink->OnCompanionInsert(pCompanionInfo);
	}
	else
	{
		//修改判断
		bool bModify=false;
		if (pCompanionInfo->cbCompanion!=cbCompanion) bModify=true;
		if ((bModify==false)&&(lstrcmp(pCompanionInfo->szNickName,pIClientUserItem->GetNickName())!=0)) bModify=true;
		if ((bModify==false)&&(lstrcmp(pCompanionInfo->szUserNote,pIClientUserItem->GetUserNoteInfo())!=0)) bModify=true;

		//修改判断
		if (bModify=false) return pCompanionInfo;

		//设置信息
		pCompanionInfo->cbCompanion=cbCompanion;
		pCompanionInfo->dwGameID=pIClientUserItem->GetGameID();
		pCompanionInfo->dwUserID=pIClientUserItem->GetUserID();
		lstrcpyn(pCompanionInfo->szNickName,pIClientUserItem->GetNickName(),CountArray(pCompanionInfo->szNickName));
		lstrcpyn(pCompanionInfo->szUserNote,pIClientUserItem->GetUserNoteInfo(),CountArray(pCompanionInfo->szUserNote));

		//更新通知
		if (m_pIUserCompanionSink!=NULL) m_pIUserCompanionSink->OnCompanionUpdate(pCompanionInfo);
	}

	//变量定义
	IStream * pIStreamSub=NULL;
	IStorage * pIStorageSub=NULL;
	IStorage * pIStorageRoot=NULL;

	//写入数据
	try
	{
		//构造名字
		WCHAR szStorageName[16]=L"";
		_snwprintf(szStorageName,CountArray(szStorageName),L"%ld",dwUserID);

		//工作目录
		TCHAR szDirectory[MAX_PATH]=TEXT("");
		CWHService::GetWorkDirectory(szDirectory,CountArray(szDirectory));

		//打开文件
		WCHAR szInfomationFile[MAX_PATH]=L"";
		_snwprintf(szInfomationFile,CountArray(szInfomationFile),L"%s\\%s",(LPCWSTR)(CT2CW(szDirectory)),INFORMATION_FILE);

		//打开文件
		StgOpenStorage(szInfomationFile,NULL,STGM_READWRITE|STGM_SHARE_EXCLUSIVE,0,0,&pIStorageRoot);
		if (pIStorageRoot==NULL) StgCreateDocfile(szInfomationFile,STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,0,&pIStorageRoot);

		//错误判断
		if (pIStorageRoot==NULL) throw TEXT("StgCreateDocfile Create pIStorageRoot Error");

		//打开存储
		pIStorageRoot->CreateStorage(szStorageName,STGM_WRITE|STGM_SHARE_EXCLUSIVE,0,0,&pIStorageSub);
		if (pIStorageSub==NULL) pIStorageRoot->CreateStorage(szStorageName,STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE,0,0,&pIStorageSub);

		//错误处理
		if (pIStorageSub==NULL) throw TEXT("CreateStorage Create pIStorageSub Error");

		//创建数据
		pIStorageSub->CreateStream(STREAM_COMPANION,STGM_WRITE|STGM_SHARE_EXCLUSIVE,0,0,&pIStreamSub);
		if (pIStreamSub==NULL) pIStorageSub->CreateStream(STREAM_COMPANION,STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE,0,0,&pIStreamSub);

		//错误处理
		if (pIStreamSub==NULL) throw TEXT("CreateStream Create pIStreamSub Error");

		//写入数据
		ULONG cbWriteCount=0L;
		pIStreamSub->Write(pCompanionInfo,sizeof(tagCompanionInfo),&cbWriteCount);

		//释放接口
		SafeRelease(pIStreamSub);
		SafeRelease(pIStorageSub);
		SafeRelease(pIStorageRoot);

		return pCompanionInfo;
	}
	catch (...)
	{
		//错误断言
		ASSERT(FALSE);

		//释放接口
		SafeRelease(pIStreamSub);
		SafeRelease(pIStorageSub);
		SafeRelease(pIStorageRoot);
	}

	return NULL;
}