App info blocks are one of the more arcane aspects of the Data Manager...
fun!
Here's the code I use to update an existing app info block by creating a new
one and deleting the old one. I start with a database ID and card number, so
my code doesn't include a call to DmOpenDatabaseInfo.
DmDatabaseInfo - to get the LocalID of the old app info block
MemLocalIDToGlobal - to convert the LocalID to a handle (to be freed later)
DmNewHandle - to allocate the new app info block
MemHandleLock - to get a pointer to the new app info block
DmWrite - to fill in the new app info block
MemPtrUnlock - to unlock the chunk for the new app info block
(MemHandleUnlock would work just as well)
MemHandleToLocalID - to convert the handle for the new app info block to a
LocalID
DmSetDatabaseInfo - to assign the new app info block to the database
MemHandleFree - to free the old app info block
I do it this way, rather than resizing the existing app info block, so that
the operation is closer to being atomic.
Now let's compare it to your code:
> DmOpenDatabaseInfo - to get dbID and dbCardNum
> DmDatabaseInfo - to get LocalID of the current AppInfoBlock
> MemHandleNew - to get a memory location for new AppInfoBlock
Use DmNewHandle instead. MemHandleNew allocates in the wrong heap.
> MemHandleLock
> MemMove - to get the wanted data in
You'll need to use DmWrite since your chunk will be in the storage heap.
> MemHandleUnlock
> MemHandleToLocalID - to get it int he format usable in the next call
> DmSetDatabaseInfo - to attach the new AppInfoBlock to the DB
> if
> (
> (NULL != appInfoIDP)
> &&
> (NULL != (*appInfoIDP))
> )
> {
The first test should be unnecessary. You should pass DmDatabaseInfo the
address of a local variable (declared on the stack).
> MemLocalIDToPtr - get the pointer, wich hope we can get to the handle
> MemPtrRecoverHandle - getting the handler so I can release it
> MemHandleFree - here we go OS, the memory is back
You can replace the first two calls with MemLocalIDToGlobal, or you can
replace the last two with MemPtrFree, but it should work as is. Make sure
you grab the old app info block's LocalID before you overwrite it. (Duh)
> when I try accessing the newly set AppInfoBlock from another
> application I get total garbage in it
Yup - that's cause you used the wrong heap.
> I also sometimes manage to get into a situation where I cannot delete
> that particular DB anymore
Same reason.
I can send you sample code if this doesn't do it for you. Email me off-list.
--
Danny @ Palm
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/