Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 to VB.NET api call to access registry not working

Status
Not open for further replies.

lunchbox88

Programmer
Feb 17, 2004
208
0
0
US
I have an application that was recently run through the migration wizard to bring it from VB6 to VB.NET 2k3. I am having some issues with one particular thing...

The application accesses the registry using functions in ADVAPI32.dll. In VB6, everything works fine, and the API function (RegOpenKeyEx) returns "ERROR_SUCCESS." In .NET, it returns 1314 which I believe is the system error code for privilege not held.

Anyone run in to this, or something similiar before? Any ideas on how to resolve it?

Thanks
 
In vb.net you can use the Microsoft.Win32.Registry() class to access the registry, the class is very easy to use, and would give full errors.

For the privilege error, would the key that you are tring to access be locked out (another users profile, security...) some keys cannot be accessed. A vb.net windows app would use your security credentials when accessing the registry.
 
My account has local admin rights, so I don't think it's the security credentials of my account. What doesn't make sense to me is that it would work under VB6, but not VB.NET.

Which function under Microsoft.Win32.Registry() would access the key?
 
I would look into establishing a WMI connection and reading the registry from there. You would still have to pass the admin credentials even though your account has admin rights.

"Bugs are good for building character in the user
 
Under Microsoft.Win32.Registry you have the class roots (CurrentUser = HKEY_CURRENT_USER,CurrentConfig = HKEY_CURRENT_CONFIG...). Which one depends on which part of the registry you need.

Give this code a try:
Code:
Dim LocalMachine As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine

Dim ans As String = LocalMachine.OpenSubKey("Hardware").OpenSubKey("Description").OpenSubKey("System").OpenSubKey("CentralProcessor").OpenSubKey("0").GetValue("ProcessorNameString")

MsgBox(ans)
 
Or a shorter version of the above code:
Code:
Dim LocalMachine As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine

Dim ans As String = LocalMachine.OpenSubKey("HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("ProcessorNameString")

MsgBox(ans)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top