Thursday, January 25, 2007

Motivation behind the Srego CE ToolPack Command-Line Utilities

The command-line tools that make up the Srego CE Toolpack have fairly long history. Their development was inspired by the need to easily move files back and forth between a desktop computer and a connected Windows CE Device (Pocket PC, Window Mobile). Of course, there are existing ways to accomplish this task. The File Explorer (with the help of AciveSync) provides a nice GUI interface that fully integrates the desktop files with the device’s files all through drag-and- drop manipulation. File Explorer provides very effective ad hoc file manipulation and an easy way to explore files on the device. The File Explorer’s interface is ideal for these tasks, but it is not very good at tasks that will be repeated often, tasks that may include a complex set of actions, tasks that will be scheduled, or any task that is automating some workflow. In the desktop world, we would simply write a script to perform these tasks and leave the File Explorer to what it does best. How do you do perform these tasks on a device? This problem posed an in-house question that needed to be solved for developing and testing applications written for handheld devices. The need to frequently move data files back and forth between the desktop and device resulted in cePush and cePull. In development mode, test files and result files could be shuttled between the desktop and device very easily. At a Command Prompt, the command is entered once, then DOS Key (the up and down arrow traversal of the command history or the F7 command history menu) is used to perform the file moves over and over. For more complex tasks, a .bat file can be created to perform a set of transfers. The batch file can even employ some logic and can be scheduled. The original cePush and cePull were very simple; they had no wild card support and no options. There were later enhanced to provide equivalent functionality found in the Command Prompt's Copy command.

The in-house success with cePush and cePull commands led to the need to create even more utilities that would remove directories on the device (ceMkdir, cdRmdir), delete files on the device (ceDel), and simply see what files are on the device on the device (ceDir). Then the need for moving and copying files on the device to the device (versus from the device to the desktop) was needed (ceMove, ceCopy).

The cePush and cePull utilities were updated to support more synchronization operations (checking time and dates to see if an up-to-date file is already present). With this functionally a new utility was added to update a files timestamp (ceTouch, similar to the UNIX touch utility). The ceJump utility was added to streamline editing files on the device.
With all of these utilities available, it became obvious that the one thing missing from the Command Prompt style-environment was the concept of a current directory. The ceCD utility was added to implement this concept and all of the utilities were modified to utilize a current directory concept which often reduces the lengthy paths that need to be typed (for example \my documents was a commonly typed path for the device and always requires quotes, setting this path to be the current simplifies the command).

The last utility added to the toolpack was the ceReg utility which allows registry modifications from the desktop command-line. It is a simplified version of the Reg utility on the desktop, but it still allows the creation, query, and updating of registry entries on the device.

Tuesday, January 23, 2007

Working with .cdb files created from .mdb files

When you convert a .mdb file to a .cdb file (when you copy it to the device with ActiveSync), there are several additional tables that get created:

MSysTables
MSysFields
MSysIndexes
MSysProcs

You can use the two of these to get Metadata from the .cdb file:

MSysTables
TableName
TableId
TableFlags (0 for user, 3 for system)

MSysFields
TableId
FieldName
FieldId
Len
Type (2=INT16, 18=UINT16, 3=INT32, 19=UINT32, 64=FileTime, 1=String, 65=Blob, 11=BOOL, 5=DOUBLE)


Here are two sample functions to get the meta data from the tables:

Function GetTableListFromCDB(cdbFile As String, _
tableId() As Long, _
tableName() As String, _
tableFlags() As Long, _
tableCount As Integer) As Boolean

tableCount = 0

' Mount a Database Volume
Dim filename As String
Dim volumeId As String

volumeId = _


SregoCETPx1.MountDatabaseVolume(cdbFile, OPEN_EXISTING)

If volumeId = "" Then
MsgBox "Unable to mount database volume"
GetTableListFromCDB = False
Exit Function
End If

' Open Database in mounted Volume
Dim dbHandle As Long
dbHandle = SregoCETPx1.OpenDatabaseByName(volumeId, _
"MSysTables", 0, CEDB_AUTOINCREMENT)

If dbHandle <> 0 Then ' make sure database was opened

' read each record
While SregoCETPx1.ReadDatabaseRecord(dbHandle)


tableName(tableCount) = _

SregoCETPx1.GetCurrentRecPropValue(0)

tableId(tableCount) = _

SregoCETPx1.GetCurrentRecPropValue(1)


