Win32 API 日本語リファレンス
ホームSystem.WindowsProgramming › GetPrivateProfileStructW

GetPrivateProfileStructW

関数
iniファイルからチェックサム付き構造体データをUnicodeで取得する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetPrivateProfileStructW(
    LPCWSTR lpszSection,
    LPCWSTR lpszKey,
    void* lpStruct,   // optional
    DWORD uSizeStruct,
    LPCWSTR szFile   // optional
);

パラメーター

名前方向説明
lpszSectionLPCWSTRin読み取り対象のセクション名を指す。
lpszKeyLPCWSTRin読み取り対象のキー名を指す。
lpStructvoid*outoptional取得した構造体データを受け取るバッファへのポインタ。
uSizeStructDWORDinlpStructが指すバッファのサイズをバイト単位で指定する。
szFileLPCWSTRinoptional対象INIファイルのパス。データはチェックサム付きで格納されている。

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetPrivateProfileStructW(
    LPCWSTR lpszSection,
    LPCWSTR lpszKey,
    void* lpStruct,   // optional
    DWORD uSizeStruct,
    LPCWSTR szFile   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool GetPrivateProfileStructW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpszSection,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string lpszKey,   // LPCWSTR
    IntPtr lpStruct,   // void* optional, out
    uint uSizeStruct,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] string szFile   // LPCWSTR optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function GetPrivateProfileStructW(
    <MarshalAs(UnmanagedType.LPWStr)> lpszSection As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> lpszKey As String,   ' LPCWSTR
    lpStruct As IntPtr,   ' void* optional, out
    uSizeStruct As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> szFile As String   ' LPCWSTR optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpszSection : LPCWSTR
' lpszKey : LPCWSTR
' lpStruct : void* optional, out
' uSizeStruct : DWORD
' szFile : LPCWSTR optional
Declare PtrSafe Function GetPrivateProfileStructW Lib "kernel32" ( _
    ByVal lpszSection As LongPtr, _
    ByVal lpszKey As LongPtr, _
    ByVal lpStruct As LongPtr, _
    ByVal uSizeStruct As Long, _
    ByVal szFile As LongPtr) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetPrivateProfileStructW = ctypes.windll.kernel32.GetPrivateProfileStructW
