Getting Windows Mobile Device ID

by Ken Tucker 26. March 2009 21:10

I got email today asking me how to get the device ID from a pocket pc with vb

 

Imports System.Text

Public Class Form1

    <System.Runtime.InteropServices.DllImport("coredll.dll")> _
Private Shared Function GetDeviceUniqueID(ByVal appdata As Byte(), ByVal cbApplictionData As Integer, ByVal dwDeviceIDVersion As Integer, ByVal deviceIDOuput As Byte(), ByRef pcbDeviceIDOutput As Integer) As Integer
    End Function

    Private Function GetDeviceId(ByVal appData As String) As Byte()

        Dim appDataBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(appData)
        Dim outputSize As Integer = 20
        Dim output(19) As Byte

        Dim result As Integer = GetDeviceUniqueID(appDataBytes, appDataBytes.Length, 1, output, outputSize)

        Return output

    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sbId As New StringBuilder

        Dim bID() As Byte = GetDeviceId("MyAppName")

        For Each b In bID
            sbId.Append(String.Format("{0:x2}", b))
        Next

        Debug.WriteLine(sbId.ToString)
    End Sub
End Class

 

References

 

http://www.peterfoot.net/GetDeviceUniqueIDForVB.aspx

http://msdn.microsoft.com/en-us/library/ms893522.aspx

Getting Windows Mobile Device ID

by Ken Tucker 26. March 2009 21:10

I got email today asking me how to get the device ID from a pocket pc with vb

 

Imports System.Text

Public Class Form1

    <System.Runtime.InteropServices.DllImport("coredll.dll")> _
Private Shared Function GetDeviceUniqueID(ByVal appdata As Byte(), ByVal cbApplictionData As Integer, ByVal dwDeviceIDVersion As Integer, ByVal deviceIDOuput As Byte(), ByRef pcbDeviceIDOutput As Integer) As Integer
    End Function

    Private Function GetDeviceId(ByVal appData As String) As Byte()

        Dim appDataBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(appData)
        Dim outputSize As Integer = 20
        Dim output(19) As Byte

        Dim result As Integer = GetDeviceUniqueID(appDataBytes, appDataBytes.Length, 1, output, outputSize)

        Return output

    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sbId As New StringBuilder

        Dim bID() As Byte = GetDeviceId("MyAppName")

        For Each b In bID
            sbId.Append(String.Format("{0:x2}", b))
        Next

        Debug.WriteLine(sbId.ToString)
    End Sub
End Class

 

References

 

http://www.peterfoot.net/GetDeviceUniqueIDForVB.aspx

http://msdn.microsoft.com/en-us/library/ms893522.aspx

Compact Framework DataGrid Alternating Color

by Ken Tucker 19. October 2007 17:01

One of the things I miss from the Windows Forms DataGrid is the ability to have the rows alternating in color.  For this example I created a custom DataGridTextBox column which will display each other row in an different color. Basically I override the paint method to call the existing paint method with a different back color brush for every other column.

 

Public Class AltColorColumn
    Inherits DataGridTextBoxColumn

    Private m_AltColor As Color
    Public Property AltColor() As Color
        Get
            Return m_AltColor
        End Get
        Set(ByVal value As Color)
            m_AltColor = value
        End Set
    End Property

    Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
        If rowNum Mod 2 = 0 Then
            MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
        Else
            Dim bb As New SolidBrush(m_AltColor)
            MyBase.Paint(g, bounds, source, rowNum, bb, foreBrush, alignToRight)
            bb.Dispose()
        End If
    End Sub

    Public Sub New()
        m_AltColor = Color.LightSkyBlue
    End Sub
End Class

 

To test the column style I added the Northwind Sql Compact edition database and let it create a typed dataset for the products table for me.  While compiling the project I got a few errors in the designer generated code for the typed dataset. The compact framework does not support the dispose method for the memory stream.  You can comment out the few lines of code safely

 

Finally
    If (Not (s1) Is Nothing) Then
        's1.Dispose
    End If
    If (Not (s2) Is Nothing) Then
        's2.Dispose
    End If
End Try

 

Here is the code I used to generate a datagrid with alternating color columns.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bs As New BindingSource
        Dim dt As New NorthwindDataSet.ProductsDataTable
        Dim ta As New NorthwindDataSetTableAdapters.ProductsTableAdapter

        ta.Fill(dt)

        bs.DataSource = dt

        Dim ts As New DataGridTableStyle
        ts.MappingName = dt.TableName

        Dim colName As New AltColorColumn
        With colName
            .Width = 150
            .HeaderText = "Product Name"
            .MappingName = "Product Name"
        End With

        Dim colPrice As New AltColorColumn

        With colPrice
            .Width = 75
            .HeaderText = "Price"
            .MappingName = "Unit Price"
            .Format = "c"
        End With

        ts.GridColumnStyles.Add(colName)
        ts.GridColumnStyles.Add(colPrice)
        DataGrid1.TableStyles.Add(ts)
        DataGrid1.DataSource = bs
    End Sub
End Class

Formatting the Output in a Compact Framework DataGrid

by Ken Tucker 19. October 2007 08:22

Recently I got a new windows mobile 6 Pocket PC phone.  The datagrid provided in the Compact Framework is a very limited version of the Windows Forms DataGrid.  Here is an example which will format the output of a datagrid.  The windows forms datagrid has a GetColumnAtRow which you can override to return the formatted data.  In the compact framework version you need to override the paint method to draw the formatted data in the datagrid.  Note use the columns PropertyDescriptor's GetValue method to get the cells value.  For this example I created a new column which converts true false to yes no.

 

Imports System.Data

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGrid1.DataSource = CreateTables()
        Dim ts As New DataGridTableStyle
        ts.MappingName = "Persons"
        Dim textCol As New DataGridTextBoxColumn
        textCol.MappingName = "Name"
        textCol.HeaderText = "Name"
        textCol.Width = 100

        ts.GridColumnStyles.Add(textCol)
        Dim boolCol As New YesNoColumn
        boolCol.MappingName = "USA"
        boolCol.HeaderText = "USA"
        boolCol.Width = 30

        ts.GridColumnStyles.Add(boolCol)
        DirectCast(DataGrid1.DataSource, _
                 DataTable).DefaultView.AllowNew = False
        DataGrid1.TableStyles.Add(ts)
    End Sub

    Private Function CreateTables() As DataTable
        Dim dtVBReg As New DataTable("Persons")
        dtVBReg.Columns.Add("Name")
        dtVBReg.Columns.Add("USA", GetType(System.Boolean))
        dtVBReg.LoadDataRow(New Object() {"Ken Tucker", True}, True)
        dtVBReg.LoadDataRow(New Object() {"Cor Ligthert", False}, True)
        Return dtVBReg
    End Function
End Class

Public Class YesNoColumn
    Inherits DataGridTextBoxColumn

    Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
        'clear the cell
        g.FillRectangle(backBrush, bounds)

        Dim s As String
        ' Get the cells value
        s = Me.PropertyDescriptor.GetValue(source.List(rowNum)).ToString

        If s.ToLower = "true" Then s = "Yes" Else s = "No"

        Dim r As Rectangle = bounds
        r.Inflate(0, -1)
        g.DrawString(s, New Font("Arial", 8, FontStyle.Regular), foreBrush, r)
    End Sub
End Class