
Imports System
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices


' ------------------------------------------------------------------
'プログラムＩＤ　　：  CLS_REGACCESS
'プログラム名　　　：　レジストリ制御関数
'作成日　　　　　　：　2004/09/15
'更新日　　　　　　：
'担当者　　　　　　：　(株)エコー・システム
'バージョン　　　　：　1.00
'使用例           ：
'                   <取得>
'                    Dim RootKey As Long
'                    Dim KeyPth As String
'                    Dim KeyNm As String
'                    Dim clsReg as New CLS_REGACCESS
'
'                    RootKey = HKEY_LOCAL_MACHINE
'                    KeyPth = "SOFTWARE\Microsoft\Windows\CurrentVersion"
'                    KeyNm = "RegisteredOwner"
'
'                    Me!Text1 = clsReg.fGetRegValue(RootKey, KeyPth, KeyNm)
'                   <保存>
'                    '文字列を保存
'                    KeyNm = "StrTest"
'                    strdata = "abc"
'                    clsReg.Y_SetRegValue RootKey, KeyPth, KeyNm, strdata
'
'                   'DWORD値を保存
'                    KeyNm = "LngTest"
'                    lngdata = 12345
'                    clsReg.Y_SetRegValue RootKey, KeyPth, KeyNm, lngdata
'
'                   'バイナリ値を保存
'                    KeyNm = "ByteTest"
'                    For i = 0 To 10
'                        bytdata(i) = i + 1
'                    Next
'                    clsReg.Y_SetRegValue RootKey, KeyPth, KeyNm, bytdata
'------------------------------------------------------------------

Public Class clsReg
    Inherits System.ComponentModel.Component

    'プロファイルのキーの値を取得する
    Private Declare Function GetPrivateProfileString Lib "kernel32" _
        Alias "GetPrivateProfileString" ( _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpApplicationName As String, _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpKeyName As String, _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpDefault As String, _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpReturnedString As StringBuilder, _
        ByVal nSize As UInt32, _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpFileName As String) As UInt32

    'プロファイルのキーの値を書き込む
    Private Declare Function WritePrivateProfileString Lib "kernel32" _
        Alias "WritePrivateProfileString" ( _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpApplicationName As String, _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpKeyName As String, _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpString As String, _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpFileName As String) As UInt32


    '***********************************************************
    '概要      : プロファイル（INIﾌｧｲﾙ）の値を取得する
    'ﾊﾟﾗﾒｰﾀ    :変数名        ,IO ,型        ,説明
    '          :pProFile      ,I  ,String    ,プロファイル名
    '          :pSection      ,I  ,String    ,セクション名
    '          :pKey          ,I  ,String    ,キー名
    '          :戻り値        ,O  ,String    ,キーの値
    '説明      :
    '***********************************************************
    Public Function fGetIniValueOLD(ByVal pProFile As String, ByVal pSection As String, ByVal pKey As String) As String
        Dim wBuf As StringBuilder = New StringBuilder(256)
        Dim wnum As UInt32
        Dim wPath As String
        Dim intCnt As Integer

        wPath = pProFile
        wnum = GetPrivateProfileString(pSection, pKey, "", wBuf, Convert.ToUInt32(wBuf.Capacity), wPath)
        If wBuf.Length = 0 Then fGetIniValueOLD = "" : Exit Function

        fGetIniValueOLD = wBuf.ToString
    End Function

    Public Function fGetIniValue(ByVal pProFile As String, ByVal pSection As String, ByVal pKey As String) As String

        Dim sr As StreamReader = New StreamReader(pProFile)
        Dim sectionName = pSection
        Dim result As String = ""

        Dim a As String
        Dim st As String()
        Dim i As Integer = 0
        Dim isSection As Boolean = False

        Try

            Do
                a = sr.ReadLine


                If a <> String.Empty Then


                    If a.Contains(pSection) Then
                        isSection = True
                    End If


                    st = a.Split(New Char() {"="c})

                    If st.Length = 2 Then
                        If st(0) = pKey And isSection = True Then
                            Return st(1)
                        End If
                    End If
                End If

            Loop Until a Is Nothing

            sr.Close()

            Return result



        Catch ex As Exception
            Return ""
        End Try

    End Function

    '***********************************************************
    '概要      : プロファイル（INIﾌｧｲﾙ）へ値を書き込む
    'ﾊﾟﾗﾒｰﾀ    :変数名        ,IO ,型        ,説明
    '          :pProFile      ,I  ,String    ,プロファイル名
    '          :pSection      ,I  ,String    ,セクション名
    '          :pKey          ,I  ,String    ,キー名
    '          :pBuf          ,I  ,String    ,キーの値
    '          :戻り値        ,O  ,Boolean   ,常にFalse
    '説明      :
    '***********************************************************
    Public Function fPutIniValue(ByVal pProFile As String, ByVal pSection As String, ByVal pKey As String, ByVal pBuf As String) As Boolean
        Dim wPath As String
        Dim wnum As UInt32

        wPath = pProFile
        wnum = WritePrivateProfileString(pSection, pKey, pBuf, wPath)

        fPutIniValue = False
    End Function



    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Public Sub New()
        MyBase.new()
    End Sub
End Class