GetPrivateProfileStructW.restype = wintypes.BOOL
GetPrivateProfileStructW.argtypes = [
    wintypes.LPCWSTR,  # lpszSection : LPCWSTR
    wintypes.LPCWSTR,  # lpszKey : LPCWSTR
    ctypes.POINTER(None),  # lpStruct : void* optional, out
    wintypes.DWORD,  # uSizeStruct : DWORD
    wintypes.LPCWSTR,  # szFile : LPCWSTR optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetPrivateProfileStructW = Fiddle::Function.new(
  lib['GetPrivateProfileStructW'],
  [
    Fiddle::TYPE_VOIDP,  # lpszSection : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpszKey : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpStruct : void* optional, out
    -Fiddle::TYPE_INT,  # uSizeStruct : DWORD
    Fiddle::TYPE_VOIDP,  # szFile : LPCWSTR optional
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn GetPrivateProfileStructW(
        lpszSection: *const u16,  // LPCWSTR
        lpszKey: *const u16,  // LPCWSTR
        lpStruct: *mut (),  // void* optional, out
        uSizeStruct: u32,  // DWORD
        szFile: *const u16  // LPCWSTR optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode)]
public static extern bool GetPrivateProfileStructW([MarshalAs(UnmanagedType.LPWStr)] string lpszSection, [MarshalAs(UnmanagedType.LPWStr)] string lpszKey, IntPtr lpStruct, uint uSizeStruct, [MarshalAs(UnmanagedType.LPWStr)] string szFile);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetPrivateProfileStructW' -Namespace Win32 -PassThru
# $api::GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
#uselib "KERNEL32.dll"
#func global GetPrivateProfileStructW "GetPrivateProfileStructW" wptr, wptr, wptr, wptr, wptr
; GetPrivateProfileStructW lpszSection, lpszKey, lpStruct, uSizeStruct, szFile   ; 戻り値は stat
; lpszSection : LPCWSTR -> "wptr"
; lpszKey : LPCWSTR -> "wptr"
; lpStruct : void* optional, out -> "wptr"
; uSizeStruct : DWORD -> "wptr"
; szFile : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global GetPrivateProfileStructW "GetPrivateProfileStructW" wstr, wstr, sptr, int, wstr
; res = GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
; lpszSection : LPCWSTR -> "wstr"
; lpszKey : LPCWSTR -> "wstr"
; lpStruct : void* optional, out -> "sptr"
; uSizeStruct : DWORD -> "int"
; szFile : LPCWSTR optional -> "wstr"
; BOOL GetPrivateProfileStructW(LPCWSTR lpszSection, LPCWSTR lpszKey, void* lpStruct, DWORD uSizeStruct, LPCWSTR szFile)
#uselib "KERNEL32.dll"
#cfunc global GetPrivateProfileStructW "GetPrivateProfileStructW" wstr, wstr, intptr, int, wstr
; res = GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
; lpszSection : LPCWSTR -> "wstr"
; lpszKey : LPCWSTR -> "wstr"
; lpStruct : void* optional, out -> "intptr"
; uSizeStruct : DWORD -> "int"
; szFile : LPCWSTR optional -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetPrivateProfileStructW = kernel32.NewProc("GetPrivateProfileStructW")
)

// lpszSection (LPCWSTR), lpszKey (LPCWSTR), lpStruct (void* optional, out), uSizeStruct (DWORD), szFile (LPCWSTR optional)
r1, _, err := procGetPrivateProfileStructW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszSection))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszKey))),
	uintptr(lpStruct),
	uintptr(uSizeStruct),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szFile))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetPrivateProfileStructW(
  lpszSection: PWideChar;   // LPCWSTR
  lpszKey: PWideChar;   // LPCWSTR
  lpStruct: Pointer;   // void* optional, out
  uSizeStruct: DWORD;   // DWORD
  szFile: PWideChar   // LPCWSTR optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetPrivateProfileStructW';
result := DllCall("KERNEL32\GetPrivateProfileStructW"
    , "WStr", lpszSection   ; LPCWSTR
    , "WStr", lpszKey   ; LPCWSTR
    , "Ptr", lpStruct   ; void* optional, out
    , "UInt", uSizeStruct   ; DWORD
    , "WStr", szFile   ; LPCWSTR optional
    , "Int")   ; return: BOOL
●GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile) = DLL("KERNEL32.dll", "bool GetPrivateProfileStructW(char*, char*, void*, dword, char*)")
# 呼び出し: GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
# lpszSection : LPCWSTR -> "char*"
# lpszKey : LPCWSTR -> "char*"
# lpStruct : void* optional, out -> "void*"
# uSizeStruct : DWORD -> "dword"
# szFile : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "kernel32" fn GetPrivateProfileStructW(
    lpszSection: [*c]const u16, // LPCWSTR
    lpszKey: [*c]const u16, // LPCWSTR
    lpStruct: ?*anyopaque, // void* optional, out
    uSizeStruct: u32, // DWORD
    szFile: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetPrivateProfileStructW(
    lpszSection: WideCString,  # LPCWSTR
    lpszKey: WideCString,  # LPCWSTR
    lpStruct: pointer,  # void* optional, out
    uSizeStruct: uint32,  # DWORD
    szFile: WideCString  # LPCWSTR optional
): int32 {.importc: "GetPrivateProfileStructW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "kernel32");
extern(Windows)
int GetPrivateProfileStructW(
    const(wchar)* lpszSection,   // LPCWSTR
    const(wchar)* lpszKey,   // LPCWSTR
    void* lpStruct,   // void* optional, out
    uint uSizeStruct,   // DWORD
    const(wchar)* szFile   // LPCWSTR optional
);
ccall((:GetPrivateProfileStructW, "KERNEL32.dll"), stdcall, Int32,
      (Cwstring, Cwstring, Ptr{Cvoid}, UInt32, Cwstring),
      lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
# lpszSection : LPCWSTR -> Cwstring
# lpszKey : LPCWSTR -> Cwstring
# lpStruct : void* optional, out -> Ptr{Cvoid}
# uSizeStruct : DWORD -> UInt32
# szFile : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetPrivateProfileStructW(
    const uint16_t* lpszSection,
    const uint16_t* lpszKey,
    void* lpStruct,
    uint32_t uSizeStruct,
    const uint16_t* szFile);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
-- lpszSection : LPCWSTR
-- lpszKey : LPCWSTR
-- lpStruct : void* optional, out
-- uSizeStruct : DWORD
-- szFile : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetPrivateProfileStructW = lib.func('__stdcall', 'GetPrivateProfileStructW', 'int32_t', ['str16', 'str16', 'void *', 'uint32_t', 'str16']);
// GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
// lpszSection : LPCWSTR -> 'str16'
// lpszKey : LPCWSTR -> 'str16'
// lpStruct : void* optional, out -> 'void *'
// uSizeStruct : DWORD -> 'uint32_t'
// szFile : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetPrivateProfileStructW: { parameters: ["buffer", "buffer", "pointer", "u32", "buffer"], result: "i32" },
});
// lib.symbols.GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
// lpszSection : LPCWSTR -> "buffer"
// lpszKey : LPCWSTR -> "buffer"
// lpStruct : void* optional, out -> "pointer"
// uSizeStruct : DWORD -> "u32"
// szFile : LPCWSTR optional -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetPrivateProfileStructW(
    const uint16_t* lpszSection,
    const uint16_t* lpszKey,
    void* lpStruct,
    uint32_t uSizeStruct,
    const uint16_t* szFile);
