views:

28

answers:

1

This time i'm tring to work with registry values and so i'm using the following code

Private Function RegKeyExists(ByVal hKey As Long, ByVal sKeyPath As String) As Boolean

      Dim lResult As Long
      Dim hSubkey As Long

      lResult = RegOpenKeyEx(hKey, sKeyPath, 0, KEY_EXECUTE, hSubkey)
      ' Determine if handle is valid
      If hSubkey <> 0 Then
           RegKeyExists = True
           RegCloseKey(hKey)
      End If

 End Function

and i'm calling this function from the folloing line

RegKeyExists(HKEY_LOCAL_MACHINE, "\Software\OTA\Elefsina\Vechicle")

Of course the path is not exists and so i'm expecting tho receive FALSE from the Function, but intead of that the program is stoped inside the function and gives me an error of

"System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=vehicles
  StackTrace:
       at vehicles.vehicleformmain.RegOpenKeyEx(Int64 hKey, String& lpSubKey, Int64 ulOptions, Int64 samDesired, Int64 phkResult)
       at vehicles.vehicleformmain.RegKeyExists(Int64 hKey, String sKeyPath) in D:\Work\ADATA\New2010Projects\OTA\Elefsina\Projects\vehicles\vehicles\vehicleformmain.vb:line 95
       at vehicles.vehicleformmain.vehicleformmain_Load(Object sender, EventArgs e) in D:\Work\ADATA\New2010Projects\OTA\Elefsina\Projects\vehicles\vehicles\vehicleformmain.vb:line 106
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.set_Visible(Boolean value)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at vehicles.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
  InnerException: "

I can't understand why is that error occured and what i have to do is there anybody to assist me?

+2  A: 

There is no need to use Windows API here... the .NET framework provides means for accessing the registry:

Dim regKey = Registry.LocalMachine.OpenSubKey("Software\OTA\Elefsina\Vechicle", False)

If regKey Is Nothing Then
    ''# Key doesn't exist

Else
    ''# Key exists -- do something with it, if you want, and then close it

    regKey.Close()
End If
Heinzi
PS: "Vehicle" has only one "c". :-)
Heinzi
thank you for the correction it was made by wrong typing
Lefteris Gkinis
Your Help was Greate, now please tell me how to create a subkey and passing value to it?
Lefteris Gkinis
and if i write regkey.close then i take an error of "Object reference not set to an instance of an object." Why?
Lefteris Gkinis
Only call regkey.Close if regKey is not nothing.
Heinzi
The easiest way to create a value is to use Registry.SetValue; here is the documentation: http://msdn.microsoft.com/en-us/library/5a5t63w8.aspx.
Heinzi