Problem with regi api in vb.net

devilmaster1

Member
Joined
Jul 29, 2007
Messages
16
Programming Experience
5-10
Hey i been tying to use the registry api calls in vb.net and when i try i always get this error message.

object reference not set to an instance of an object vb.net

the code i was using is this.

Public Function MakeDWORD(FolderKey As String, SubFolderKey As String, KeyValue As String, KeyText As String)
Dim nBufferKey As Long, nBufferSubKey As Long
Dim InputValue As String
InputValue = KeyText
RegCreateKey HKEYCURRENTUSER, FolderKey, nBufferKey
RegOpenKey HKEYCURRENTUSER, FolderKey, nBufferKey
RegCreateKey nBufferKey, SubFolderKey, nBufferSubKey
RegOpenKey nBufferKey, SubFolderKey, nBufferSubKey
RegSetValueEx nBufferSubKey, KeyValue, 0, REG_DWORD, InputValue, 4
End Function

it errors out on the regcreatekey part.
RegCreateKey HKEYCURRENTUSER, FolderKey, nBufferKey

and thats the error i get any idea how i can fix that cause its kinda frustating i use vb.net 2003 and i know how to access the registery though the name spaces but its limited to only the reg_sz entrie i need to use dword and binary but i cant using namespaces but this function here is erroring out any idea how i can fix the problem ?
 
I think you might want to declare your variables like this:

VB.NET:
Dim nBufferKey As New Long
Dim nBufferSubKey As New Long

I hope this will help you.

Jah Bless!!!:D
 
i give that a try and see what happens and your right about not using api to access the registry the only problem is the Microsoft.Win32.Registry and .RegistryKey classes does not allow you to use the dword or binary i have only been able to write reg_sz entries to the regisry using the win32.registry name spaces which is why i am trying to use the api so that i can write dword and binary values but if you know of any way to get the name space to write dword or binary values i would like to know and i tell you if that worked.
 
Hey i tryed that new as long and i get this error

An unhandled exception of type 'System.NullReferenceException' occurred in RegistryBSTest.exe
Additional information: Object reference not set to an instance of an object.

and its on this code

RegCreateKey(HKEY_CURRENT_USER, FolderKey, nBufferKey)

now here is my api declares.

Imports System.Runtime.InteropServices

Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByVal lpType As Long, ByVal lpData As String, ByVal lpcbData As Long) As Long
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal phkResult As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Const REG_BINARY = 3
Private Const REG_DWORD = 4
Private Const REG_SZ = 1
Public Const HKEY_CURRENT_USER = &H80000001

and the function is this.


Public Function MakeDWord(ByVal FolderKey As String, ByVal SubFolderKey As String, ByVal KeyValue As String, ByVal KeyText As String)
Dim nBufferKey As New Long, nBufferSubKey As New Long
Dim InputValue As String
InputValue = KeyText
RegCreateKey(HKEY_CURRENT_USER, FolderKey, nBufferKey)
RegOpenKey(HKEY_CURRENT_USER, FolderKey, nBufferKey)
RegCreateKey(nBufferKey, SubFolderKey, nBufferSubKey)
RegOpenKey(nBufferKey, SubFolderKey, nBufferSubKey)
RegSetValueEx(nBufferSubKey, KeyValue, 0, REG_DWORD, InputValue, Len(InputValue))
End Function

yet still it errors out not sure why any suggestions ?
 
Windows API functions almost invariably expect 32-bit numbers. In VB6 the Integer type was 16-bit and the Long type was 32-bit. In VB.NET the Integer type is 32-bit and the Long type is 64-bit.
 
yes true and i have tryed changing the longs to integers and still the code does not error but it does not write any data to the registry neither.
i left the longs in the function but changed all as long codes to integer in the declare functions when i did that the code did not error yet it did not write.
so any other ideas?
 
Back
Top