⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 obeximpl.cpp

📁 symbian下基于Object Exchange (OBEX)的协议进行数据传输的源代码。
💻 CPP
字号:
// Obex.cpp: implementation of the CObex class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ObexSample.h"
#include "ObexImpl.h"
#include <afxmt.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

// {F9ec7bc4-953c-11d2-984e-525400dc9e09}
GUID CLSID_FileExchange_NetOrder = {0xc47becf9, 0x3c95, 0xd211, 
{0x98, 0x4e, 0x52, 0x54, 0x00, 0xdc, 0x9e, 0x09}};

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CObex::CObex()
{
	m_dwCookie = 0;
	m_hWnd = NULL;
}

CObex::~CObex()
{
    Shutdown();
}

int CObex::InitObex(HWND hWnd) 
{
    HRESULT hr;
	CString sInfo;
	CComPtr<IObex> pObex;
	
	m_hWnd = hWnd;
	
	hr = pObex.CoCreateInstance(__uuidof(Obex),NULL,CLSCTX_INPROC_SERVER);
	
    if(FAILED(hr)) 
	{
        sInfo.Format(_T("Failed to initialize Obex interface! hr = 0x%X, err =  0x%X\n"), 
			hr, GetLastError());
		::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
        return 0;
    }
	hr = pObex->QueryInterface(__uuidof(IObex2),(void**)&m_pObex);
    if(FAILED(hr)) 
	{
        sInfo.Format(_T("Failed to initialize Obex interface! hr = 0x%X, err =  0x%X\n"), 
			hr, GetLastError());
		::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
        return 0;
    }
	pObex.Release();
	
    if (!m_pObex)
		return 0;
	
	memset(m_oDevs,0,sizeof(m_oDevs));

	m_pObex->Initialize();
	
    IObexCaps *pObexCaps = NULL;
    hr = m_pObex->QueryInterface(IID_IObexCaps, (LPVOID *)&pObexCaps);
    if(SUCCEEDED(hr)) 
	{
        pObexCaps->SetCaps(SEND_DEVICE_UPDATES);
        pObexCaps->Release();
    }
	
	CCriticalSection csLock;
	csLock.Lock(INFINITE);
	
    IObexSinkImpl *pSink = new IObexSinkImpl(hWnd);
    if (!pSink) 
	{
		csLock.Unlock();
        return 0;
    }
	
    hr = m_pObex->QueryInterface(IID_IConnectionPointContainer, (LPVOID *)&m_pContainer); 
    if (!SUCCEEDED(hr) || (m_pContainer == 0)) 
	{
		csLock.Unlock();
        return 0;
    }
	
    hr = m_pContainer->FindConnectionPoint(IID_IObexSink, &m_pConPoint);
    if (!SUCCEEDED(hr) || (m_pConPoint == 0))
	{
        m_pContainer.Release();
		csLock.Unlock();
        return 0;
    }
	
    hr = m_pConPoint->Advise((IUnknown *)pSink, &m_dwCookie);
	
	csLock.Unlock();
	
    if (ERROR_SUCCESS != m_pObex->StartDeviceEnum())
        return 0;

	return 1;
}

void CObex::Shutdown()
{
    if (m_pObex) 
	{
        m_pObex->StopDeviceEnum();
        ResetDevList ();
        if (m_pConPoint) 
		{
            m_pConPoint->Unadvise(m_dwCookie);
            Sleep(100);
            m_pConPoint.Release();
        }
        if (m_pContainer) 
		{
            m_pContainer.Release();
        }
        m_pObex->Shutdown();
        m_pObex.Release();
    }
}

