[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1832
  • Last Modified:

How to successfully use VB 6 registry functions like GetProfileString, GetPrivateProfileString, GetPrivateProfileSectionNames.

Hi,

I am facing problems populating a listbox after making selection of section names that are populated in a combo box. Here is the code.

Option Explicit

Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" _
  (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
       
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
  (ByVal lpSectionName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, _
   ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" _
  (ByVal lpSectionName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, _
   ByVal nSize As Long) As Long


Public lstItems, IniFile, Buffer As String
Public SectionName, KeyName As String
Public defaultvalue, ProfileGetSessionNames As String
Public Result, BuffLen, y, EmptyCharAt, AvailableChars As Integer

Private Sub Form_Load()
IniFile = "C:\Test.ini"
End Sub

Private Sub btnGet_Click()
   If cmbLoadSectionItems.ListCount = 0 Then
   
      Buffer = Space$(2048)
      BuffLen = Len(Buffer)
     
      Result = GetPrivateProfileSectionNames(Buffer, BuffLen, IniFile)
      EmptyCharAt = 1
      AvailableChars = 1
      y = 1
      'MsgBox "Length of the returned string  " & Result
   
      While AvailableChars > 0 And Result <> 0
      EmptyCharAt = InStr(y, Buffer, Chr$(0))
      AvailableChars = EmptyCharAt - y
     ' MsgBox AvailableChars & " characters available to fetch"
     ' MsgBox "Empty Character at :  " & EmptyCharAt
      If (AvailableChars <> 0) Then
      cmbLoadSectionItems.AddItem (Mid(Buffer, y, AvailableChars))
      End If
      y = EmptyCharAt + 1
      Wend
    Else
       
    End If
   
  End Sub
  Private Sub cmbLoadSectionItems_Click()
   lstKeyNames.Clear
   Buffer = Space$(2048)
   BuffLen = Len(Buffer)
   SectionName = LTrim(RTrim(cmbLoadSectionItems.Text))
   
   Result = GetPrivateProfileString(SectionName, KeyName, defaultvalue, Buffer, BuffLen, IniFile)

   EmptyCharAt = 1
   AvailableChars = 1
   y = 1
   MsgBox "Length of the returned string  " & Result
 
   While AvailableChars > 0 And Result <> 0
   EmptyCharAt = InStr(y, Buffer, Chr$(0))
   
   AvailableChars = EmptyCharAt - y
'   MsgBox AvailableChars & " characters available to fetch"
'   MsgBox "Empty Character at :  " & EmptyCharAt
   If (AvailableChars <> 0) Then
   lstKeyNames.AddItem (Mid(Buffer, y, AvailableChars))
   
   End If
   y = EmptyCharAt + 1
   Wend
End Sub

 
Private Sub lstKeyNames_Click()
   txtKeyValue.Text = ""
   Buffer = Space$(2048)
   BuffLen = Len(Buffer)
   SectionName = LTrim(RTrim(cmbLoadSectionItems.Text))
   KeyName = LTrim(RTrim(lstKeyNames.Text))
'   MsgBox SectionName
'   MsgBox KeyName
'   Result = GetProfileString(SectionName, KeyName, defaultvalue, Buffer, BuffLen)
    Result = GetPrivateProfileString(SectionName, KeyName, defaultvalue, Buffer, BuffLen, IniFile)

'   MsgBox "Total chars of the key value  " & Result
 
   If Result Then
     ProfileGetSessionNames = Left$(Buffer, Result)
     
   End If
   
   txtKeyValue.Text = ProfileGetSessionNames
   MsgBox "Now emptying KeyName..."
   KeyName = ""
   
End Sub


A brief idea of what my code does is - I am populating  section name first in a combox box in the click event of a button. Then once the drop down gets selected, I click on one of the options and based on section name, I am populating Key names in a list box. Once the list box is populated, I click on one of the key names and I get the key values of that key name. When this cycle is completed, I go back to my section names drop down list and when I select any option out of it, I am unable to populate my list box with any values. when I go back into the code and step thru, I am unable to see any value returned by this function (just after finishing my cycle) - "GetPrivateProfileString". What could possibly be going wrong here??

Thanks for any help!
J
0
Jammerules
Asked:
Jammerules
  • 2
  • 2
1 Solution
 
danaseamanCommented:
Use this registry class.
It should have everything you need to get values and enumerate sections:

http://www.vbaccelerator.com/home/VB/Code/Libraries/Registry_and_Ini_Files/Complete_Registry_Control/article.asp
0
 
JammerulesAuthor Commented:
Dan,

Thanks for your reply. I wish it was so easier on me. But I am reading an "ini" file that is physically located in a local folder. I have read elsewhere that it has become obsolete now a days to use an ini file. But my project uses otherwise (duh!!!)...any other thoughts??
Jammer
0
 
danaseamanCommented:
Sorry. I gave you the wrong link.
For INI files use this class:
http://www.vbaccelerator.com/codelib/inireg/inifile.htm

There is nothing wrong with INI files however there is a new issue with Windows Vista that does not allow user to write to files in Program Files folder due to permissions. You must write to the Users\... Folder where you have permsissions.

Another alternative is using XML.


0
 
JammerulesAuthor Commented:
Hey danaseaman

That's fine. I have solved the mystery behind my problem. The value of the globally declared variable is not getting cleared (not initialized) and apparently, there was a special character (not null, not white space) that is getting stored and when that global variable is being sent into the API function, the function is using as if a value has been passed as a parameter and bringing unwanted results. What did the trick was to declare a completely new variable before calling the API and that variable is being initialized everytime. So that worked.

I am still awarding you points for the suggestions you provided. Keep up the good work!
Thanks
J
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now