Go Premium for a chance to win a PS4. Enter to Win

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

INI files

I want to use INI files with VB5. In running VB under Windows NT 4.0 WS so when I use the getsetting and savesetting functions, I write down my data in the registry.

How can I still write the data to INI files ?
0
marcel051897
Asked:
marcel051897
  • 3
1 Solution
 
y96andhaCommented:
Use the API functions WritePrivateProfileString, GetPrivateProfileString and GetPrivateProfileInt.
0
 
y96andhaCommented:
API declarations:

Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long

0
 
marcel051897Author Commented:
I tried it with these functions, but I get a compile error.

Can you send some sample code for these functions?
0
 
y96andhaCommented:
This is a copy of my test form. Save it as a file with extension .frm and load it into VB.

VERSION 4.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   4230
   ClientLeft      =   1140
   ClientTop       =   1515
   ClientWidth     =   6195
   Height          =   4635
   Left            =   1080
   LinkTopic       =   "Form1"
   ScaleHeight     =   4230
   ScaleWidth      =   6195
   Top             =   1170
   Width           =   6315
   Begin VB.TextBox value
      Height          =   285
      Left            =   2400
      TabIndex        =   9
      Text            =   "Text4"
      Top             =   2880
      Width           =   3495
   End
   Begin VB.CommandButton cmdwrite
      Caption         =   "Write"
      Height          =   375
      Left            =   4680
      TabIndex        =   4
      Top             =   3480
      Width           =   1215
   End
   Begin VB.CommandButton cmdread
      Caption         =   "Read"
      Height          =   375
      Left            =   2400
      TabIndex        =   3
      Top             =   3480
      Width           =   1215
   End
   Begin VB.TextBox valuename
      Height          =   285
      Left            =   2400
      TabIndex        =   2
      Text            =   "Text3"
      Top             =   2400
      Width           =   3495
   End
   Begin VB.TextBox section
      Height          =   285
      Left            =   2400
      TabIndex        =   1
      Text            =   "Text2"
      Top             =   1920
      Width           =   3495
   End
   Begin VB.TextBox filename
      Height          =   285
      Left            =   2400
      TabIndex        =   0
      Text            =   "Text1"
      Top             =   1440
      Width           =   3495
   End
   Begin VB.Label Label4
      Caption         =   "Value"
      Height          =   255
      Left            =   600
      TabIndex        =   8
      Top             =   2880
      Width           =   1335
   End
   Begin VB.Label Label3
      Caption         =   "Value name"
      Height          =   255
      Left            =   600
      TabIndex        =   7
      Top             =   2400
      Width           =   1455
   End
   Begin VB.Label Label2
      Caption         =   "Section"
      Height          =   255
      Left            =   600
      TabIndex        =   6
      Top             =   1920
      Width           =   1335
   End
   Begin VB.Label Label1
      Caption         =   "Ini file name"
      Height          =   255
      Left            =   600
      TabIndex        =   5
      Top             =   1440
      Width           =   1335
   End
End
Attribute VB_Name = "Form1"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName 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 GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long




Private Sub cmdread_Click()
    Dim str As String
    str = String(256, 0)
    GetPrivateProfileString section.Text, valuename.Text, "<value not found>", str, 255, filename.Text
    If InStr(str, Chr(0)) Then
        str = Left$(str, InStr(str, Chr(0)) - 1)
    End If
    value.Text = str
End Sub

Private Sub cmdwrite_Click()
    WritePrivateProfileString section.Text, valuename.Text, value.Text, filename.Text
End Sub

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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