int CObex::SendFile(IObexDevice *pDevice, LPTSTR pszFileName, DWORD dwFlags) 
{
    LPTSTR pszName;
    DWORD dwBytesWritten;
    int nCnt, nFileSize, nTotal;
    HRESULT hr;
    BYTE pBuff[BUFFSIZE];
	CString sInfo;
	CFile File;
	
	sInfo.Format(_T("Sending file %s"), pszFileName);
	::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
	
    memset(pBuff,0,sizeof(pBuff));
	
    pszName = wcsrchr (pszFileName, '\\');
    if (pszName == 0)
        pszName = pszFileName;
    else
        pszName++;
	
	if ( !File.Open(pszFileName,CFile::modeRead) )
		return 0;
	
    nFileSize = File.GetLength();
	
    IHeaderCollection *pHC = 0;
    hr = CoCreateInstance(__uuidof(HeaderCollection), NULL, 
		CLSCTX_INPROC_SERVER, __uuidof(IHeaderCollection),
		(void **)&pHC);
    if (FAILED(hr)) 
	{
        return -2;
    }
	
    if (dwFlags & DEV_SERVICE_FTP)
        pHC->AddTarget (sizeof (CLSID_FileExchange_NetOrder),
		(UCHAR *)&CLSID_FileExchange_NetOrder);
	
    hr = pDevice->Connect (NULL, 0, pHC);
    if (FAILED(hr)) 
	{
        sInfo.Format(_T("Failed to connect! hr = 0x%X, err =  0x%X\n"), 
			hr, GetLastError());
		::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
        pHC->Release();
        return -3;
    }
	
    sInfo = _T("Connected");
	::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
	
    IHeaderCollection *pFileHC = 0;
    hr = CoCreateInstance(__uuidof(HeaderCollection), NULL, 
		CLSCTX_INPROC_SERVER, __uuidof(IHeaderCollection),
		(void **)&pFileHC);
    if (FAILED(hr)) 
	{
        pHC->Release();
        pDevice->Disconnect (pHC);
        return -2;
    }
	
    hr = pFileHC->AddName(pszName);
    if (FAILED(hr)) 
	{
        pHC->Release();
        pFileHC->Release();
        return -3;
    }
	
    IStream *stOut = 0;    
    hr = pDevice->Put(pFileHC, &stOut);  
    if (FAILED(hr)) 
	{
        pDevice->Disconnect (pHC);
        pHC->Release();
        pFileHC->Release();
        return -4;
    }
	
    nTotal = nFileSize;
    while (nFileSize) 
	{
        nCnt = __min (BUFFSIZE, nFileSize);
		
        dwBytesWritten = File.Read(pBuff, nCnt);
        if (!dwBytesWritten) 
		{
			sInfo.Format(_T("ReadFile error %d"), GetLastError());
			::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
            break;
        }
        nCnt = (int)dwBytesWritten;
		
        sInfo.Format(_T("sending %d bytes"), nCnt);
		::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
		
        hr = stOut->Write (pBuff, nCnt, &dwBytesWritten);
        if(FAILED(hr)) 
		{
			sInfo.Format(_T("send error: hr = 0x%X, err = 0x%X"), hr, GetLastError());
			::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
            break;
        }
        nFileSize -= (int)dwBytesWritten;
		sInfo.Format(_T("%d%% sent"), (nTotal-nFileSize)*100/nTotal);
		::SendMessage(m_hWnd, WM_OBEX_INFO,0,(LPARAM)&sInfo);
	}
    stOut->Commit (STGC_DEFAULT);

    stOut->Release();
    pDevice->Disconnect (pHC);
    if(pHC)
        pHC->Release();
    if(pFileHC)
        pFileHC->Release();
    return 0;
}

int CObex::ResetDevList() 
{
    for (int i = 0; i < sizeof(m_oDevs)/sizeof(OBEXDEVICEINFO); i++) 
	{
        if (m_oDevs[i].pDevBag) 
		{
            m_oDevs[i].pDevBag->Release();
            m_oDevs[i].pDevBag = 0;
        }
    }
    return 0;
}

POBEXDEVICEINFO CObex::FindDevInfo(IPropertyBag* pDevBag) 
{
    for (int i = 0, j = -1; i < sizeof(m_oDevs)/sizeof(OBEXDEVICEINFO); i++) 
	{
        if (m_oDevs[i].pDevBag == pDevBag)
            return &m_oDevs[i];
		
        if ((j == -1) && (m_oDevs[i].pDevBag == 0)) 
		{
            j = i;
        }
    }
    return &m_oDevs[j];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -