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

📄 dbase.cpp

📁 Windows CE .Net 下面 VOIP编程的经典实例。对于初学Windows 平台下VOIP编程技术的程序员颇具借鉴意义!
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/***************************************************************************


  Disclaimer:

    This code and information is provided "as is" without warranty of
    any kind, either expressed or implied, including but not limited to
    the implied warranties of merchantability and/or fitness for a
    particular purpose.


Module Name:

    DBase.cpp

Abstract:

    Database handling (buddies list with machine name, email and presence info)

Notes:

******************************************************************************/

#include "stdafx.h"
#include "dbase.h"
#include "rtcs.h"

static HINSTANCE s_hinst;

// Properties stored in the database (at least the ones we're interested in)
const CEPROPID rgReadPropID[] = { PROPID_NAME, PROPID_BLOB, PROPID_ADDR, PROPID_STAT, PROPID_TYPE };

// Initialize database and return handle to it
HANDLE MyAddrDB_Init( HINSTANCE hinst )
{
	s_hinst = hinst;
	return MyAddrDB_OpenCreate();
}


// Close database
void MyAddrDB_Close( HANDLE hMyAddrDB )
{
	CloseHandle( hMyAddrDB );
}


// Check if database exists -> if not, create it (otherwise just open) and return handle
HANDLE MyAddrDB_OpenCreate( void )
{
	CEOID	oidMyAddrDB = 0;
	HANDLE	hMyAddrDB = 0;

	for(int i=0; i<2; i++)
	{
		if( INVALID_HANDLE_VALUE == ( hMyAddrDB = CeOpenDatabase( &oidMyAddrDB, ADDRDB_NAME, PROPID_NAME, CEDB_AUTOINCREMENT, NULL ) ) ) 
		{
			SORTORDERSPEC so[2];
			so[0].propid = PROPID_NAME;
			so[0].dwFlags = CEDB_SORT_CASEINSENSITIVE;
			so[1].propid = PROPID_TYPE;
			so[1].dwFlags = CEDB_SORT_DESCENDING;
				
			if( !( oidMyAddrDB = CeCreateDatabase( ADDRDB_NAME, 0, 2, so) ) )
			{
				RETAILMSG(1, ( L"Failed to open or create CEOS Address Database\r\n" ) );
				return 0;
			}
		}
	}
	ASSERT(hMyAddrDB && (hMyAddrDB != INVALID_HANDLE_VALUE));
	
	return hMyAddrDB;			
}


// Delete a record from the database (all the properties, not only what we're interested in)
BOOL MyAddrDB_Delete( HANDLE hMyAddrDB, CEOID oid )
{
	return CeDeleteRecord( hMyAddrDB, oid );
}


// Write (or update) a record in the database (only properties we're interested in), return oid (new or updated)
CEOID MyAddrDB_Write( HANDLE hMyAddrDB, CEOID oid, LPTSTR pszName, LPTSTR pszBlob, LPTSTR pszAddr, int nStatus, int nType )
{
	CEPROPVAL rgPropVal[5];
	DEBUGMSG( ZONE_STATUS, ( L"VoIPDemo: Updating address database for %s\r\n", pszName ) );

	ASSERT( hMyAddrDB && ( hMyAddrDB != INVALID_HANDLE_VALUE ) );
	ASSERT( pszName && pszAddr );
	memset(rgPropVal, 0, sizeof(rgPropVal));
	rgPropVal[0].propid = PROPID_NAME;
	rgPropVal[1].propid = PROPID_BLOB;
	rgPropVal[2].propid = PROPID_ADDR;
	rgPropVal[3].propid = PROPID_STAT;
	rgPropVal[4].propid = PROPID_TYPE;
	rgPropVal[0].val.lpwstr = pszName;
	rgPropVal[1].val.lpwstr = pszBlob;
	rgPropVal[2].val.lpwstr = pszAddr;
	rgPropVal[3].val.lVal = nStatus;
	rgPropVal[4].val.lVal = nType;

	if( !( oid = CeWriteRecordProps( hMyAddrDB, oid, 5, rgPropVal ) ) )
	{	
		DEBUGMSG( ZONE_ERROR, ( L"VoIPDemo: MyAddrDB_Write(0x%0x) failed(GLE=%d)\r\n", oid, GetLastError() ) );
	}
	return oid;
}

