To obtain contacts on mobile phones, Microsoft provides the icontact interface, which can be used to obtain SIM card contacts, which can be achieved through the simreadphonebookentry function. Examples are provided in the SDK. However, you can also read contacts by reading the database.
I remember that I have seen on the Internet the efficiency of using the icontact interface to read contacts and reading databases to get contacts. It seems that reading databases can be much faster.
Mobile phone contacts are stored in Pim. on the vol volume, the database name is contacts database. I don't know when the SIM card contact will be stored in this database, so if I want to obtain all contacts, read-Only databases can be used to increase the reading speed, especially when reading SIM cards.
After opening the database, obtain the number of contacts in the database, apply for a space, and read the contacts one by one.
The following code is used:
- Int getcontactbyedb ()
- {
- Ceguid guid = {0 };
- Create_invalidguid (& guid );
- If (! Cemountdbvolex (& guid, _ T ("Pim. Vol"), null, open_existing | edb_mount_flag ))
- {
- Return-1;
- }
- Ceoid OID = 0;
- Handle hdatabase = ceopendatabaseinsession (null, & guid, & OID,
- _ T ("Contacts Database"), null,
- Cedb_autoincrement, null );
- If (hdatabase = invalid_handle_value)
- {
- Ceunmountdbvol (& guid );
- Return-1;
- }
- // Obtain the number of contacts
- By_handle_db_information bhdi = {0 };
- Bhdi. wversion = 2;
- Bhdi. guidvol = guid;
- Bhdi. oiddbase = OID;
- If (! Cegetdbinformationbyhandle (hdatabase, & bhdi ))
- {
- Return-1;
- }
- DWORD dwcontactnumber = bhdi. infdatabase. dwnumrecords;
- * Ppuinfo = new phoneuserinfo [dwcontactnumber];
- If (* ppuinfo = NULL)
- {
- Return-1;
- }
- // Start reading
- Word wnumprops = 0;
- Cepropval * precord = NULL;
- DWORD dwbufsize = 0;
- Ceoid oidrecord = 0;
- DWORD dwpos = 0;
- Ceseekdatabaseex (hdatabase, cedb_seek_beginning, 0, 0, 0 );
- While (oidrecord = cereadrecordpropsex (hdatabase, cedb_allowrealloc, & wnumprops, null, (lpbyte *) & precord, & dwbufsize, null ))
- {
- For (INT I = 0; I <wnumprops; I ++)
- {
- Switch (precord [I]. propid)
- {
- Case pimpr_first_name:
- Break;
- Case pimpr_last_name:
- Break;
- Case pimpr_mobile_telephone_number: // mobile phone number (this is also the field read by the SIM card)
- Break;
- }
- }
- Dwpos ++;
- }
- Closehandle (hdatabase );
- Ceunmountdbvol (& guid );
- Return dwpos;
- }
Some people say that it is not successful to use cegetdbinformationbyhandle to retrieve database records. During the test, it is normal and the number of records can be correctly obtained.