Me he decidido a escribir este post a razón de la escasa documentación respecto al manejo del registro en visual studio 2008. Existen funciones y clases creadas para visual basic 6 pero no funciona para la ultima version de visual.net.
Este ejemplo está enfocado a listar las aplicaciones instaladas en el equipo.
Módulo REG32:
Código:
Más información acerca de las funciones de manejo del registro de windows en:Option Strict Off Option Explicit On Module REG32 'Códigos de error y sus valores: help.netop.com/support/errorcodes/win32_error_codes.htm 'ADVAPI32 Registry API Bas File. ' This file was not writen by me but I like to thank who did write it ' -------------------------------------------------------------------- ' ADVAPI32 ' -------------------------------------------------------------------- ' function prototypes, constants, and type definitions ' for Windows 32-bit Registry API Public Const HKEY_CLASSES_ROOT As Integer = &H80000000 Public Const HKEY_CURRENT_USER As Integer = &H80000001 Public Const HKEY_LOCAL_MACHINE As Integer = &H80000002 Public Const HKEY_USERS As Integer = &H80000003 Public Const HKEY_PERFORMANCE_DATA As Integer = &H80000004 Public Const ERROR_SUCCESS As Short = 0 Public Const ERROR_NONE As Short = 0 Public sKeys As Collection Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal lpName As String, _ ByRef lpcbName As Integer, ByVal lpReserved As Integer, ByVal lpClass As String, ByRef lpcbClass As Integer, ByRef lpftLastWriteTime As Integer) As Integer Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Integer, ByVal lpSubKey As String, ByRef phkResult As Integer) As Integer Public Sub GetKeyNames(ByVal hKey As Integer, ByVal strPath As String) Dim Cnt, TKey As Integer 'Cnt es un contador para acceder a la subclaves, empieza como 0 y se incrementa hasta que se llegue al final Dim StrBuff, StrKey As String RegOpenKey(hKey, strPath, TKey) Do StrBuff = New String(vbNullChar, 255) 'A pointer to a buffer that receives the name of the subkey, including the terminating null character 'RegEnumKeyEx devuelve el valor ERROR_SUCCESS que equivale a 0 cuando tiene exito. Si falla devuelve un codigo de error If RegEnumKeyEx(TKey, Cnt, StrBuff, 255, 0, vbNullString, 0, 0) <> 0 Then Exit Do Cnt = Cnt + 1 StrKey = Left(StrBuff, InStr(StrBuff, vbNullChar) - 1) sKeys.Add(StrKey) Loop End Sub End Module
msdn.microsoft.com/en-us/library/ms724875%28VS.85%29.aspx
Formulario principal. Se carga con el evento LOAD en un listview:
Código:
Private Sub FormNewApp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load sKeys = New Collection LoadList() End Sub Private Sub LoadList() Dim StrDisName, StrPub, StrVersion, StrInstLoc As String Dim Icnt As Short Dim IntString As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" With ListView1 .Columns.Add("Software", 200, HorizontalAlignment.Center) .Columns.Add("Version", 100, HorizontalAlignment.Center) .Columns.Add("Location", 200, HorizontalAlignment.Center) .Columns.Add("Publisher", 200, HorizontalAlignment.Center) .View = View.List .GridLines = True .BorderStyle = BorderStyle.Fixed3D End With REG32.GetKeyNames(HKEY_LOCAL_MACHINE, IntString) For Icnt = 1 To sKeys.Count() - 1 If (sKeys.Item(Icnt).ToString.StartsWith("KB") = False) Then StrDisName = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\" _ + IntString + sKeys.Item(Icnt), "DisplayName", vbNullChar) 'lectura del nombre StrVersion = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\" _ + IntString + sKeys.Item(Icnt), "DisplayVersion", vbNullChar) 'lectura de la version StrInstLoc = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\" _ + IntString + sKeys.Item(Icnt), "InstallLocation", vbNullChar) 'lectura del directorio de instalacion StrPub = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\" _ + IntString + sKeys.Item(Icnt), "Publisher", vbNullChar) 'lectura del desarrollador If (StrPub <> vbNullChar) Then 'filtrado de entradas que no tengan desarrollador y actualizaciones 'Las actualizaciones contienen "KB" en DisplayName If (StrDisName <> vbNullChar) And (StrDisName.Contains("KB") = False) Then Dim item As New ListViewItem(StrDisName) If (StrVersion = vbNullChar) Then StrVersion = "------" End If If (StrInstLoc = vbNullChar) Then StrInstLoc = "------" End If If (StrPub = vbNullChar) Then StrPub = "------" End If item.SubItems.Add(StrVersion) item.SubItems.Add(StrInstLoc) item.SubItems.Add(StrPub) ListView1.Items.Add(item) End If End If End If Next ListView1.Sorting = SortOrder.Ascending sKeys = Nothing StrDisName = "" End Sub