// Read record from database (only properties we're interested in), returns OID on success, 0 on eof & -1 on error
CEOID MyAddrDB_Read( HANDLE hMyAddrDB, CEOID oidIn, CEPROPVAL** ppPropVal )
{
	CEOID oidOut;
	DWORD dwIndex, cb;
	WORD wPropCount;
	
	ASSERT(hMyAddrDB && (hMyAddrDB != INVALID_HANDLE_VALUE));
	
	*ppPropVal=0;
	
	if(oidIn) 
	{
		if(!CeSeekDatabase(hMyAddrDB, CEDB_SEEK_CEOID, oidIn, &dwIndex))
		{
			DEBUGMSG(ZONE_ERROR, (L"MyAddrDB_Read failed seeking to 0x%04x (GLE=%d)\r\n", oidIn, GetLastError()));
			return -1;
		}
	}
	cb=0;
	wPropCount=5;
	if( !( oidOut = CeReadRecordProps(hMyAddrDB, CEDB_ALLOWREALLOC, &wPropCount, ( CEPROPID* )rgReadPropID, ( LPBYTE* )ppPropVal, &cb) ) )
	{
		ASSERT( *ppPropVal==0 );
		if( GetLastError() == ERROR_NO_MORE_ITEMS )
			return 0;
		else 
		{
			DEBUGMSG(ZONE_ERROR, (L"MyAddrDB_Read failed reading (GLE=%d)\r\n", GetLastError()));
			return -1;
		}
	}
	if( ( wPropCount != 5) || !cb ||
		( ( *ppPropVal )[0].wFlags & CEDB_PROPNOTFOUND ) ||
		( ( *ppPropVal )[1].wFlags & CEDB_PROPNOTFOUND ) ||
		( ( *ppPropVal )[2].wFlags & CEDB_PROPNOTFOUND ) ||
		( ( *ppPropVal )[3].wFlags & CEDB_PROPNOTFOUND ) ||
		( ( *ppPropVal )[4].wFlags & CEDB_PROPNOTFOUND ) )
	{
		DEBUGMSG( ZONE_ERROR, ( L"MyAddrDB_Read error reading: Got count=%d bytes=%d flags=%d %d %d %d\r\n", 
			wPropCount, cb, (*ppPropVal)[0].wFlags, (*ppPropVal)[1].wFlags, (*ppPropVal)[2].wFlags, (*ppPropVal)[3].wFlags, (*ppPropVal)[4].wFlags ) );
		LocalFree( *ppPropVal );
		*ppPropVal=0;

		return -1;
	}
	return oidOut;
}

// Find the buddy by name, or by name and buddy type
//
// if the name is NULL, it's assumed we're looking for the OID of the first buddy of type
// iBt
CEOID MyAddrDB_Find( IN HANDLE hMyAddrDB, IN BSTR bstrName, IN INT iBt ) 
{

	DWORD dwIndex;

	CEOID oid = 0;
	BOOL bFound = FALSE;

	CEPROPVAL sPropVal;
	sPropVal.wFlags = 0;	
	
	if (!bstrName) {
		// just looking for my own identifier, have to cycle through the DB for now

		while (!bFound) {
			CEPROPVAL* pPropVal = NULL;
			oid = MyAddrDB_Read(hMyAddrDB, 0, &pPropVal);
			
			if (!oid || oid == -1 || !pPropVal) {
				return 0;
			}
			
			if (pPropVal[DBF_TYPE].val.lVal == iBt) {
				// here's our guy
				bFound = TRUE;			
			}
			
			LocalFree(pPropVal);		
		}
	} else {
		// look by name and type
		sPropVal.propid = PROPID_NAME;
		sPropVal.val.lpwstr = bstrName;
		
		for (int i = 0; i < 2 && !bFound; i++) {
			oid = CeSeekDatabase(
				hMyAddrDB,					//HANDLE hDatabase, 
				(i == 0) ? CEDB_SEEK_VALUEFIRSTEQUAL : CEDB_SEEK_VALUENEXTEQUAL,	//DWORD dwSeekType, 
				(DWORD) &sPropVal,				//DWORD dwValue, 
				&dwIndex					//LPDWORD lpdwIndex
				); 
			
			if (!oid || oid == -1) {
				return 0;
			}
			
			CEPROPVAL* pPropVal = NULL;
			MyAddrDB_Read( hMyAddrDB, oid, &pPropVal);
			
			if (!pPropVal) {
				return 0;
			}
			
			if (iBt == 0 || pPropVal[DBF_TYPE].val.lVal == iBt) {
				//It's the buddy we want, with the right type (or we don't care about type)
				bFound = TRUE;
			}
			
			LocalFree(pPropVal);
		}
	} // end else
	
	if (bFound)
		return oid;
	else
		return 0;
}

// Seek to the beginning of the database
int MyAddrDB_Reset( HANDLE hMyAddrDB )
{
	DWORD dwIndex;
	
	if( !CeSeekDatabase(hMyAddrDB, CEDB_SEEK_BEGINNING, 0, &dwIndex ) )
	{
		DEBUGMSG(ZONE_ERROR, (L"MyAddrDB_Reset failed seeking to beginning (GLE=%d)\r\n", GetLastError()));
		return -1;
	}

	return 0;
}

⌨️ 快捷键说明

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