tableFlags(tableCount) = _

SregoCETPx1.GetCurrentRecPropValue(2)

tableCount = tableCount + 1

Wend

SregoCETPx1.CloseDatabase dbHandle

End If

SregoCETPx1.UnmountDatabaseVolume volumeId

GetTableListFromCDB = True

End Function


Function GetFieldListFromCDB(cdbFile As String, _

tableId As Long, fieldName() As String, _

fieldId() As Long, fieldLen() As Long, _

fieldType() As Long, _

fieldCount As Integer) As Boolean

fieldCount = 0

' Mount a Database Volume
Dim filename As String
Dim volumeId As String

volumeId = _

SregoCETPx1.MountDatabaseVolume(cdbFile, _

OPEN_EXISTING)

If volumeId = "" Then
MsgBox "Unable to mount database volume"
GetFieldListFromCDB = False
Exit Function
End If

Dim recTableId As Long

' Open Database in mounted Volume
Dim dbHandle As Long
Dim id As Long
dbHandle = SregoCETPx1.OpenDatabaseByName(volumeId, _
"MSysFields", 0, CEDB_AUTOINCREMENT)

If dbHandle <> 0 Then ' Success


' read each record
While SregoCETPx1.ReadDatabaseRecord(dbHandle)

recTableId = SregoCETPx1.GetCurrentRecPropValue(0)

If recTableId = tableId Then

fieldName(fieldCount) = _

SregoCETPx1.GetCurrentRecPropValue(1)

id = SregoCETPx1.GetCurrentRecPropValue(2)
fieldId(fieldCount) = ShiftRight(id, 24)

fieldLen(fieldCount) = _

SregoCETPx1.GetCurrentRecPropValue(3)
fieldType(fieldCount) = _


SregoCETPx1.GetCurrentRecPropValue(4)

fieldCount = fieldCount + 1

End If

Wend

SregoCETPx1.CloseDatabase dbHandle

End If


SregoCETPx1.UnmountDatabaseVolume volumeId

GetFieldListFromCDB = True

End Function


' Power2 from http://search.devx.com/
Function Power2(ByVal exponent As Long) As Long
Static res(0 To 31) As Long
Dim i As Long

' rule out errors
If exponent <> 31 Then Err.Raise 5

' initialize the array at the first call
If res(0) = 0 Then
res(0) = 1
For i = 1 To 30
res(i) = res(i - 1) * 2
Next
' this is a special case
res(31) = &H80000000
End If

' return the result
Power2 = res(exponent)

End Function

' ShiftRight from http://search.devx.com/
Function ShiftRight(ByVal value As Long, ByVal times As Long) As Long
' we need to create a mask of 1's corresponding to the
' digits in VALUE that will be retained in the result
Dim mask As Long, signBit As Long

' return zero if too many times
If times >= 32 Then Exit Function
' return the value if zero times
If times = 0 Then ShiftRight = value: Exit Function

