Database Notifications

< BACK  NEXT >
[oR]

When opening a database you can elect to receive notifications whenever the database or mounted database volume is changed. The last argument in CeOpenDatabaseEx is NULL if no notification is required, or a pointer to a CENOTIFYREQUEST structure specifying information on how notifications are to be received. Notifications are normally received as a WM_DBNOTIFICATION message sent to a specific application window, with lParam pointing to a CENOTIFICATION structure. The CENOTIFIYREQUEST members are shown in Table 4.16.

Table 4.16. CENOTIFYREQUEST structure members
Member Purpose
DWORD dwSize Size of the CENOTIFYREQUEST structure.
HWND hWnd Window handle to receive WM_DBNOTIFICATION message.
DWORD dwFlags Use CEDB_EXNOTIFICATION, 0 uses old-style notifications.
HANDLE hHeap Heap from which to allocate CENOTIFICATION structures. Use NULL for default heap.
DWORD dwParam Programmer-supplied value passed into CENOTIFICATION structure.

Listing 4.15 shows the setting up of notification on the Company database. In this case, the value "999" has been selected as the value to be passed to the CENOTIFICATION routine. You can choose any value you like, or you can ignore it. If you are setting up notifications on different databases and using the same window to receive routines, you might choose different dwParam values so you can determine which database the notifications originated from.

Listing 4.15 Sets up a notification
 HANDLE g_hDBNotification = INVALID_HANDLE_VALUE; CENOTIFYREQUEST g_cNotifyRequest; void Listing4_15() {   CEOID ceOidDB = 0;   CEGUID ceObjStore;   g_cNotifyRequest.dwSize = sizeof(CENOTIFYREQUEST);   g_cNotifyRequest.hwnd = hWnd;   g_cNotifyRequest.dwFlags = CEDB_EXNOTIFICATION;   g_cNotifyRequest.hHeap = NULL; // use default heap   // value passed to notification   g_cNotifyRequest.dwParam = 999;   CREATE_SYSTEMGUID(&ceObjStore);   g_hDBNotification  = CeOpenDatabaseEx(&ceObjStore,       &ceOidDB,       _T("Company"),       0, 0, &g_cNotifyRequest);   if(g_hDBNotification != INVALID_HANDLE_VALUE)      cout   _T("Notification set!")   endl;   else      cout  _T("Could not open database")   endl; } 

The database needs to be kept open while the application needs to receive notifications. Therefore, g_hDBNotification is a global variable. The structure g_cNotifyRequest is required in the message handle so that the CENOTIFICATION structure can be deleted. It too must be a global variable.

The next stage is to write a message handler for WM_DBNOTIFICATION. The lParam for this message is a pointer to a CENOTIFICATION structure containing information on the type of notification. The members of the CENOTIFICATION structure are listed in Table 4.17.

Table 4.17. CENOTIFICATION structure members
Member Purpose
DWORD dwSize Size of the structure in bytes.
DWORD dwParam dwParam value specified in CENOTIFYREQUEST. "999" in the above example.
UINT uType Type of notification:
DB_CEOID_CREATED
DB_CEOID_DATABASE_DELETED
DB_CEOID_RECORD_DELETED
DB_CEOID_CHANGED
CEGUID guid CEGUID of the mounted database volume.
CEOID oid Object Identifier of the object (e.g. record or database) that generated the notification.
CEOID oidParent Parent Object Identifier of the object generating the notification. If the object is a record, the parent is the database.

Listing 4.16 shows a notification handler that reports the type of notification.

Listing 4.16 Notification handler
 case WM_DBNOTIFICATION:   {   CENOTIFICATION* cNote = (CENOTIFICATION*)lParam;   cout   endl   _T("Database Notification: ");   cout   (DWORD) cNote->>dwParam   endl;   switch(cNote->>uType)   {     case DB_CEOID_CREATED:       cout   _T("New OID object was created.")              endl;       break;     case DB_CEOID_DATABASE_DELETED:       cout   _T("Database was deleted.")              endl;       break;     case DB_CEOID_RECORD_DELETED:       cout   _T("Record was deleted.")   endl;       break;     case DB_CEOID_CHANGED:       cout   _T("Object was modified.")   endl;       break;   }   CeFreeNotification(&g_cNotifyRequest, cNote);   break;   } 

The function CeFreeNotification must be called each time a WM_DBNOTIFICATION message is received. You must pass a pointer to the original CENOTIFYREQUEST structure (which was passed to CeOpenDatabaseEx) and the pointer to the CENOTIFICATION structure.

Notifications will stop when the database is closed by calling CloseHandle.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net