ISpeechDataKey Code Example (Microsoft Speech Platform)

Microsoft Speech Platform SDK 11

Microsoft Speech Platform

Interface: ISpeechDataKey

Code Example

The following Visual Basic form code demonstrates the methods of the ISpeechDataKey interface. To run this code, create a form containing the following controls:

  • Three command buttons, called Command1, Command2, and Command3

Paste this code into the Declarations section of the form.

The Form_Load procedure creates a voice object and gets its object token.

The Command1 procedure creates a data key, called K1, from the voice's token. This key provides data access to the token's folder in the Speech configuration database. The procedure then uses that datakey's CreateKey method to create a subfolder called "ISpeechDataKey" within its folder; the new datakey K2, which provides access to the new subfolder, is returned by the CreateKey method. Next, the procedure uses datakey K2 to create a subfolder called "Test1" within the "ISpeechDataKey" folder. The CreateKey method returns datakey K3, which accesses the folder. The procedure then creates a default value, a string value, a long value and a binary value within the "Test1" folder. Finally, the procedure creates a subfolder called "Test2" within the "ISpeechDataKey" folder, and writes the same four values into it.

The Command2 procedure opens the "ISpeechDataKey" subfolder, and enumerates the subfolders "Test1" and "Test2" and enumerates the values with each subfolder.

The Command3 procedure enumerates and deletes the values within the "Test1" and "Test2" subfolders, deletes these folders, and finally deletes the "ISpeechDataKey" folder.



Option Explicit

Dim V As SpeechLib.SpVoice
Dim T As SpeechLib.SpObjectToken

Dim K1 As SpeechLib.ISpeechDataKey
Dim K2 As SpeechLib.ISpeechDataKey
Dim K3 As SpeechLib.ISpeechDataKey

Const SPERR_NO_MORE_ITEMS = &H80045039;

Private Sub Command1_Click()
    On Error GoTo EH

    'Create subkey voice\ISpeechDataKey
    Set K1 = T.DataKey
    Set K2 = K1.CreateKey("ISpeechDataKey")

    'Create subkey voice\ISpeechDataKey\Test1
    Set K3 = K2.CreateKey("Test1")

    Call K3.SetStringValue("", "The default value")
    Call K3.SetStringValue("Description", "A test string value")
    Call K3.SetLongValue("Long_10K", 10000)
    Call K3.SetBinaryValue("Binary_100K", 100000)

    'Create subkey voice\ISpeechDataKey\Test2
    Set K3 = K2.CreateKey("Test2")

    Call K3.SetStringValue("", "The default value")
    Call K3.SetStringValue("Description", "A test string value")
    Call K3.SetLongValue("Long_10K", 10000)
    Call K3.SetBinaryValue("Binary_100K", 100000)

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Command2_Click()
    Dim nn As Long, mm As Long
    Dim strKey As String

    Set K1 = T.DataKey
    Debug.Print "Key " & T.GetDescription

    Set K2 = K1.OpenKey("ISpeechDataKey")
    Debug.Print "   Key ISpeechDataKey"

    'Enumerate keys within "ISpeechDataKey"
    nn = 0
    On Error Resume Next
    Do
        strKey = K2.EnumKeys(nn)
        If Err.Number = SPERR_NO_MORE_ITEMS Then Exit Do
        Debug.Print "      Key """ & strKey & """"
        nn = nn + 1

        'Enumerate values within each subkey
        Set K3 = K2.OpenKey(strKey)
        Call EnumerateValues(K3)

    Loop
    Err.Clear
    On Error GoTo 0

End Sub

Private Sub Command3_Click()
    Dim strKey As String, strValue As String

    Set K1 = T.DataKey
    Set K2 = K1.OpenKey("ISpeechDataKey")

    'Enumerate and delete keys loop
    On Error Resume Next
    Do
        strKey = K2.EnumKeys(0)
        If Err.Number = SPERR_NO_MORE_ITEMS Then Exit Do

        'Enumerate and delete values loop
        Set K3 = K2.OpenKey(strKey)
        Do
            strValue = K3.EnumValues(0)
            If Err.Number = 0 Then
                Call K3.DeleteValue(strValue)
            End If
        Loop Until Err.Number = SPERR_NO_MORE_ITEMS
        Err.Clear

        Call K2.DeleteKey(strKey)
    Loop

    Call K1.DeleteKey("ISpeechDataKey")

End Sub

Private Sub Form_Load()
    On Error GoTo EH

    'T is object token for first available voice
    Set V = New SpVoice
    Set T = V.GetVoices().Item(0)

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub EnumerateValues(DK As ISpeechDataKey)
    Dim nn As Long, strValue As String
    Dim varVariant As Variant
    Dim lngLong As Long
    Dim strString As String

    nn = 0
    On Error Resume Next
    Do
        strValue = DK.EnumValues(nn)
        If Err.Number = SPERR_NO_MORE_ITEMS Then Exit Do

        If Left(strValue, 6) = "Binary" Then
            'Binary
            varVariant = DK.GetBinaryValue(strValue)
            strString = Format(varVariant)
        ElseIf Left(strValue, 4) = "Long" Then
            'Long
            lngLong = DK.GetLongValue(strValue)
            strString = Format(lngLong)
        Else
            'String
            strString = DK.GetStringValue(strValue)
        End If

        Debug.Print "         Val """ & strValue & """ = """ & strString & """"
        nn = nn + 1

    Loop
    Err.Clear

End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Const NL = vbNewLine
    Dim T As String

    T = "Desc: " & Err.Description & NL
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"
    End

End Sub