2010-10-06 53 views
0

這一次我特林與註冊表值工作,所以我用下面的代碼與登記工作值的

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 

和我打電話從folloing線這個功能

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

我不明白爲什麼發生了這個錯誤,我必須做什麼 有沒有人來幫助我?

回答

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 
+1

PS: 「車輛」 只有一個 「C」。 :-) – Heinzi 2010-10-06 16:08:13

+0

感謝您通過錯誤的輸入進行修改 – 2010-10-06 19:12:57

+0

您的幫助是Greate,現在請告訴我如何創建一個子密鑰並將值傳遞給它? – 2010-10-06 19:13:50