Windows 使用注册表值
这次我尝试使用注册表值,因此我使用以下代码Windows 使用注册表值,windows,vb.net,registry,access-violation,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
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
当然,路径不存在,所以我希望从函数中收到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: "
我不明白为什么会发生这样的错误,我该怎么办
有人帮我吗?这里不需要使用Windows API。。。NET框架:
PS:“车辆”只有一个“c”。-)您的帮助很好,现在请告诉我如何创建子项并向其传递值?如果我编写regkey.close,则会出现“对象引用未设置为对象的实例”的错误。为什么?仅调用regkey.close,如果regkey不是空值。创建值的最简单方法是使用Registry.SetValue;以下是文档:。
"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: "
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