 |
\r\n\r\n\r\nHi, \r\n \r\nI\'m pretty new to VB, but have been writing a program using VB 2008 Express. I have a section of code using the RegNotifyChangeKeyValue function that I could compile perfectly using VB Studio 2003, but it throws a InvalidCast Exception under VB 2005/2008. The code is: \r\n \r\n Code: \r\n Private Function WaitForChange() As Boolean\r\n Dim notifyEvent As New Threading.AutoResetEvent(False)\r\n Dim KeyHandle As IntPtr\r\n Dim KeyToMonitor As RegistryKey\r\n KeyToMonitor = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\MediaPlayer\\CurrentMetadata", False)\r\n Dim regKeyType As Type = GetType(RegistryKey)\r\n Dim field As System.Reflection.FieldInfo\r\n field = regKeyType.GetField("hkey", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)\r\n KeyHandle = CType(field.GetValue(KeyToMonitor), IntPtr)\r\n\r\n Dim err As Integer\r\n err = NativeMethods.RegNotifyChangeKeyValue(KeyHandle, _\r\n True, NotifyFilterFlags.REG_NOTIFY_CHANGE_ATTRIBUTES _\r\n Or NotifyFilterFlags.REG_NOTIFY_CHANGE_LAST_SET _\r\n Or NotifyFilterFlags.REG_NOTIFY_CHANGE_NAME _\r\n Or NotifyFilterFlags.REG_NOTIFY_CHANGE_SECURITY, _\r\n notifyEvent.Handle, True)\r\n\r\n If err = 0 Then\r\n notifyEvent.WaitOne()\r\n RaiseEvent KeyChanged(CObj(Me), New EventArgs())\r\n Return True\r\n Else\r\n Debug.WriteLine(err)\r\n Return False\r\n End If\r\n End Function\r\n This throws an Invalid Cast exception at KeyHandle = e(field.GetValue(KeyToMonitor) line. As far as I can tell it is because the variable Field is passing the string Microsoft.Win32.SafeHandles.SafeRegistryHandle rather than an integer. Would anyone be able to tell me how to call the handle of the registry key opened in a way compatible with Safe Keys? \r\n \r\nThanks. \r\n \r\n\r\n | \r\n \r\n \r\n \r\n\r\n \r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n \r\n \r\n \r\n \r\n  \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n\r\n \r\n\r\n | \r\n \r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n\r\n\r\n\r\n\r\n';
// next/previous post info
pn[1328799] = "1328799,1328799";
pn[0] = ",1328799";
// cached usernames
pu[0] = guestphrase;
pu[187830] = "Irish";
// -->

05-10-2009, 02:23 PM
|
|
Newcomer
|
|
Join Date: May 2009
Posts: 1
|
|
RegNotifyChangeKeyValue Handle Question
|
Hi,
I'm pretty new to VB, but have been writing a program using VB 2008 Express. I have a section of code using the RegNotifyChangeKeyValue function that I could compile perfectly using VB Studio 2003, but it throws a InvalidCast Exception under VB 2005/2008. The code is:
Code:
Private Function WaitForChange() As Boolean
Dim notifyEvent As New Threading.AutoResetEvent(False)
Dim KeyHandle As IntPtr
Dim KeyToMonitor As RegistryKey
KeyToMonitor = Registry.CurrentUser.OpenSubKey("Software\Microsoft\MediaPlayer\CurrentMetadata", False)
Dim regKeyType As Type = GetType(RegistryKey)
Dim field As System.Reflection.FieldInfo
field = regKeyType.GetField("hkey", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
KeyHandle = CType(field.GetValue(KeyToMonitor), IntPtr)
Dim err As Integer
err = NativeMethods.RegNotifyChangeKeyValue(KeyHandle, _
True, NotifyFilterFlags.REG_NOTIFY_CHANGE_ATTRIBUTES _
Or NotifyFilterFlags.REG_NOTIFY_CHANGE_LAST_SET _
Or NotifyFilterFlags.REG_NOTIFY_CHANGE_NAME _
Or NotifyFilterFlags.REG_NOTIFY_CHANGE_SECURITY, _
notifyEvent.Handle, True)
If err = 0 Then
notifyEvent.WaitOne()
RaiseEvent KeyChanged(CObj(Me), New EventArgs())
Return True
Else
Debug.WriteLine(err)
Return False
End If
End Function
This throws an Invalid Cast exception at KeyHandle = e(field.GetValue(KeyToMonitor) line. As far as I can tell it is because the variable Field is passing the string Microsoft.Win32.SafeHandles.SafeRegistryHandle rather than an integer. Would anyone be able to tell me how to call the handle of the registry key opened in a way compatible with Safe Keys?
Thanks.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Threaded Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|