Hello seniors,

I am beginner in Palm OS programming and have a basic
background of C++. I faced problem while creating a
very simple application that performs
storing/retrieving data to/from Palm. The application
should be able to accept data for two fields (Name &
Note). Then retrieve it back on screen. The
application has successfully add a new record.
However, error occurs when retrieving the record at
UnPackRecord method. I have been trying to solve this
problem for 3 days. However, I couldn't manage it. I
really appreciate your help.

Here are the codes:

typedef struct
{
        Char* Name;
        Char* Note;
} testRecItem;

typedef struct
{
        char    NameNote;
}testRec;

typedef testRecItem*    testRecItemPtr;
typedef testRec*                testRecPtr;

//*************************************************************************
static void ShowRecord(UInt16 index)
{
        MemHandle               recordH;
        testRecPtr              recordP;
        testRecItemPtr  record;
                
        recordH = DmQueryRecord(testDB, index);
        if (recordH != NULL)
        {
                recordP = MemHandleLock(recordH);
                UnPackRecord(record, recordP);
                MemHandleUnlock(recordH);
                
                SetText(GetObjectPtr(MainNameField), record->Name);
                SetText(GetObjectPtr(MainNoteField), record->Note);
        }
}

static void UnPackRecord( testRecItemPtr record,
testRecPtr src )
{
        Char    *p;
        
        p = &src->NameNote;
        //The error occurs just right here
        record->Name = p; 
        p += StrLen(p) + 1;
        record->Note = p; 
}

static void PackRecord( testRecItemPtr record, MemPtr
recordP )
{
        Char                    zero=0;
        testRecPtr              nilP=0;
        MemPtr                  p;
        UInt16                  length;
        UInt32                  offset;
        
        offset = (UInt32)&nilP->NameNote;
        if ( record->Name )
        {
                p = record->Name;
                length = StrLen(p) + 1 ;
                DmWrite( recordP, offset, p, length);
                offset += length;
        }
        else
        {
                DmWrite( recordP, offset, &zero, 1 );
                offset++;
        }
        
        if ( record->Note ) 
        {
                p = record->Note;
                length = StrLen(p) + 1 ;
                DmWrite( recordP, offset, p, length);
        }
        else
                DmWrite( recordP, offset, &zero, 1 );
}

static UInt16 GetPackedSize(testRecItemPtr record)
{
        UInt16  size;
        
        size = sizeof(testRec);
        if (record->Name)
                size += StrLen(record->Name) + 1;
        if (record->Note)
                size += StrLen(record->Note) + 1;
        
        return size;
}

static void AddNewRecord(void)
{
        MemHandle               recordH;
        testRecItemPtr  newRecord;
        testRecPtr              recordP;

        UInt16 index = DmNumRecords(testDB);
        
        newRecord->Name =
FldGetTextPtr(GetObjectPtr(MainNameField));
        newRecord->Note =
FldGetTextPtr(GetObjectPtr(MainNoteField));
        
        recordH = DmNewHandle(testDB,
GetPackedSize(newRecord));
        recordP = MemHandleLock(recordH);
        
        PackRecord(newRecord, recordP);
        MemHandleUnlock(recordH);

        DmAttachRecord(testDB, &index, recordH, 0);

        FrmCustomAlert(MsgAlert, "New record has been added",
NULL, NULL);
}

http://my.yahoo.com.au - My Yahoo!
- It's My Yahoo! Get your own!

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to