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.
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 notificationHANDLE 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.
Listing 4.16 shows a notification handler that reports the type of notification. Listing 4.16 Notification handlercase 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.
|