Pls see the code.Iam opening the ContactsDataBase and getting the FirstName field text from the records and encrypting the FirstName.
If only one contact is present in the PhoneBook,its encrypting successfully.But when more than 1 contact is present in the PhoneBook only 1contact is encrypting and the emulator crashes with "user 42" Panic error.
// Read name of the default database
_LIT( KOrgContactFile,"" );
TBuf<KMaxDatabasePathAndNameLength> orgContactFile( KOrgContactFile );
CContactDatabase::GetDefaultNameL( orgContactFile );
orgContactFile.LowerCase();
if ( iContactDb )
{
if ( !iDbName.Compare( orgContactFile ) )
{
return;
}
iContactDb->CloseTables();
delete iContactDb;
iContactDb = NULL;
}
//Open the Default DataBase
TRAPD(err, iContactDb = CContactDatabase::OpenL( orgContactFile );
// Check if database already exist
if ( err != KErrNotFound )//If the DataBase already Exists then Read Contacts From the DataBase
{
iContactDb->SetDbViewContactType( KUidContactCard );
TFieldType aFieldType1( KUidContactFieldFamilyName );//FirstName
TFieldType aFieldType2( KUidContactFieldGivenName );//LastName
CContactDatabase::TSortPref sortPref1( aFieldType1 );
CContactDatabase::TSortPref sortPref2( aFieldType2 );
// Sort contacts by Family and Given Name
CArrayFixFlat<CContactDatabase::TSortPref>* aSortOrder =
new (ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(2);
CleanupStack::PushL( aSortOrder );
aSortOrder->AppendL( sortPref1 );
aSortOrder->AppendL( sortPref2 );
// The database takes ownership of the sort order array passed in
iContactDb->SortL( aSortOrder );
// The caller does not take ownership of this object.
// so do not push it onto the CleanupStack
const CContactIdArray* contacts = iContactDb->SortedItemsL();
// Go through each contact item and
const TInt nc( contacts->Count() );
for(TInt i=0; i <= nc-1; i++)
{
CContactItem* contact = NULL;
// The caller takes ownership of the returned object.
// So push it onto the CleanupStack
contact = iContactDb->OpenContactL( (*contacts)[i] );
CleanupStack::PushL( contact );
//This is used to get the count of Fields in a Record
CContactItemFieldSet& fieldSet = contact->CardFields();
//Now get the Text of Fields in a Record
HBufC* firstNameBuf = NULL;
HBufC* lastNameBuf = NULL;
// Get first name
TInt findpos( fieldSet.Find( KUidContactFieldGivenName ) );
// Check that the first name field is actually there.
CContactTextField* firstName;
// Check that the first name field is actually there.
if ( (findpos > -1) || (findpos >= fieldSet.Count()) )
{
CContactItemField& firstNameField = fieldSet[findpos];
firstName = firstNameField.TextStorage();
//Get the FirstName Text to firstNameText
TPtrC firstNameText = firstName->Text();
TInt firstNameTextLength = firstNameText.Length();
//Store the FirstNameText to unsigned char
unsigned char *encryptdata = new unsigned char;
for( TInt item=0;item<firstNameTextLength;item++)
{
encryptdata[item] = (unsigned char)firstNameText[item];
}
unsigned char *DKey = new unsigned char [16]; //to store Key
//Generate Key
KeyGen(DKey, 16);
//Passing the Key and Encrypting the FirstNameText
Encrypt(encryptdata,dwDataLength,DKey);
TBuf8<100>DataEncryption;
DataEncryption.Copy(encryptdata);
TBuf<100>DataAfterEncryption;
DataAfterEncryption.Copy(DataEncryption);
//Set the Encrypted text to FirstName
firstName->SetTextL(DataAfterEncryption);
//Allocate the FirstName Text to firstNameBuf
firstNameBuf = firstName->Text().AllocLC();
}




