Enumerating Databases

Enumerating databases is the process of sequentially accessing each database in a group. The group can either include all databases in the object store or only those of a specified type. Enumeration can be used when a change needs to be made to all databases of a certain type, or when synchronizing data between the desktop computer and the Windows CE-based device.

To enumerate databases, call the CeFindFirstDatabase and CeFindNextDatabase functions.

CeFindFirstDatabase establishes and returns a handle to the enumeration context for the type identifier specified. If the type identifier was zero, the context will include all the databases. Note that CeFindFirstDatabase does not give the object identifier for the first database. Use the handle to the enumeration context to call CeFindNextDatabase repeatedly to obtain the object identifiers for each database in turn. When there are no more databases of that type, CeFindNextDatabase returns the value zero. To ensure that there was no problem with the enumeration, call GetLastError and check for the ERROR_NO_MORE_ITEMS value.

When the application is finished enumerating databases, it must close the handle to the enumeration context by using the CloseHandle function.

The following code example enumerates all databases in the object store and adds their names to a combo box.

HANDLE hEnumDB;         // handle to a database enumerator
TCHAR szBuf[MAX_BUF];   // tmp string for combobox or message box
HWND hCB1;              // combo box; value set by calling GetDlgItem 
...
hEnumDB = CeFindFirstDatabase(0);
if (INVALID_HANDLE_VALUE == hEnumDB) 
{
                        // error handling omitted...uses GetLastError()
    return;             // continue only if FindFirst succeeds
}
while( (WceObjID = CeFindNextDatabase(hEnumDB)) != 0) 
{
   if (!CeOidGetInfo(WceObjID, &WceObjInfo) ) 
   {
      CloseHandle(hEnumDB);
                        // error handling omitted...uses GetLastError()
      return;           // continue only if FindNext succeeds
   }
   else 
   {
      wsprintf(szBuf, WceObjInfo.infDatabase.szDbaseName);
      SendMessage(hCB1, CB_ADDSTRING, 0, LPARAM(szBuf));
   }
}
CloseHandle(hEnumDB);