ホーム › System.WindowsProgramming › WritePrivateProfileStructW
WritePrivateProfileStructW
関数iniファイルへチェックサム付き構造体データをUnicodeで書き込む。
シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
BOOL WritePrivateProfileStructW(
LPCWSTR lpszSection,
LPCWSTR lpszKey,
void* lpStruct, // optional
DWORD uSizeStruct,
LPCWSTR szFile // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpszSection | LPCWSTR | in | 書き込み対象のセクション名を指す。 |
| lpszKey | LPCWSTR | in | 書き込み対象のキー名を指す。NULLでキー削除となる。 |
| lpStruct | void* | inoptional | 書き込む構造体データへのポインタ。NULLでキー削除を意味する。 |
| uSizeStruct | DWORD | in | lpStructが指すデータのサイズをバイト単位で指定する。 |
| szFile | LPCWSTR | inoptional | 対象INIファイルのパス。チェックサムが付加されて保存される。 |
戻り値の型: BOOL
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
BOOL WritePrivateProfileStructW(
LPCWSTR lpszSection,
LPCWSTR lpszKey,
void* lpStruct, // optional
DWORD uSizeStruct,
LPCWSTR szFile // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool WritePrivateProfileStructW(
[MarshalAs(UnmanagedType.LPWStr)] string lpszSection, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string lpszKey, // LPCWSTR
IntPtr lpStruct, // void* optional
uint uSizeStruct, // DWORD
[MarshalAs(UnmanagedType.LPWStr)] string szFile // LPCWSTR optional
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WritePrivateProfileStructW(
<MarshalAs(UnmanagedType.LPWStr)> lpszSection As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> lpszKey As String, ' LPCWSTR
lpStruct As IntPtr, ' void* optional
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
' uSizeStruct : DWORD
' szFile : LPCWSTR optional
Declare PtrSafe Function WritePrivateProfileStructW 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
WritePrivateProfileStructW = ctypes.windll.kernel32.WritePrivateProfileStructW
WritePrivateProfileStructW.restype = wintypes.BOOL
WritePrivateProfileStructW.argtypes = [
wintypes.LPCWSTR, # lpszSection : LPCWSTR
wintypes.LPCWSTR, # lpszKey : LPCWSTR
ctypes.POINTER(None), # lpStruct : void* optional
wintypes.DWORD, # uSizeStruct : DWORD
wintypes.LPCWSTR, # szFile : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
WritePrivateProfileStructW = Fiddle::Function.new(
lib['WritePrivateProfileStructW'],
[
Fiddle::TYPE_VOIDP, # lpszSection : LPCWSTR
Fiddle::TYPE_VOIDP, # lpszKey : LPCWSTR
Fiddle::TYPE_VOIDP, # lpStruct : void* optional
-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 WritePrivateProfileStructW(
lpszSection: *const u16, // LPCWSTR
lpszKey: *const u16, // LPCWSTR
lpStruct: *mut (), // void* optional
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, SetLastError = true)]
public static extern bool WritePrivateProfileStructW([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_WritePrivateProfileStructW' -Namespace Win32 -PassThru
# $api::WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)#uselib "KERNEL32.dll"
#func global WritePrivateProfileStructW "WritePrivateProfileStructW" wptr, wptr, wptr, wptr, wptr
; WritePrivateProfileStructW lpszSection, lpszKey, lpStruct, uSizeStruct, szFile ; 戻り値は stat
; lpszSection : LPCWSTR -> "wptr"
; lpszKey : LPCWSTR -> "wptr"
; lpStruct : void* optional -> "wptr"
; uSizeStruct : DWORD -> "wptr"
; szFile : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global WritePrivateProfileStructW "WritePrivateProfileStructW" wstr, wstr, sptr, int, wstr
; res = WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
; lpszSection : LPCWSTR -> "wstr"
; lpszKey : LPCWSTR -> "wstr"
; lpStruct : void* optional -> "sptr"
; uSizeStruct : DWORD -> "int"
; szFile : LPCWSTR optional -> "wstr"; BOOL WritePrivateProfileStructW(LPCWSTR lpszSection, LPCWSTR lpszKey, void* lpStruct, DWORD uSizeStruct, LPCWSTR szFile)
#uselib "KERNEL32.dll"
#cfunc global WritePrivateProfileStructW "WritePrivateProfileStructW" wstr, wstr, intptr, int, wstr
; res = WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
; lpszSection : LPCWSTR -> "wstr"
; lpszKey : LPCWSTR -> "wstr"
; lpStruct : void* optional -> "intptr"
; uSizeStruct : DWORD -> "int"
; szFile : LPCWSTR optional -> "wstr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procWritePrivateProfileStructW = kernel32.NewProc("WritePrivateProfileStructW")
)
// lpszSection (LPCWSTR), lpszKey (LPCWSTR), lpStruct (void* optional), uSizeStruct (DWORD), szFile (LPCWSTR optional)
r1, _, err := procWritePrivateProfileStructW.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 // BOOLfunction WritePrivateProfileStructW(
lpszSection: PWideChar; // LPCWSTR
lpszKey: PWideChar; // LPCWSTR
lpStruct: Pointer; // void* optional
uSizeStruct: DWORD; // DWORD
szFile: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'KERNEL32.dll' name 'WritePrivateProfileStructW';result := DllCall("KERNEL32\WritePrivateProfileStructW"
, "WStr", lpszSection ; LPCWSTR
, "WStr", lpszKey ; LPCWSTR
, "Ptr", lpStruct ; void* optional
, "UInt", uSizeStruct ; DWORD
, "WStr", szFile ; LPCWSTR optional
, "Int") ; return: BOOL●WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile) = DLL("KERNEL32.dll", "bool WritePrivateProfileStructW(char*, char*, void*, dword, char*)")
# 呼び出し: WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
# lpszSection : LPCWSTR -> "char*"
# lpszKey : LPCWSTR -> "char*"
# lpStruct : void* optional -> "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 WritePrivateProfileStructW(
lpszSection: [*c]const u16, // LPCWSTR
lpszKey: [*c]const u16, // LPCWSTR
lpStruct: ?*anyopaque, // void* optional
uSizeStruct: u32, // DWORD
szFile: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc WritePrivateProfileStructW(
lpszSection: WideCString, # LPCWSTR
lpszKey: WideCString, # LPCWSTR
lpStruct: pointer, # void* optional
uSizeStruct: uint32, # DWORD
szFile: WideCString # LPCWSTR optional
): int32 {.importc: "WritePrivateProfileStructW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "kernel32");
extern(Windows)
int WritePrivateProfileStructW(
const(wchar)* lpszSection, // LPCWSTR
const(wchar)* lpszKey, // LPCWSTR
void* lpStruct, // void* optional
uint uSizeStruct, // DWORD
const(wchar)* szFile // LPCWSTR optional
);ccall((:WritePrivateProfileStructW, "KERNEL32.dll"), stdcall, Int32,
(Cwstring, Cwstring, Ptr{Cvoid}, UInt32, Cwstring),
lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
# lpszSection : LPCWSTR -> Cwstring
# lpszKey : LPCWSTR -> Cwstring
# lpStruct : void* optional -> 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 WritePrivateProfileStructW(
const uint16_t* lpszSection,
const uint16_t* lpszKey,
void* lpStruct,
uint32_t uSizeStruct,
const uint16_t* szFile);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
-- lpszSection : LPCWSTR
-- lpszKey : LPCWSTR
-- lpStruct : void* optional
-- 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 WritePrivateProfileStructW = lib.func('__stdcall', 'WritePrivateProfileStructW', 'int32_t', ['str16', 'str16', 'void *', 'uint32_t', 'str16']);
// WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
// lpszSection : LPCWSTR -> 'str16'
// lpszKey : LPCWSTR -> 'str16'
// lpStruct : void* optional -> 'void *'
// uSizeStruct : DWORD -> 'uint32_t'
// szFile : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
WritePrivateProfileStructW: { parameters: ["buffer", "buffer", "pointer", "u32", "buffer"], result: "i32" },
});
// lib.symbols.WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile)
// lpszSection : LPCWSTR -> "buffer"
// lpszKey : LPCWSTR -> "buffer"
// lpStruct : void* optional -> "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 WritePrivateProfileStructW(
const uint16_t* lpszSection,
const uint16_t* lpszKey,
void* lpStruct,
uint32_t uSizeStruct,
const uint16_t* szFile);
C, "KERNEL32.dll");
// $ffi->WritePrivateProfileStructW(lpszSection, lpszKey, lpStruct, uSizeStruct, szFile);
// lpszSection : LPCWSTR
// lpszKey : LPCWSTR
// lpStruct : void* optional
// 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 WritePrivateProfileStructW(
WString lpszSection, // LPCWSTR
WString lpszKey, // LPCWSTR
Pointer lpStruct, // void* optional
int uSizeStruct, // DWORD
WString szFile // LPCWSTR optional
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("kernel32")]
lib LibKERNEL32
fun WritePrivateProfileStructW = WritePrivateProfileStructW(
lpszSection : UInt16*, # LPCWSTR
lpszKey : UInt16*, # LPCWSTR
lpStruct : Void*, # void* optional
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 WritePrivateProfileStructWNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>, Uint32, Pointer<Utf16>);
typedef WritePrivateProfileStructWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>, int, Pointer<Utf16>);
final WritePrivateProfileStructW = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<WritePrivateProfileStructWNative, WritePrivateProfileStructWDart>('WritePrivateProfileStructW');
// lpszSection : LPCWSTR -> Pointer<Utf16>
// lpszKey : LPCWSTR -> Pointer<Utf16>
// lpStruct : void* optional -> Pointer<Void>
// uSizeStruct : DWORD -> Uint32
// szFile : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WritePrivateProfileStructW(
lpszSection: PWideChar; // LPCWSTR
lpszKey: PWideChar; // LPCWSTR
lpStruct: Pointer; // void* optional
uSizeStruct: DWORD; // DWORD
szFile: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'KERNEL32.dll' name 'WritePrivateProfileStructW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WritePrivateProfileStructW"
c_WritePrivateProfileStructW :: CWString -> CWString -> Ptr () -> Word32 -> CWString -> IO CInt
-- lpszSection : LPCWSTR -> CWString
-- lpszKey : LPCWSTR -> CWString
-- lpStruct : void* optional -> Ptr ()
-- uSizeStruct : DWORD -> Word32
-- szFile : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let writeprivateprofilestructw =
foreign "WritePrivateProfileStructW"
((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 -> (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 ("WritePrivateProfileStructW" write-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
(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 $WritePrivateProfileStructW = Win32::API::More->new('KERNEL32',
'BOOL WritePrivateProfileStructW(LPCWSTR lpszSection, LPCWSTR lpszKey, LPVOID lpStruct, DWORD uSizeStruct, LPCWSTR szFile)');
# my $ret = $WritePrivateProfileStructW->Call($lpszSection, $lpszKey, $lpStruct, $uSizeStruct, $szFile);
# lpszSection : LPCWSTR -> LPCWSTR
# lpszKey : LPCWSTR -> LPCWSTR
# lpStruct : void* optional -> LPVOID
# uSizeStruct : DWORD -> DWORD
# szFile : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f WritePrivateProfileStructA (ANSI版) — iniファイルへチェックサム付き構造体データをANSIで書き込む。