プロが教えるわが家の防犯対策術!

lpKeyNameにNullを設定するとセクション内にあるすべてのキー名をバッファに格納します。とヘルプに書いてあったんですが、
そのままlpKeyNameにNullを入力すると「型が一致しません」というエラーが出ます。
特別な入力の仕方があるのでしょうか?

A 回答 (1件)

田吾作7です。


Nullの扱いですが、それはあくまでCの記述であって、CのNullとVBのNullは意味が違います。
GetPrivateProfileStringではvbNullStringを使用します。


Public Declare Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringA" ( _
    ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As String, _
    ByVal nSize As Long, _
    ByVal lpFileName As String) As Long

Public Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" _
    (ByVal lpBuffer As String, ByVal nSize As Long) As Long

'ウィンドウズディレクトリを返す
Public Function GetWinDir() As String
  Dim lpBuffer As String * 260
  Call GetWindowsDirectory(lpBuffer, Len(lpBuffer))
  GetWinDir = Left(lpBuffer, InStr(lpBuffer, Chr$(0)) - 1)
  If Right(GetWinDir, 1) <> "\" Then GetWinDir = GetWinDir & "\"
End Function

'INIファイルからセクションを列挙する
Private Sub Main()
  Dim lngRetValue   As Long       'ステータス
  Dim strIniFileName As String      '設定ファイル名
  Dim strSectionName As String      'セクション名
  Dim strKeyName   As String      'キー名
  Dim strDefault   As String      'デフォルト値
  Dim strReturned   As String * 1023  '値を返す文字列
  Dim wkVal      As Variant
  Dim i        As Long
  
  'イニシャルファイルの指定
  strIniFileName = GetWinDir & "System.ini"
  'セクションにNULLをセット
  strSectionName = vbNullString
  'キーにNULLセット
  strKeyName = vbNullString
  '見つからないときに返す文字列
  strDefault = "キーが見つからない"
  
  'キーが帰る文字列に領域をセット
  strReturned = String(Len(strReturned), vbNullChar)
  
  'キーの取得
  lngRetValue = GetPrivateProfileString _
        (ByVal strSectionName _
        , ByVal strKeyName _
        , ByVal strDefault _
        , strReturned _
        , ByVal Len(strReturned) _
        , ByVal (strIniFileName & vbNullChar))
  'キーの取得成功
  If lngRetValue >= 0 Then
    '結果の出力
    wkVal = Split(strReturned, vbNullChar)
    For i = LBound(wkVal) To UBound(wkVal)
      Debug.Print wkVal(i)
      If wkVal(i) = "" Then
        Exit For
      End If
    Next i
  'キーの取得失敗
  Else
    Debug.Print "失敗しました"
  End If
End Sub
    • good
    • 0
この回答へのお礼

ありがとうございました。
解決しました。

お礼日時:2001/10/16 12:03

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!