#pragma once #include <vector> using namespace std; typedef struct RECORDINFO { WCHAR wsProName[64]; //process name double dbCount[3]; //traffic data int iConnType; //connection type RECORDINFO() { memset(dbCount, 0x0, sizeof(dbCount)); } }RECORD_INFO; class CDBOperater { public: CDBOperater(); ~CDBOperater(); public: BOOL InstallDBVol(); BOOL OpenDatabase(LPTSTR szDBName); BOOL CloseDatabase(HANDLE handleDB); BOOL UninstallDBVol(CEGUID ceGuidVol); BOOL ReadRecordFromDB(HANDLE handleDB, vector<RECORD_INFO> & processCount); BOOL WriteRecordToDB(HANDLE handleDB, TCHAR * szProcessName, double iTraData, int szConnType); private: BOOL CreateDatabase(LPTSTR szDBName); public: CEGUID m_ceGuidVol; //The ID of the database vol HANDLE m_handleDBToday; //The handle of the database named today private: CEOID m_oidDatabase; //The ID of the database }; #ifndef EDB #define EDB #endif #include "stdafx.h" #include "debug.h" #include "FnRetCk.h" #include "DBOperate.h" #include <vector> #ifndef EDB extern "C" { #include <windbase_edb.h> } #endif using namespace std; #define MAKEPROP(n,t) ((n<<16)|CEVT_##t) #define PROP_NAME MAKEPROP(20, LPWSTR) //define the ID of the process name #define PROP_DATA MAKEPROP(30, R8) //define the ID of the GPRS traffic data #define PROP_DATA_WIFI MAKEPROP(28, R8) //define the ID of the WIFI traffic data #define PROP_DATA_USB MAKEPROP(32, R8) //define the ID of the USB traffic data #define PROP_TYPE MAKEPROP(10, UI2) //define the ID of the connection type #define PROPERTY_NUM 5 //The numbers of property, there are five propertys #define DATABASE_VOLUME L"//Windows//Wireless Monitor.vol" //define the root of database volume CDBOperater::CDBOperater() { DBG_MSG(MSG_INFO, L"+CDBOperater::CDBOperater"); //m_ceGuidVol = {0}; m_oidDatabase = 0; m_handleDBToday = NULL; DBG_MSG(MSG_INFO, L"-CDBOperater::CDBOperater"); } CDBOperater::~CDBOperater() { DBG_MSG(MSG_INFO, L"+CDBOperater::~CDBOperater"); DBG_MSG(MSG_INFO, L"-CDBOperater::~CDBOperater"); } BOOL CDBOperater::InstallDBVol() { DBG_MSG(MSG_INFO, L"+CDBOperater::CreateDBVol"); BOOL bRet; //Return value BOOL bRes; //API's return value bRet = FALSE; bRes = FALSE; bRes = CeMountDBVolEx(&m_ceGuidVol, DATABASE_VOLUME, NULL, OPEN_ALWAYS); BEBR(bRes, L"Mount the database volume failed.%d", GetLastError()); bRet = TRUE; Exit: DBG_MSG(MSG_INFO, L"-CDBOperater::CreateDBVol"); return bRet; } BOOL CDBOperater::UninstallDBVol(CEGUID ceGuidVol) { DBG_MSG(MSG_INFO, L"+CDBOperater::UnInstallDBVol"); BOOL bRet; //Return value BOOL bRes; //API's return value bRet = FALSE; bRes = FALSE; //Before Unmount the database volume //1. Close all the opened databases //2. Save volume data into permanent storage medium bRes = CeUnmountDBVol(&ceGuidVol); BW(bRes, L"Unmount the data base volume failed."); bRet = TRUE; DBG_MSG(MSG_INFO, L"-CDBOperater::UnInstallDBVol"); return bRet; } BOOL CDBOperater::OpenDatabase(LPTSTR szDBName) { DBG_MSG(MSG_INFO, L"+CDBOperater::OpenDatabase"); BOOL bRet; //Return value BOOL bRes; //API's return value HANDLE handleSession; //The handle of session, used for open the database bRet = FALSE; bRes = FALSE; handleSession = NULL; handleSession = CeCreateSession(&m_ceGuidVol); if (INVALID_HANDLE_VALUE == handleSession) { DBG_MSG(MSG_ERR, L"Create the session failed.%d", GetLastError()); bRet = FALSE; goto Exit; } m_handleDBToday = CeOpenDatabaseInSession(handleSession, &m_ceGuidVol, &m_oidDatabase, szDBName, NULL, CEDB_AUTOINCREMENT, NULL); if (INVALID_HANDLE_VALUE == m_handleDBToday) { DBG_MSG(MSG_TRK, L"Open the database failed."); DBG_MSG(MSG_TRK, L"Try to create the database."); //Maybe the database did not existent //Create a database with the passed database name if (!CreateDatabase(_T("today"))) { //Create the database failed. goto exit and un-install the database vol UninstallDBVol(m_ceGuidVol); bRet = FALSE; goto Exit; } else { //Create the database succeed. //Try to open the database again DBG_MSG(MSG_TRK, L"Create the database succeed. @_@"); m_handleDBToday = CeOpenDatabaseInSession(handleSession, &m_ceGuidVol, &m_oidDatabase, szDBName, NULL, CEDB_AUTOINCREMENT, NULL); if (INVALID_HANDLE_VALUE == m_handleDBToday) { DBG_MSG(MSG_ERR, L"Open the database failed again.-_-"); UninstallDBVol(m_ceGuidVol); bRet = FALSE; goto Exit; } } } bRet = TRUE; Exit: DBG_MSG(MSG_INFO, L"-CDBOperater::OpenDatabase"); return bRet; } BOOL CDBOperater::CloseDatabase(HANDLE handleDB) { DBG_MSG(MSG_INFO, L"+CDBOperater::CloseDatabase"); BOOL bRet; //Return value BOOL bRes; //API's return value bRet = FALSE; bRes = FALSE; //Check the handle of database whether availability if (handleDB == NULL) { bRet = FALSE; goto Exit; } //Close the database bRes = CloseHandle(handleDB); BW(bRes, L"Close the handle failed."); //Save volume data into permanent storage medium bRes = CeFlushDBVol(&m_ceGuidVol); BW(bRes, L"Save volume data into permanent storage medium failed."); bRet = TRUE; Exit: DBG_MSG(MSG_INFO, L"-CDBOperater::CloseDatabase"); return bRet; } BOOL CDBOperater::CreateDatabase(LPTSTR szDBName) { DBG_MSG(MSG_INFO, L"+CDBOperater::CreateDatabase"); BOOL bRet; //Return value BOOL bRes; //API's return value CEDBASEINFOEX dbObjInfo; //information about a database object CEPROPSPEC propspec[PROPERTY_NUM]; //information for the specified property bRet = FALSE; bRes = FALSE; ZeroMemory(&dbObjInfo, sizeof(CEDBASEINFOEX)); ZeroMemory(&propspec, sizeof(CEPROPSPEC) * PROPERTY_NUM); dbObjInfo.wVersion = 2; //Version of this structure. Must be set to 2. dbObjInfo.dwFlags = CEDB_VALIDDBFLAGS | CEDB_VALIDNAME | CEDB_VALIDSORTSPEC; dbObjInfo.dwDbaseType = 0; _tcscpy(dbObjInfo.szDbaseName, szDBName); //The first property information -process name propspec[0].wVersion = 1; propspec[0].propid = PROP_NAME; propspec[0].dwFlags = 0; propspec[0].pwszPropName = L"name"; propspec[0].cchPropName = CEDB_MAXDBASENAMELEN; //The second property information - CPRS traffic data propspec[1].wVersion = 1; propspec[1].propid = PROP_DATA; propspec[1].dwFlags = 0; propspec[1].pwszPropName = L"data"; propspec[1].cchPropName = CEDB_MAXDBASENAMELEN; //The third property information - WIFI traffic data propspec[2].wVersion = 1; propspec[2].propid = PROP_DATA_WIFI; propspec[2].dwFlags = 0; propspec[2].pwszPropName = L"data_wifi"; propspec[2].cchPropName = CEDB_MAXDBASENAMELEN; //The fourth property information - USB traffic data propspec[3].wVersion = 1; propspec[3].propid = PROP_DATA_USB; propspec[3].dwFlags = 0; propspec[3].pwszPropName = L"data_usb"; propspec[3].cchPropName = CEDB_MAXDBASENAMELEN; //The third property information - connection type propspec[4].wVersion = 1; propspec[4].propid = PROP_TYPE; propspec[4].dwFlags = 0; propspec[4].pwszPropName = L"type"; propspec[4].cchPropName = CEDB_MAXDBASENAMELEN; m_oidDatabase = CeCreateDatabaseWithProps(&m_ceGuidVol, &dbObjInfo, PROPERTY_NUM, propspec); if (m_oidDatabase == NULL) { DBG_MSG(MSG_ERR, L"Create the database filed.%d", GetLastError()); bRet = FALSE; goto Exit; } bRet = TRUE; Exit: DBG_MSG(MSG_INFO, L"-CDBOperater::CreateDatabase"); return bRet; } BOOL CDBOperater::WriteRecordToDB(HANDLE handleDB, TCHAR * szProcessName, double iTraData, int szConnType) { DBG_MSG(MSG_INFO, L"+CDBOperater::WriteRecordToDB"); BOOL bRet; //Return value WORD wNumProps; //The numbers of property DWORD dwBufSize; //????????????????????? CEOID oidUpdate; //The ID of the record will be updated CEOID oidProperty; //The ID of the record read form database double dbTrafficDataGprs; //The GPRS traffic data double dbTrafficDataWifi; //The WIFI traffic data double dbTrafficDatausb; //The USB traffic data CEPROPVAL *pPropertyFDB; //The property value read from the database CEPROPVAL propertyValue[PROPERTY_NUM]; //The property value will write to database bRet = FALSE; wNumProps = 0; dwBufSize = 0; oidUpdate = 0; oidProperty = 0; dbTrafficDataGprs = 0.0; dbTrafficDataWifi = 0.0; dbTrafficDatausb = 0.0; pPropertyFDB = NULL; ZeroMemory(&propertyValue, sizeof(CEPROPVAL) * 5); //Fill the property values //The process name propertyValue[0].propid = PROP_NAME; propertyValue[0].val.lpwstr = (wchar_t*)szProcessName; propertyValue[0].wFlags = 0; //The GPRS traffic data propertyValue[1].propid = PROP_DATA; if (0 == szConnType) { propertyValue[1].val.dblVal = iTraData; } propertyValue[1].wFlags = 0; //the WIFI traffic data propertyValue[2].propid = PROP_DATA_WIFI; if (1 == szConnType) { propertyValue[2].val.dblVal = iTraData; } propertyValue[2].wFlags = 0; //The USB traffic data propertyValue[3].propid = PROP_DATA_USB; if (2 == szConnType) { propertyValue[3].val.dblVal = iTraData; } propertyValue[3].wFlags = 0; //the connection type propertyValue[4].propid = PROP_TYPE; propertyValue[4].val.uiVal = szConnType; propertyValue[4].wFlags = 0; //Seek the database pointer to begin oidProperty = CeSeekDatabaseEx(handleDB, CEDB_SEEK_BEGINNING, 0, 0, NULL); if (oidProperty == 0) { //This is the first record if (0 == CeWriteRecordProps(handleDB, 0, PROPERTY_NUM, propertyValue)) { DBG_MSG(MSG_WARN, L"Write the record to the Database failed.%d", GetLastError()); } } else { BOOL bExistent = FALSE; //is the name exist in the data base //Read all the records while (oidProperty = CeReadRecordPropsEx(handleDB, CEDB_ALLOWREALLOC, &wNumProps, NULL, (LPBYTE *)&pPropertyFDB, &dwBufSize, NULL)) { for (int i = 0; i < wNumProps; i ++) { switch(pPropertyFDB[i].propid) { case PROP_NAME: { if (0 == wcsncmp(szProcessName, pPropertyFDB[i].val.lpwstr, 256)) { //The name has existed in the data base oidUpdate = oidProperty; bExistent = TRUE; } } break; case PROP_DATA: { if (0 != szConnType && bExistent == TRUE) { //Save the GPRS traffic data dbTrafficDataGprs = pPropertyFDB[i].val.dblVal; } } break; case PROP_DATA_WIFI: { if (1 != szConnType && bExistent == TRUE) { //Save the WIFI traffic data dbTrafficDataWifi = pPropertyFDB[i].val.dblVal; } } break; case PROP_DATA_USB: { if (2 != szConnType && bExistent == TRUE) { //Save the USB traffic data dbTrafficDatausb = pPropertyFDB[i].val.dblVal; } } break; } } //if one of the record has existed in the database, break the while if (bExistent) { break; } } if (bExistent) { //Update the existent record //propertyValue[1].val.dblVal = iTraData + dbTrafficData; if (0 != szConnType) { propertyValue[1].val.dblVal = dbTrafficDataGprs; } if (1 != szConnType) { propertyValue[2].val.dblVal = dbTrafficDataWifi; } if (2 != szConnType) { propertyValue[3].val.dblVal = dbTrafficDatausb; } if (0 == CeWriteRecordProps(handleDB, oidUpdate, PROPERTY_NUM, propertyValue)) { DBG_MSG(MSG_WARN, L"Write the record to the Database failed.%d", GetLastError()); bRet = FALSE; goto Exit; } bExistent = FALSE; bRet = TRUE; goto Exit; } //if the record was not existed in the database //Insert the this record into the database if (0 == CeWriteRecordProps(handleDB, 0, PROPERTY_NUM, propertyValue)) { DBG_MSG(MSG_WARN, L"Write the record to the Database failed.%d", GetLastError()); bRet = FALSE; goto Exit; } } bRet = TRUE; Exit: DBG_MSG(MSG_INFO, L"-CDBOperater::WriteRecordToDB"); return bRet; } BOOL CDBOperater::ReadRecordFromDB(HANDLE handleDB, vector<RECORD_INFO> & processCount) { DBG_MSG(MSG_INFO, L"+CDBOperater::ReadRecordFromDB"); BOOL bRet; //return value WORD wNumProps; //The numbers of property DWORD dwBufSize; //???????????????????? CEOID oidRecord; //The ID of records CEPROPVAL *pPorpValue; //The property value RECORD_INFO recordinfo; //All the records info bRet = FALSE; wNumProps = 0; dwBufSize = 0; oidRecord = 0; pPorpValue = NULL; ZeroMemory(&recordinfo, sizeof(RECORD_INFO)); //Seek the pointer of database in the beginning if (0 == CeSeekDatabaseEx(handleDB, CEDB_SEEK_BEGINNING, 0, 0, NULL)) { DBG_MSG(MSG_WARN, L"Seek Error! %d", GetLastError()); bRet = FALSE; goto Exit; } while (oidRecord = CeReadRecordPropsEx(handleDB, CEDB_ALLOWREALLOC, &wNumProps, NULL, (LPBYTE *)&pPorpValue, &dwBufSize, NULL)) { for (int i = 0; i < wNumProps; i ++) { switch(pPorpValue[i].propid) { case PROP_NAME: { ::StringCchCopyW(recordinfo.wsProName, 64, (TCHAR*)pPorpValue[i].val.lpwstr); } break; case PROP_DATA: { recordinfo.dbCount[0] = pPorpValue[i].val.dblVal; } break; case PROP_DATA_WIFI: { recordinfo.dbCount[1] = pPorpValue[i].val.dblVal; } break; case PROP_DATA_USB: { recordinfo.dbCount[2] = pPorpValue[i].val.dblVal; } break; case PROP_TYPE: { recordinfo.iConnType = pPorpValue[i].val.uiVal; } break; } } processCount.push_back(recordinfo); } bRet = TRUE; Exit: DBG_MSG(MSG_INFO, L"-CDBOperater::ReadRecordFromDB"); return bRet; }