pcdmsyncobjhand.cpp
来自「《Windows CE 权威指南》(作者:(美)CHRIS MUENCH」· C++ 代码 · 共 310 行
CPP
310 行
//
// File:
// =====
// PCDMSyncObjHand.cpp: Implementation File
//
// Description:
// ============
// Class implements the IReplObjHandler interface - Object
// handler that serializes/deserializes CPCDMSyncFolder
// objects.
//
#include "stdafx.h"
#include <cesync.h>
#include "PCDMSyncFldr.h"
#include "PCDMSyncObjHand.h"
//<BOOK_ADDON Chapter 9.3.2.1> ***************************************************
#include "syncbuffer.h"
#import "D:\Projects\Hamlet\Final\Sources\ThreadSample\PCDMDLL\Step7\DebugU\pcdmdll.dll" rename_namespace("PCDMdll_space") raw_interfaces_only
using namespace PCDMdll_space;
//</BOOK_ADDON Chapter 9.3.2.1> ***************************************************
CPCDMSyncObjHandler::CPCDMSyncObjHandler(CPCDMSyncFolder *pFolder): m_cRef(1) // initial count of 1
{
m_pFolder = pFolder;
m_pWriteSetup = NULL;
m_pReadSetup = NULL;
m_pReadPacket = NULL;
}
CPCDMSyncObjHandler::~CPCDMSyncObjHandler()
{
if(m_pReadPacket)
{
delete [] m_pReadPacket;
m_pReadPacket = NULL;
}
}
//
// ======================== IUnkown Implementation =======================
//
STDMETHODIMP CPCDMSyncObjHandler::QueryInterface(const IID& iid, void **ppv)
{
if(ppv == NULL)
return E_INVALIDARG;
if(::IsEqualIID(iid, IID_IUnknown))
{
*ppv = static_cast<IUnknown*>(this);
}
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
reinterpret_cast<IUnknown *>(*ppv)->AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG) CPCDMSyncObjHandler::AddRef()
{
return ::InterlockedIncrement(&m_cRef);
}
STDMETHODIMP_(ULONG) CPCDMSyncObjHandler::Release()
{
if(::InterlockedDecrement(&m_cRef) == 0)
{
delete this;
return 0;
}
return m_cRef;
}
//
// ======================== IReplObjHandler Implementation =======================
//
STDMETHODIMP CPCDMSyncObjHandler::Setup(PREPLSETUP pSetup)
{
if(pSetup == NULL)
return E_INVALIDARG;
//
// TODO: Do any initializing before we start reading/writing
// here. Since we could be reading & writing at the same time,
// we need to save the pointer to the setup struct differently
//
if(pSetup->fRead) // We're going to be reading data
{
m_pReadSetup = pSetup;
//
// TODO: Add any initialization code for reading data
// from the device here. GetPacket() will be called next.
//
m_pReadPacket = new BYTE[MAXDATASIZE]; // we can handle upto MAXDATASIZE bytes of data
if(m_pReadPacket == NULL)
return E_OUTOFMEMORY;
}
else // We're going to be writing data
{
m_pWriteSetup = pSetup;
//
// TODO: Add any initialization code for writing data
// to the device here. SetPacket() will be called next.
//
}
return S_OK;
}
STDMETHODIMP CPCDMSyncObjHandler::Reset(PREPLSETUP pSetup)
{
if(pSetup == NULL)
return E_INVALIDARG;
//
// TODO: Perform any clean up as a result of the read/write
// Setup() calls, here.
//
if(m_pReadPacket)
{
delete [] m_pReadPacket;
m_pReadPacket = NULL;
}
return S_OK;
}
//<BOOK_ADDON Chapter 9.3.2.1> ***************************************************
int PCDMTracksIDs[]= { 0,2,1 };
//</BOOK_ADDON Chapter 9.3.2.1> ***************************************************
STDMETHODIMP CPCDMSyncObjHandler::GetPacket(LPBYTE *lppbPacket, DWORD *pcbPacket, DWORD cbRecommend)
{
if((lppbPacket == NULL)||(pcbPacket == NULL))
return E_INVALIDARG;
*lppbPacket = NULL;
*pcbPacket = NULL;
//
// TODO: Active Sync manager calls this function to get the object
// (m_pReadSetup->hItem), streamed into the memory location *lppbPacket.
// You will need to break up the object into packets of size <= cbRecommend.
// It is your responsibility to obtain & free the required memory for each packet.
// When done with all the packets return RWRN_LAST_PACKET, else, return
// S_OK.
//
// We open the sample text file, and stream its data into the buffer
// pointed to by m_pReadPacket.
//
CReplItem *pItem = reinterpret_cast<CReplItem*>(m_pReadSetup->hItem);
if(pItem == NULL)
return RERR_BAD_OBJECT;
//<BOOK_ADDON Chapter 9.3.2.1> ***************************************************
OutputDebugString(TEXT("In GetPacket...\n\r"));
_RecordsetPtr tRs;
_ConnectionPtr tConn;
_variant_t vaEmpty;
_bstr_t bsEmpty = L"";
HRESULT hr;
TCHAR id[20];
int i,max;
WCHAR tStr[255];
VARIANT value,tValue;
VariantInit(&value);
VariantInit(&tValue);
_bstr_t strBuffer(L"PCDMSync://PCDM/PCDMAccessDB?Command=1&Table=PCDMCDs");
hr=tConn.CreateInstance( __uuidof( Connection ) );
if (FAILED(hr)) return RERR_BAD_OBJECT;
tConn->ConnectionString = L"Provider=MSDASQL.1;Persist Security Info=False;Data Source=DeluxeCD";
hr=tConn->Open( bsEmpty, bsEmpty, bsEmpty, -1 );
if (FAILED(hr)) return RERR_BAD_OBJECT;
hr=tRs.CreateInstance( __uuidof( Recordset ) );
if (FAILED(hr)) return RERR_BAD_OBJECT;
_bstr_t bsSel(L"SELECT * FROM Tracks where TitleID=");
FILETIME& ftOld=pItem->GetModified();
wsprintf(id,TEXT("%d"),ftOld.dwHighDateTime);
bsSel+=id;
tRs->PutRefActiveConnection( tConn );
strBuffer+=pItem->GetId();
tRs->Open(bsSel,vtMissing,adOpenKeyset,adLockBatchOptimistic,-1);
while (!tRs->EOF)
{
strBuffer+=L"PCDMSync://PCDM/PCDMAccessDB?Command=1&Table=PCDMTracks&";
max=tRs->Fields->GetCount();
for (i=0;i<max;i++)
{
VariantClear(&value); VariantClear(&tValue);
VariantCopy(&value,&tRs->Fields->Item[(long)i]->Value);
VariantChangeType(&tValue,&value,0,VT_BSTR);
if (lstrlenW(tValue.bstrVal)!=0)
wsprintfW(tStr,L"Fld%d=%s",PCDMTracksIDs[i],tValue.bstrVal);
else
wsprintfW(tStr,L"Fld%d=n.a.",PCDMTracksIDs[i]);
strBuffer+=tStr;
if (i<2)
strBuffer+=L"&";
else
strBuffer+=L";";
}
tRs->MoveNext();
}
hr=RWRN_LAST_PACKET;
OutputDebugString(strBuffer);
OutputDebugString(TEXT("\n\r"));
memset(m_pReadPacket,0,MAXDATASIZE);
memcpy(m_pReadPacket,(WCHAR *)strBuffer,(strBuffer.length()+1)*sizeof(WCHAR));
*lppbPacket = m_pReadPacket;
*pcbPacket = (strBuffer.length()+1)*sizeof(WCHAR);
tRs->Close();
OutputDebugString(TEXT("GP:Out..\n\r"));
return hr;
//</BOOK_ADDON Chapter 9.3.2.1> ***************************************************
}
STDMETHODIMP CPCDMSyncObjHandler::SetPacket(LPBYTE lpbPacket, DWORD cbPacket)
{
if(lpbPacket == NULL)
return E_INVALIDARG;
CReplItem *pItem = NULL;
//
// TODO: Active Sync manager calls this function to set an item
// (m_pReadSetup->hItem), streamed into the memory location lpbPacket.
// The item may be new or could be an existing item. The data for
// the item may also be broken into more than one packet.
// This function will be called repeatedly until the last
// packet has been received.
//
if(!(m_pWriteSetup->dwFlags & RSF_NEW_OBJECT)) // Item already exists
{
// TODO: Find the folder item in your folder
// & update its data with the data received thru
// this packet
pItem = reinterpret_cast<CReplItem*>(m_pWriteSetup->hItem);
}
else // New Item
{
// TODO: Create a new item in your folder,
// update its data with the data received thru
// this packet and set m_pWriteSetup->hItem
// to new handle. <winerror.h>
//
pItem = new CReplItem(m_pFolder->GetSyncFileName());
m_pWriteSetup->hItem = reinterpret_cast<HREPLITEM>(pItem);
}
//
// We always create and update the sample file
//
//<BOOK_ADDON Chapter 9.3.2.1> ***************************************************
OutputDebugString(TEXT("In SetPacket...\n\r"));
_ConnectionPtr tConn;
_variant_t vaEmpty;
_bstr_t bsEmpty = L"";
PSYNCBUFFERUP pSync=(PSYNCBUFFERUP)lpbPacket;
_bstr_t bsSel((WCHAR *)pSync->str);
HRESULT hr=tConn.CreateInstance( __uuidof( Connection ) );
if (FAILED(hr)) return S_OK;
tConn->ConnectionString = L"Provider=MSDASQL.1;Persist Security Info=False;Data Source=DeluxeCD";
hr=tConn->Open( bsEmpty, bsEmpty, bsEmpty, -1 );
if (FAILED(hr)) return S_OK;
OutputDebugString(bsSel);
OutputDebugString(TEXT("\n\r"));
IPCDMUtilsPtr comUtils;
hr=comUtils.CreateInstance(__uuidof( PCDMUtils ) );
if (FAILED(hr))
{
OutputDebugString(TEXT("SETPACKET: Could not access the PCDMUtils Class\n\r"));
return S_OK;
}
int idx=0;
BSTR strURL;
while (comUtils->GetURL(idx,bsSel,&strURL)==S_OK)
{
hr=tConn->Execute(strURL,&vaEmpty,0);
idx++;
}
FILETIME oldft;
oldft.dwHighDateTime=pSync->ID;
pItem->SetModified(oldft);
//<BOOK_ADDON Chapter 9.3.2.1> ***************************************************
return S_OK;
}
STDMETHODIMP CPCDMSyncObjHandler::DeleteObj(PREPLSETUP pSetup)
{
if(pSetup == NULL)
return E_INVALIDARG;
//
// TODO: ActiveSync manager calls this function when an
// folder item has been deleted from the device. You will need
// to delete the item from your folder pointed by(pSetup->hItem).
//
return m_pFolder->DeleteItem(pSetup->hItem);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?