CSDN博客

img gwbsky

WinCE EDB (图片文件存储CeOpenStream, CeStreamWrite, CeStreamSaveChanges)

发表于2008/9/28 10:54:00  193人阅读

 

#define MAKEPROP(n,t)    ((n<<16)|CEVT_##t)

 

// -----------------------------------------------------------------------------
// property of image table
#define PROPID_IMAGE_ID    MAKEPROP(200, UI4)
#define PROPID_IMAGE_STREAM   MAKELONG(CEVT_STREAM, 0x0008)

 

// 普通文件类型存储请参照系统的例子 FileDB

 

// ----------------------------------------------------------------------------

// 创建数据库
BOOL CreateDB()
{
 // define the sort information 
 SORTORDERSPECEX dbSortInfo = {0};
 dbSortInfo.wVersion = 2;
 dbSortInfo.wNumProps = 1;
 dbSortInfo.wKeyFlags = 0;
 dbSortInfo.rgPropID[0] = PROPID_IMAGE_ID;
 dbSortInfo.rgdwFlags[0] = CEDB_SORT_DESCENDING;

 // define the contact group database information
 CEDBASEINFOEX dbinfo;
 memset(&dbinfo, 0, sizeof(dbinfo));
 dbinfo.wVersion = 2;
 dbinfo.wNumSortOrder = 1;
 dbinfo.dwFlags =  CEDB_VALIDNAME | CEDB_VALIDSORTSPEC | CEDB_VALIDDBFLAGS;
 wcscpy_s(dbinfo.szDbaseName, -1, SYSTEMIMAGE_DB_NAME);
 dbinfo.dwDbaseType = 0;
 dbinfo.dwNumRecords = 3000;
 dbinfo.rgSortSpecs[0] = dbSortInfo;

 // define the property information
 CEPROPSPEC   CEPropSpec[NUM_IMAGEDB_PROPS];
 DWORD iIndex = 0;
 DefineProp(CEPropSpec, iIndex++, PROPID_IMAGE_ID,  DB_PROP_NOTNULL, TEXT("ImageID"));
 DefineProp(CEPropSpec, iIndex++, PROPID_IMAGE_STREAM, 0, TEXT("ImageStream"));

 return CeCreateDatabaseWithProps(&m_VolGUID, &dbinfo, NUM_IMAGEDB_PROPS, CEPropSpec);
}

 

// ----------------------------------------------------------------------------
// open the database
BOOL OpenDB()
{
 CEOID oidDb = 0;
 HANDLE hSession =  CeCreateSession(&m_VolGUID);
 // open database by mig33 username
 SORTORDERSPECEX dbSortInfo = {0};    
 dbSortInfo.wVersion = 2;  
 dbSortInfo.wNumProps = 1;
 dbSortInfo.wKeyFlags = 0;  
 dbSortInfo.rgPropID[0] = PROPID_IMAGE_ID;
 dbSortInfo.rgdwFlags[0] = CEDB_SORT_DESCENDING;

 CloseHandle(m_hDb); 
 m_hDb = CeOpenDatabaseInSession(hSession, &m_VolGUID, &oidDb, SYSTEMIMAGE_DB_NAME,
  &dbSortInfo, CEDB_AUTOINCREMENT, NULL);

 return (m_hDb != INVALID_HANDLE_VALUE); 
}

 

// ----------------------------------------------------------------------------
// Get object ID by primary key
CEOID  GetOidRecord(DWORD dwImageID)
{
 if (INVALID_HANDLE_VALUE == m_hDb)
 {
  DebugString(L"the contact group database is not open!");

  return 0L;
 }

 CEPROPVAL propvals;
 ZeroMemory(&propvals, sizeof(CEPROPVAL));
 propvals.propid = PROPID_IMAGE_ID;
 propvals.val.ulVal = dwImageID;
 propvals.wFlags = CEDB_PROPNOTFOUND;

 return CeSeekDatabaseEx(m_hDb, CEDB_SEEK_VALUEFIRSTEQUAL, (DWORD)&propvals, 1, NULL);
}

 

// ----------------------------------------------------------------------------

//  二进制流字段的存储

 CEPROPVAL props[1];
 SetPropValue(props, 0, PROPID_IMAGE_ID, imageItem.m_dwImageID);

 CEOID oidRecord = CeWriteRecordProps(m_hDb, 0, 1, props);

 if (!oidRecord)
 {
  DebugString(L"the image save failure!");
  return false;
 }
 else
 {
  HANDLE hStreamObject = CeOpenStream(m_hDb, PROPID_IMAGE_STREAM, GENERIC_WRITE); // important part you should read this function very  carefulness

 

  if (CeStreamWrite(hStreamObject, imageItem.m_ImageBuffer, imageItem.m_uBufferSize, NULL))
  {
   BOOL bSaveChangs = CeStreamSaveChanges(hStreamObject);

   CloseHandle(hStreamObject);
   return true;
  }
  else
  {
   CloseHandle(hStreamObject);
   return false;
  }
 }

 

// ----------------------------------------------------------------------------
// 读取文件流
BOOL ReadItem(CImageStoreItem &imageItem)
{
 CEOID oidRecord;
 WORD wNumProps;
 DWORD dwBufSize = 0;
 CEPROPVAL *pRecord = NULL;

 oidRecord = CeReadRecordPropsEx(m_hDb, CEDB_ALLOWREALLOC, &wNumProps,
  NULL, (LPBYTE *)&pRecord, &dwBufSize, NULL);

 if (0 != oidRecord)
 {
  for (WORD i = 0; i < wNumProps; i++)
  {
   switch(pRecord[i].propid)
   {
   case PROPID_IMAGE_ID:
    ReadPropValue(pRecord[i], imageItem.m_dwImageID);
    break;
   case PROPID_IMAGE_STREAM:
    imageItem.ReadBuffer(pRecord[i].val.blob.lpb, pRecord[i].val.blob.dwCount);
    break;
   default:
    break;
   }
  }
 }
 LocalFree(pRecord);

 return (0 != oidRecord);
}

 

0 0

相关博文

我的热门文章

img
取 消
img