ab.com コミュニティ

ActiveBasicを通したコミュニケーション
現在時刻 - 2024年3月28日(木) 19:05

全ての表示時間は UTC+09:00 です




新しいトピックを投稿する  トピックへ返信する  [ 4 件の記事 ] 
作成者 メッセージ
投稿記事Posted: 2005年6月05日(日) 00:21 
 *.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


通報する
ページトップ
   
投稿記事Posted: 2005年6月05日(日) 10:44 
オフライン

登録日時: 2005年6月04日(土) 10:09
記事: 72
気になった点が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ファイルのパス名を作るのは別クラスにでも担当させて...
あるいは元のクラスは汎用的に作っておいて、それを継承してパス限定の仕様にするとか。


通報する
ページトップ
 記事の件名:
投稿記事Posted: 2005年6月05日(日) 18:29 
 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ファイルのパス名を作るのは別クラスにでも担当させて...
あるいは元のクラスは汎用的に作っておいて、それを継承してパス限定の仕様にするとか。
 とのご助言ですが、自分的には今のままでも問題ないので、
とりあえず後回しってことで…(- -;)ゞ


通報する
ページトップ
   
 記事の件名: しまった…
投稿記事Posted: 2005年6月05日(日) 18:31 
 Codeの中でボールドって効かないんですね…(- -;)


通報する
ページトップ
   
期間内表示:  ソート  
新しいトピックを投稿する  トピックへ返信する  [ 4 件の記事 ] 

全ての表示時間は UTC+09:00 です


オンラインデータ

このフォーラムを閲覧中のユーザー: Google [Bot] & ゲスト[7人]


トピック投稿:  可
返信投稿:  可
記事編集: 不可
記事削除: 不可
ファイル添付: 不可

検索:
ページ移動:  
Powered by phpBB® Forum Software © phpBB Limited
Japanese translation principally by ocean