使用注册表值

时间:2010-10-06 15:52:56

标签: windows vb.net registry access-violation

这次我要使用注册表值,所以我使用以下代码

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

我正在从下面的行中调用这个函数

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

当然路径不存在,所以我期待从函数接收FALSE,但是程序在函数内停止并且给我一个错误

"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: "

我无法理解为什么会发生错误以及我必须做什么 有人帮我吗?

1 个答案:

答案 0 :(得分:2)

此处无需使用Windows API ... .NET框架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