|
-
Nov 24th, 2013, 11:10 AM
#1
Thread Starter
Junior Member
calls procedures
hi,
i have mixed a piece of code i found with a simple variable code i made:
Code:
Imports System.Runtime.InteropServices
Module inimod
Public Class IniFile
' API functions
<DllImport("kernel32.dll")> _
Private Shared Function GetPrivateProfileString(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, ByVal lpFileName As String) _
As Integer
End Function
<DllImport("kernel32.dll")> _
Private Shared Function WritePrivateProfileString(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Integer
End Function
Private Declare Ansi Function FlushPrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As Integer, ByVal lpKeyName As Integer, ByVal lpString As Integer, ByVal lpFileName As String) As Integer
Private _filename As String
' Constructor, accepting a filename
Public Sub New(ByVal Filename As String)
_filename = Filename
End Sub
' Read-only filename property
ReadOnly Property FileName() As String
Get
Return _filename
End Get
End Property
Public Function GetString(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As String) As String
' Returns a string from your INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = GetPrivateProfileString(Section, Key, [Default], objResult, objResult.Capacity, _filename)
If intCharCount > 0 Then
GetString = Left(objResult.ToString, intCharCount)
Else
GetString = ""
End If
End Function
Public Sub WriteString(ByVal Section As String, _
ByVal Key As String, ByVal Value As String)
' Writes a string to your INI file
WritePrivateProfileString(Section, Key, Value, _filename)
Dim chan1 As String
Dim chan2 As String
Dim chan3 As String
Dim chan4 As String
Dim chan5 As String
Dim chan6 As String
Dim chan7 As String
Dim chan8 As String
Dim chan9 As String
Dim chan10 As String
Dim r
chan1 = main.memfreq1.Text
chan2 = main.memfreq2.Text
chan3 = main.memfreq3.Text
chan4 = main.memfreq4.Text
chan5 = main.memfreq5.Text
chan6 = main.memfreq6.Text
chan7 = main.memfreq7.Text
chan8 = main.memfreq8.Text
chan9 = main.memfreq9.Text
chan10 = main.memfreq10.Text
r = WritePrivateProfileString("RadioLinks", "1", chan1, Application.ExecutablePath & "\test\test.ini")
End Sub
End Class
End Module
and i want to call this part with a button click at my main form:
Code:
Public Sub WriteString(ByVal Section As String, _
ByVal Key As String, ByVal Value As String)
' Writes a string to your INI file
WritePrivateProfileString(Section, Key, Value, _filename)
Dim chan1 As String
Dim chan2 As String
Dim chan3 As String
Dim chan4 As String
Dim chan5 As String
Dim chan6 As String
Dim chan7 As String
Dim chan8 As String
Dim chan9 As String
Dim chan10 As String
Dim r
chan1 = main.memfreq1.Text
chan2 = main.memfreq2.Text
chan3 = main.memfreq3.Text
chan4 = main.memfreq4.Text
chan5 = main.memfreq5.Text
chan6 = main.memfreq6.Text
chan7 = main.memfreq7.Text
chan8 = main.memfreq8.Text
chan9 = main.memfreq9.Text
chan10 = main.memfreq10.Text
r = WritePrivateProfileString("RadioLinks", "1", chan1, Application.ExecutablePath & "\test\test.ini")
End Sub
but the call procedure and is not working. even when i set:
Code:
Sub writeproperties()
Dim chan1 As String
Dim chan2 As String
Dim chan3 As String
Dim chan4 As String
Dim chan5 As String
Dim chan6 As String
Dim chan7 As String
Dim chan8 As String
Dim chan9 As String
Dim chan10 As String
Dim r
chan1 = main.memfreq1.Text
chan2 = main.memfreq2.Text
chan3 = main.memfreq3.Text
chan4 = main.memfreq4.Text
chan5 = main.memfreq5.Text
chan6 = main.memfreq6.Text
chan7 = main.memfreq7.Text
chan8 = main.memfreq8.Text
chan9 = main.memfreq9.Text
chan10 = main.memfreq10.Text
r = WritePrivateProfileString("RadioLinks", "1", chan1, Application.ExecutablePath & "\test\test.ini")
End Sub
.
i made some stuff on vb6 a few years and im not a regular coder, i have a book for api functions but is regarding vb6 and vb8 seems diferent...
How can i write on my ini through the click of the main form button calling the function in the module?
Thanks:
Duarte V.
-
Nov 24th, 2013, 06:09 PM
#2
Re: calls procedures
Try this class
Code:
Option Strict On
Option Explicit On
Imports System.Text
Imports System.Runtime.InteropServices
Public Class IniFile
<DllImport("kernel32.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function GetPrivateProfileSectionNames( _
ByVal lpszReturnBuffer As IntPtr, _
ByVal nSize As System.Int32, _
ByVal lpFileName As String) As System.Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function GetPrivateProfileSection(ByVal lpAppName As String, _
ByVal lpReturnedString As IntPtr, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Short
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function GetPrivateProfileString( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function WritePrivateProfileString( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Integer
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function FlushPrivateProfileString( _
ByVal lpApplicationName As Integer, _
ByVal lpKeyName As Integer, _
ByVal lpString As Integer, _
ByVal lpFileName As String) As Integer
End Function
Public Shared Function ReadKey(ByVal strSection As String, ByVal strKey As String, ByVal strDefault As String, ByVal strFile As String) As String
Dim intLength As Int32
Dim cBuffer As StringBuilder
cBuffer = New StringBuilder(1024)
intLength = GetPrivateProfileString(strSection, strKey, strDefault, cBuffer, cBuffer.Capacity, strFile)
If intLength = 0 Then
Return strDefault
Else
Return cBuffer.ToString
End If
End Function
Public Shared Sub WriteKey(ByVal strSection As String, ByVal strKey As String, ByVal strValue As String, ByVal strFile As String)
WritePrivateProfileString(strSection, strKey, strValue, strFile)
End Sub
Public Shared Sub DeleteKey(ByVal strSection As String, ByVal strKey As String, ByVal strFile As String)
WritePrivateProfileString(strSection, strKey, vbNullString, strFile)
End Sub
Public Shared Sub DeleteSection(ByVal strSection As String, ByVal strFile As String)
WritePrivateProfileString(strSection, vbNullString, vbNullString, strFile)
End Sub
Public Shared Sub Flush(ByVal strFile As String)
FlushPrivateProfileString(0, 0, 0, strFile)
End Sub
End Class
To write to the ini file
Code:
IniFile.WriteKey("RadioLinks", "1", chan1, Application.ExecutablePath & "\test\test.ini")
-
Nov 24th, 2013, 06:55 PM
#3
Thread Starter
Junior Member
Re: calls procedures
Thanks ..it worked perfectly..i can now set the radio station links to my humble app... 
Thanks again:
Duarte V.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|