C, "KERNEL32.dll");
// $ffi->GetPrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile);
// lpszSection : LPCWSTR
// lpszKey : LPCWSTR
// lpStruct : void* optional, out
// uSizeStruct : DWORD
// szFile : LPCWSTR optional
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean GetPrivateProfileStructW(
        WString lpszSection,   // LPCWSTR
        WString lpszKey,   // LPCWSTR
        Pointer lpStruct,   // void* optional, out
        int uSizeStruct,   // DWORD
        WString szFile   // LPCWSTR optional
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("kernel32")]
lib LibKERNEL32
  fun GetPrivateProfileStructW = GetPrivateProfileStructW(
    lpszSection : UInt16*,   # LPCWSTR
    lpszKey : UInt16*,   # LPCWSTR
    lpStruct : Void*,   # void* optional, out
    uSizeStruct : UInt32,   # DWORD
    szFile : UInt16*   # LPCWSTR optional
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetPrivateProfileStructWNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>, Uint32, Pointer<Utf16>);
typedef GetPrivateProfileStructWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>, int, Pointer<Utf16>);
final GetPrivateProfileStructW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetPrivateProfileStructWNative, GetPrivateProfileStructWDart>('GetPrivateProfileStructW');
// lpszSection : LPCWSTR -> Pointer<Utf16>
// lpszKey : LPCWSTR -> Pointer<Utf16>
// lpStruct : void* optional, out -> Pointer<Void>
// uSizeStruct : DWORD -> Uint32
// szFile : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetPrivateProfileStructW(
  lpszSection: PWideChar;   // LPCWSTR
  lpszKey: PWideChar;   // LPCWSTR
  lpStruct: Pointer;   // void* optional, out
  uSizeStruct: DWORD;   // DWORD
  szFile: PWideChar   // LPCWSTR optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetPrivateProfileStructW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetPrivateProfileStructW"
  c_GetPrivateProfileStructW :: CWString -> CWString -> Ptr () -> Word32 -> CWString -> IO CInt
-- lpszSection : LPCWSTR -> CWString
-- lpszKey : LPCWSTR -> CWString
-- lpStruct : void* optional, out -> Ptr ()
-- uSizeStruct : DWORD -> Word32
-- szFile : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getprivateprofilestructw =
  foreign "GetPrivateProfileStructW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr void) @-> uint32_t @-> (ptr uint16_t) @-> returning int32_t)
(* lpszSection : LPCWSTR -> (ptr uint16_t) *)
(* lpszKey : LPCWSTR -> (ptr uint16_t) *)
(* lpStruct : void* optional, out -> (ptr void) *)
(* uSizeStruct : DWORD -> uint32_t *)
(* szFile : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetPrivateProfileStructW" get-private-profile-struct-w :convention :stdcall) :int32
  (lpsz-section (:string :encoding :utf-16le))   ; LPCWSTR
  (lpsz-key (:string :encoding :utf-16le))   ; LPCWSTR
  (lp-struct :pointer)   ; void* optional, out
  (u-size-struct :uint32)   ; DWORD
  (sz-file (:string :encoding :utf-16le)))   ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetPrivateProfileStructW = Win32::API::More->new('KERNEL32',
    'BOOL GetPrivateProfileStructW(LPCWSTR lpszSection, LPCWSTR lpszKey, LPVOID lpStruct, DWORD uSizeStruct, LPCWSTR szFile)');
# my $ret = $GetPrivateProfileStructW->Call($lpszSection, $lpszKey, $lpStruct, $uSizeStruct, $szFile);
# lpszSection : LPCWSTR -> LPCWSTR
# lpszKey : LPCWSTR -> LPCWSTR
# lpStruct : void* optional, out -> LPVOID
# uSizeStruct : DWORD -> DWORD
# szFile : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い