Thursday 14 May 2015

FILE I/O using "kernel32.dll"

File I/O using "kernel32.dll"

"Kernel32.dll" is one of the default library used by Windows OS. The main function of this is for faster I/O access  for configuration file using text file. Through this features, there is no need to read data inside text file one by one.

Let me show you how it works.










Inside the text file the format should like this:
















[logs] - Section
logs - Key String
1- Value during writing and returned value during reading

You can use any name for Section,logs,and Value.

How to use "kernel32.dll" in VB.Net?

Import "kernel32.dll",

Imports Microsoft.Office.Interop
Imports System.Text

Public Class Form1
 <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer

    End Function

 <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean

    End Function

End Class

Create public function for writing(blue font color),

Public Class Form1
 <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer

    End Function

 <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean

    End Function

 Public Shared Sub WriteINI(ByVal File As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
        WritePrivateProfileString(Section, Key, Value, File)

 End Sub

End Class


Create public function for reading(blue font color),

Public Class Form1
 <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer

    End Function

 <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean

    End Function

 Public Shared Sub WriteINI(ByVal File As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
        WritePrivateProfileString(Section, Key, Value, File)

 End Sub

 Public Shared Function ReadINI(ByVal File As String, ByVal Section As String, ByVal Key As String) As String
        Dim sb As New StringBuilder(10000)
        GetPrivateProfileString(Section, Key, "", sb, sb.Capacity, File)
        Return sb.ToString

 End Function

End Class


How to Call the Functions?

Drag a button to Form and label it "READ". Then the code is like this,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DATA As String
        DATA = ReadINI("C:\KERNEL\try.txt", "logs", "logs")
        MsgBox(DATA)

End Sub

DATA = ReadINI("C:\KERNEL\try.txt", "logs", "logs")

DATA - variable that hold returned value
ReadINI-function name
C:\KERNEL\try.txt"-file
logs-Section
logs-Key String


Drag another button to Form and label it "WRITE". Then the code is like this,

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
         WriteINI("C:\KERNEL\try.txt", "logs", "logs","1")

End Sub

WriteINI("C:\KERNEL\try.txt", "logs", "logs","1")

WriteINI-Function name
C:\KERNEL\try.txt"-file
logs-Section
logs-Key String
1-Value to write


I hope it helps a lot in your project. This is my first post in my blog.


You can email me at paulneuda@gmail.com for questions.