iniファイル操作クラス

オープンソース形式でコードを共有するフォーラムです。お役立ちコード、あなたも投稿してみませんか?
返信する
メッセージ
作成者
ささら

iniファイル操作クラス

#1 投稿記事 by ささら »

 *.iniファイルを操作するクラス。
作成するパスなどをある程度決め撃ちしているので、汎用性はないです(^ ^;)
汎用性を持たせようと拡張している途中で挫折していますが、
このままでもある程度は使用可能なので公開させていただきます。
好き勝手に改造してやってください。

コード: 全て選択


'APIの宣言
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (lpAppName As BytePtr, lpKeyName As BytePtr, nDefault As Long, lpFileName As BytePtr) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (lpAppName As BytePtr, lpKeyName As BytePtr, lpDefault As BytePtr, pReturnedString As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
'Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (lpAppName As BytePtr, lpReturnedString As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
'Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (lpszReturnBuffer As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
Declare Function GetPrivateProfileStruct Lib "kernel32" Alias "GetPrivateProfileStructA" (lpszSection As BytePtr,lpszKey As BytePtr,lpStruct As VoidPtr,uSizeStruct As DWord,szFile As BytePtr) As Long
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (lpAppName As BytePtr, lpString As BytePtr, lpFileName As BytePtr) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (lpAppName As BytePtr, lpKeyName As BytePtr, lpString As BytePtr, lpFileName As BytePtr) As Long
Declare Function WritePrivateProfileStruct Lib "kernel32" Alias "WritePrivateProfileStructA" (lpszSection As BytePtr, lpszKey As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord, szFile As BytePtr) As Long

'クラス
Class PRIVATEPROFILE
Private
 Path[512-1] As Byte     'プロファイルのパス
 Section[64-1] As Byte   'セクション名

Public
 'プロファイルの設定
 Function SetPrivateProfile(lpFileName As BytePtr) As Long
 dim i As Long, l As Long, buf[128-1] As Byte
  i=GetModuleFileName(GetModuleHandle(0),Path,512)
  Select Case lpFileName
  Case 0
   Path[i-3]=105
   Path[i-2]=110
   Path[i-1]=105
'  Case Else
'   GetFileTitle(Path,buf,128)
'   l=lstrlen(buf)
'   Path[i-l-1]=0
'   lstrcat(Path,lpFileName)
  End Select
 End Function

 'セクションの設定
 Function SetSection(lpAppName As BytePtr) As Long
  lstrcpy(Section,lpAppName)
 End Function

 '数値の読み込み
 Function ReadInt(lpKeyName As BytePtr,nDefault As Long) As Long
  ReadInt=GetPrivateProfileInt(Section,lpKeyName,nDefault,Path)
 End Function

 '文字列の読み込み
 Function ReadString(lpKeyName As BytePtr,nDefault As BytePtr) As BytePtr
 dim buf[512-1] As Byte
  GetPrivateProfileString(Section,lpKeyName,nDefault,buf,512,Path)
  ReadString=buf
 End Function

 '構造体の読み込み
 Function ReadStruct(lpKeyName As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord) As Long
  GetPrivateProfileStruct(Section,lpKeyName,lpStruct,uSizeStruct,Path)
 End Function

 'セクションの書き込み
 Function WriteSection(lpAppName As BytePtr) As Long
  WritePrivateProfileSection(lpAppName,"",Path)
  lstrcpy(Section,lpAppName)
 End Function

 '数値の書き込み
 Function WriteInt(lpKeyName As BytePtr,lpValue As Long) As Long
 dim buf[64-1] As Byte
  lstrcpy(buf,Str$(lpValue))
  WritePrivateProfileString(Section,lpKeyName,buf,Path)
 End Function

 '文字列の書き込み
 Function WriteString(lpKeyName As BytePtr,lpString As BytePtr) As Long
  WritePrivateProfileString(Section,lpKeyName,lpString,Path)
 End Function

 '構造体の書き込み
 Function WriteStruct(lpKeyName As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord) As Long
  WritePrivateProfileStruct(Section,lpKeyName,lpStruct,uSizeStruct,Path)
 End Function
End Class
 使用サンプル:
読み込み

コード: 全て選択


Function IniLoad()
dim pPP As PRIVATEPROFILE, wp as RECT, i As Long, buf[512-1] As Byte
 pPP.SetSection("Main")
 wp.left=pPP.ReadInt("PosX",0)
 wp.top=pPP.ReadInt("PosY",0)
 lstrcpy(buf,pPP.ReadString("FilePath",""))
 SetDlgItemText(hMainWnd,eb_exe_path,buf)
End Function
書き込み

コード: 全て選択


Function IniSave()
dim pPP As PRIVATEPROFILE, wp as RECT, i As Long, buf[512-1] As Byte
 GetWindowRect(hMainWnd,wp)

 pPP.SetSection("Main")
 pPP.WriteInt("PosX",wp.left)
 pPP.WriteInt("PosY",wp.top)
 GetDlgItemText(hMainWnd,eb_exe_path,buf,512)
 pPP.WriteString("FilePath",buf)
End Function
Tomorrow
記事: 72
登録日時: 2005年6月04日(土) 10:09

Re: iniファイル操作クラス

#2 投稿記事 by Tomorrow »

気になった点が1つ...
> '文字列の読み込み
> Function ReadString(lpKeyName As BytePtr,nDefault As BytePtr) As BytePtr
> dim buf[512-1] As Byte
> GetPrivateProfileString(Section,lpKeyName,nDefault,buf,512,Path)
> ReadString=buf
> End Function
↑のところでbuf配列は、このメソッドの終了と同時に開放されてしまうので、それをReturnすると、
呼び出し元でアクセス違反が起こる場合があります。
なので、bufをメンバ変数として持つか、またはString型として返したほうがよいと思います。

> 作成するパスなどをある程度決め撃ちしているので、汎用性はないです(^ ^;)
外部から指定できるようにしたらどうでしょうか?
INIファイルのパス名を作るのは別クラスにでも担当させて...
あるいは元のクラスは汎用的に作っておいて、それを継承してパス限定の仕様にするとか。
ささら

#3 投稿記事 by ささら »

 Tomorrowさんのご指摘も含め、全体的に修正しました。
Tomorrowさん、ご指摘有難う御座いますn(_ _)n
 

コード: 全て選択

'APIの宣言
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (lpAppName As BytePtr, lpKeyName As BytePtr, nDefault As Long, lpFileName As BytePtr) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (lpAppName As BytePtr, lpKeyName As BytePtr, lpDefault As BytePtr, pReturnedString As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
'Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (lpAppName As BytePtr, lpReturnedString As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
'Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (lpszReturnBuffer As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
Declare Function GetPrivateProfileStruct Lib "kernel32" Alias "GetPrivateProfileStructA" (lpszSection As BytePtr,lpszKey As BytePtr,lpStruct As VoidPtr,uSizeStruct As DWord,szFile As BytePtr) As Long
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (lpAppName As BytePtr, lpString As BytePtr, lpFileName As BytePtr) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (lpAppName As BytePtr, lpKeyName As BytePtr, lpString As BytePtr, lpFileName As BytePtr) As Long
Declare Function WritePrivateProfileStruct Lib "kernel32" Alias "WritePrivateProfileStructA" (lpszSection As BytePtr, lpszKey As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord, szFile As BytePtr) As Long

'クラス
Class PRIVATEPROFILE
Private
 ppPath[512-1] As Byte     'プロファイルのパス
 ppSection[64-1] As Byte   'セクション名
 ppbuf[512-1] As Byte

Public
 'プロファイルの設定
 Function SetPrivateProfile(lpFileName As BytePtr) As Long
 dim i As Long, l As Long
  i=GetModuleFileName(GetModuleHandle(0),ppPath,512)
  Select Case lpFileName
  Case 0
   ppPath[i-3]=105
   ppPath[i-2]=110
   ppPath[i-1]=105
'  Case Else
'   GetFileTitle(Path,buf,128)
'   l=lstrlen(buf)
'   Path[i-l-1]=0
'   lstrcat(Path,lpFileName)
  End Select
 End Function

 'セクションの設定
 Function SetSection(lpAppName As BytePtr) As Long
  lstrcpy(ppSection,lpAppName)
 End Function

 '数値の読み込み
 Function ReadInt(lpKeyName As BytePtr,nDefault As Long) As Long
  ReadInt=GetPrivateProfileInt(ppSection,lpKeyName,nDefault,ppPath)
 End Function

 '文字列の読み込み
 Function ReadString(lpKeyName As BytePtr,nDefault As BytePtr) As BytePtr
  GetPrivateProfileString(ppSection,lpKeyName,nDefault,ppbuf,512,ppPath)
  ReadString=ppbuf
 End Function

 '構造体の読み込み
 Function ReadStruct(lpKeyName As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord) As Long
  GetPrivateProfileStruct(ppSection,lpKeyName,lpStruct,uSizeStruct,ppPath)
 End Function

 'セクションの書き込み
 Function WriteSection(lpAppName As BytePtr) As Long
  WritePrivateProfileSection(lpAppName,"",ppPath)
  lstrcpy(ppSection,lpAppName)
 End Function

 '数値の書き込み
 Function WriteInt(lpKeyName As BytePtr,lpValue As Long) As Long
  lstrcpy(ppbuf,Str$(lpValue))
  WritePrivateProfileString(ppSection,lpKeyName,ppbuf,ppPath)
 End Function

 '文字列の書き込み
 Function WriteString(lpKeyName As BytePtr,lpString As BytePtr) As Long
  WritePrivateProfileString(ppSection,lpKeyName,lpString,ppPath)
 End Function

 '構造体の書き込み
 Function WriteStruct(lpKeyName As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord) As Long
  WritePrivateProfileStruct(ppSection,lpKeyName,lpStruct,uSizeStruct,ppPath)
 End Function
End Class
 こんな感じで宜しいんでしょうか?オブジェクト指向はムズかしい…(=_=)

 あと、サンプルも間違っていました。スミマセン(- -;)
読み込み

コード: 全て選択

Function IniLoad() 
dim pPP As PRIVATEPROFILE, wp as RECT, i As Long, buf[512-1] As Byte 
 pPP.SetPrivateProfile(0)
 pPP.SetSection("Main") 
 wp.left=pPP.ReadInt("PosX",0) 
 wp.top=pPP.ReadInt("PosY",0) 
 lstrcpy(buf,pPP.ReadString("FilePath","")) 
 SetDlgItemText(hMainWnd,eb_exe_path,buf) 
End Function
書き込み

コード: 全て選択

Function IniSave() 
dim pPP As PRIVATEPROFILE, wp as RECT, i As Long, buf[512-1] As Byte 
 GetWindowRect(hMainWnd,wp) 

 pPP.SetPrivateProfile(0)
 pPP.SetSection("Main") 
 pPP.WriteInt("PosX",wp.left) 
 pPP.WriteInt("PosY",wp.top) 
 GetDlgItemText(hMainWnd,eb_exe_path,buf,512) 
 pPP.WriteString("FilePath",buf) 
End Function
外部から指定できるようにしたらどうでしょうか?
INIファイルのパス名を作るのは別クラスにでも担当させて...
あるいは元のクラスは汎用的に作っておいて、それを継承してパス限定の仕様にするとか。
 とのご助言ですが、自分的には今のままでも問題ないので、
とりあえず後回しってことで…(- -;)ゞ
ささら

しまった…

#4 投稿記事 by ささら »

 Codeの中でボールドって効かないんですね…(- -;)
返信する