IME 単語登録

ImmRegisterWord
↑この関数を実行して単語を登録するまでの一連の処理です
品詞の取得
最初に品詞の一覧を取得する必要がありますが、数が決まっていない上に
API が構造体の配列を返して来ますので少し注意が必要です。
( 構造体の定義には、クラスを使う方法がありますがそちらではエラーになります )
typedef struct _tagSTYLEBUF {
  DWORD  dwStyle;           
  TCHAR  szDescription[STYLE_DESCRIPTION_SIZE];  
} STYLEBUF, *PSTYLEBUF;
	' ********************************************************
	' * 構造体
	' ********************************************************
	<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
	Public Structure STYLEBUF
		Public dwStyle As UInt32
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=STYLE_DESCRIPTION_SIZE)> _
		Public szDescription As String
	End Structure

	' ******************************************************
	' 品詞の一覧を取得
	' ******************************************************
	<DllImport("imm32", CharSet:=CharSet.Auto)> _
	Public Shared Function _
	ImmGetRegisterWordStyle( _
	ByVal hKL As IntPtr, _
	ByVal nItem As UInt32, _
	<[In](), Out()> ByVal buf As STYLEBUF()) As IntPtr
	End Function

	Private Const STYLE_DESCRIPTION_SIZE = 32
	' ******************************************************
	' 品詞の一覧を表示
	' ******************************************************
	Private Sub 品詞の取得_Click(ByVal sender As System.Object, _
	  ByVal e As System.EventArgs) Handles 品詞の取得.Click

		Me.LboxGrid1.Reset()
		Me.LboxGrid1.AddColumn("ID", "数値")
		Me.LboxGrid1.AddColumn("STYLE", "説明")

		Dim bufdummy(1) As STYLEBUF

		Dim nItem As Integer = ImmGetRegisterWordStyle(hKL, 0, bufdummy)
		Console.WriteLine(nItem)

		Dim buf(nItem) As STYLEBUF

		ImmGetRegisterWordStyle(hKL, nItem, buf)

		Dim i As Integer
		For i = 0 To nItem - 1
			Me.LboxGrid1.AddRow()
			Me.LboxGrid1.SetColumnText("ID", buf(i).dwStyle.ToString())
			Me.LboxGrid1.SetColumnText("STYLE", buf(i).szDescription)
		Next

	End Sub
登録
hKL と hWnd は初期処理で取得済みです
	' ******************************************************
	' IMM で使用するハンドルを取得
	' ******************************************************
	Private Sub Form1_Shown(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.Shown

		hWnd = Me.Handle
		If hWnd = IntPtr.Zero Then
			MessageBox.Show("hWnd = " + hWnd.ToString())
		End If
		hKL = GetKeyboardLayout(0)
		If hKL = IntPtr.Zero Then
			MessageBox.Show("hKL = " + hKL.ToString())
		End If

	End Sub
選択した品詞番号を使用して登録しています
	' ******************************************************
	' 登録
	' ******************************************************
	Private Sub 登録_Click(ByVal sender As System.Object, _
	 ByVal e As System.EventArgs) Handles 登録.Click

		' 選択されている行から品詞番号を取得
		Dim nCurRow = Me.LboxGrid1.GetCurrentRowIndex()
		Dim nValue As UInt32 = UInt32.Parse(Me.LboxGrid1.GetColumnText(nCurRow, "ID"))

		hIMC = ImmGetContext(hWnd)
		If hIMC = IntPtr.Zero Then
			MessageBox.Show("hIMC = " + hIMC.ToString())
			Exit Sub
		End If

		If ImmSetOpenStatus(hIMC, mc_TRUE) = 0 Then
			MessageBox.Show("ImmSetOpen Failed")
			Exit Sub
		End If

		Console.WriteLine(nValue)
		ImmRegisterWord(hKL, Me.TextBox1.Text, nValue, Me.TextBox2.Text)
		ImmReleaseContext(hWnd, hIMC)

	End Sub
その他の定義
	' ******************************************************
	' ウインドウハンドルからコンテキストを取得
	' ******************************************************
	<DllImport("imm32", CharSet:=CharSet.Auto)> _
	Public Shared Function _
	ImmGetContext(ByVal hWnd As IntPtr) As IntPtr
	End Function

	' ******************************************************
	' コンテキストを開放
	' ******************************************************
	<DllImport("imm32", CharSet:=CharSet.Auto)> _
	Public Shared Function _
	ImmReleaseContext( _
	ByVal hWnd As IntPtr, _
	ByVal hIMC As IntPtr) As Int32
	End Function

	' ******************************************************
	' 開閉
	' ******************************************************
	<DllImport("imm32", CharSet:=CharSet.Auto)> _
	Public Shared Function _
	ImmSetOpenStatus( _
	ByVal hIMC As IntPtr, _
	ByVal fMode As Int32) As Int32
	End Function

	' ******************************************************
	' 登録
	' ******************************************************
	<DllImport("imm32", CharSet:=CharSet.Auto)> _
	Public Shared Function _
	ImmRegisterWord( _
	ByVal hKL As IntPtr, _
	ByVal lpszReading As String, _
	ByVal dwStyle As UInt32, _
	ByVal lpszRegister As String) As Int32
	End Function

	' ******************************************************
	' 入力ロケール識別子を取得
	' ******************************************************
	<DllImport("User32", CharSet:=CharSet.Auto)> _
	Public Shared Function _
	GetKeyboardLayout(ByVal dwLayout As UInt32) As IntPtr
	End Function



	Private Const mc_TRUE As Int32 = 1
	Private Const mc_FALSE As Int32 = 0
	Public hIMC As IntPtr
	Public hKL As IntPtr
	Public hWnd As IntPtr