' evaluate the sign bit in advance
signBit = (value < times="31" mask =" Not" value =" (value" shiftright =" (value">

To use these function, you do something like this:

List1.Clear

Dim tableName(100) As String
Dim tableId(100) As Long
Dim tableFlags(100) As Long ' 0-user, 3-system
Dim tableCount As Integer

Dim fieldId(100) As Long
Dim fieldName(100) As String
Dim fieldLen(100) As Long
Dim fieldType(100) As Long
Dim fieldCount As Integer

Dim i As Integer
Dim j As Integer

tableCount = 0

Dim cdbFile As String

cdbFile = "\my documents\gps.cdb"

If GetTableListFromCDB(cdbFile, tableId, tableName, _
tableFlags, tableCount) Then

For i = 0 To tableCount - 1
' only show user tables
If tableFlags(i) = 0 Then


List1.AddItem "Table: " + tableName(i) + _
" ID: " + Str(tableId(i))

If GetFieldListFromCDB(cdbFile, tableId(i),

fieldName, fieldId, _

fieldLen, fieldType, _

fieldCount) Then

For j = 0 To fieldCount - 1

List1.AddItem (" " + Str(fieldId(j)) + _
" - " + fieldName(j) + " Type: " + _
Str(fieldType(j)) + _
" Len: " + Str(fieldLen(j))) + _
" ID: " + Str(fieldId(j))

Next

List1.AddItem ""
End If
End If
Next
End If

The output looks like this:


This code sample gets a list of all user tables in the .cdb and display them in a listbox along with each field in that table. The key things here are the TableId and the FieldId. These number are what tie the metadata to the real records in the table.

If you are really going to read a real table, you would do something like the following. My sample .cdb file is gps.cdb and it has a table in it called note.

' Mount a Database Volume
Dim filename As String
Dim volumeId As String

volumeId = SregoCETPx1.MountDatabaseVolume(cdbFile, OPEN_EXISTING)

If volumeId = "" Then
List1.AddItem "Unable to mount Volume: " + cdbFile
Exit Sub
End If

' Open Database in mounted Volume
Dim dbHandle As Long
dbHandle = SregoCETPx1.OpenDatabaseByName(volumeId, _
"note", 0, CEDB_AUTOINCREMENT)

If dbHandle <> 0 Then ' make sure database was opened successfully

Dim recCount As Integer
recCount = 0

While SregoCETPx1.ReadDatabaseRecord(dbHandle) ' read each record

Dim pCount As Long
Dim recId As Long

recId = SregoCETPx1.GetCurrentRecId
pCount = SregoCETPx1.GetCurrentRecPropCount

List1.AddItem Str(recCount) + " - RecId: " + _
Str(recId) + " PropCnt: " + Str(pCount)


For i = 0 To pCount - 1 ' display each property in record

Dim pId As Long
Dim pVal As Variant
Dim pLen As Long
Dim pFlags As Long
Dim pType As Long

pId = SregoCETPx1.GetCurrentRecPropId(i)
pVal = SregoCETPx1.GetCurrentRecPropValue(i)
pLen = SregoCETPx1.GetCurrentRecPropLen(i)
pFlags = SregoCETPx1.GetCurrentRecPropFlags(i)
pType = SregoCETPx1.GetCurrentRecPropType(i)

Dim tempstr As String


Select Case pType

Case CEVT_I4, CEVT_I2, CEVT_UI4, CEVT_UI2, CEVT_R8

tempstr = Val(pVal)

Case CEVT_LPWSTR

tempstr = pVal

Case CEVT_BOOL

If pVal Then tempstr = "True" Else tempstr = "False"

Case CEVT_BLOB

tempstr = SregoCETPx1.GetCurrentRecPropBlobStr(i)


Case CEVT_FILETIME

tempstr = CStr(pVal)

Case Else

tempstr = "***" ' unhandled type

End Select

List1.AddItem (" " + Str(pId) + " = " + tempstr)

Next



recCount = recCount + 1

Wend

SregoCETPx1.CloseDatabase dbHandle

End If

SregoCETPx1.UnmountDatabaseVolume volumeId


The output looks like this:


The property id of each field in the record can be looked up in the FieldId list populated by the previous calls.

Thursday, January 11, 2007

ceJump

The ceJump utility is one of the more arcane tools in the Srego CE ToolPack. The tool is arcane because it is not obvious from its name what it does and there is no Command Prompt equivalent for the desktop. This posting’s goal is to help clarify what this tool is for and help users get more out of the Srego CE ToolPack Command-Line tools.

The concept behind ceJump is simple. It provides a quick way to edit a file located on the connected device from the desktop. The same tasks can be accomplished with other tools in the CE ToolPack. It may be enlightening to see what the process is using the other tools to better understand what ceJump actually does.

Let’s say we have a file on the device called addprop.txt. This file is located in the \my documents directory and is a simple text file. I need to edit the entries in this file fairly frequently to tune the application using it. Here is the process with using ceJump:

cepull "\my documents\addprop.txt" c:\temp\addprop.txt

notepad c:\temp\addprop.txt

cepush c:\temp\addprop.txt "\my documents\addprop.txt"

With ceJump, the process is much simpler:

ceJump "\my documents\addprop.txt"

If the current directory on the device (set with ceCD.exe) is \my documents, then you could simply say:

ceJump addprop.txt

The ceJump utility will automatically pull the specified file from the connected device to the system temp directory on the desktop, then launches the associated application via the operating system shell. When the user exits the launched application, the file is checked to see if it had been modified. If the file had been modified it is automatically pushed back to the connected device. The utility “Jumps” over all of the steps for moving the file back and forth between the device and the desktop. Granted, you must be editing a file that the desktop shell has an association for, but your productivity with working with these files can be greatly increased.