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

WriteProfileStringW

関数
win.iniへ文字列をUnicodeで書き込む旧式関数。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL WriteProfileStringW(
    LPCWSTR lpAppName,   // optional
    LPCWSTR lpKeyName,   // optional
    LPCWSTR lpString   // optional
);

パラメーター

名前方向
lpAppNameLPCWSTRinoptional
lpKeyNameLPCWSTRinoptional
lpStringLPCWSTRinoptional

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL WriteProfileStringW(
    LPCWSTR lpAppName,   // optional
    LPCWSTR lpKeyName,   // optional
    LPCWSTR lpString   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool WriteProfileStringW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpAppName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpKeyName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpString   // LPCWSTR optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WriteProfileStringW(
    <MarshalAs(UnmanagedType.LPWStr)> lpAppName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpKeyName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpString As String   ' LPCWSTR optional
) As Boolean
End Function
' lpAppName : LPCWSTR optional
' lpKeyName : LPCWSTR optional
' lpString : LPCWSTR optional
Declare PtrSafe Function WriteProfileStringW Lib "kernel32" ( _
    ByVal lpAppName As LongPtr, _
    ByVal lpKeyName As LongPtr, _
    ByVal lpString 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

WriteProfileStringW = ctypes.windll.kernel32.WriteProfileStringW
WriteProfileStringW.restype = wintypes.BOOL
WriteProfileStringW.argtypes = [
    wintypes.LPCWSTR,  # lpAppName : LPCWSTR optional
    wintypes.LPCWSTR,  # lpKeyName : LPCWSTR optional
    wintypes.LPCWSTR,  # lpString : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
WriteProfileStringW = Fiddle::Function.new(
  lib['WriteProfileStringW'],
  [
    Fiddle::TYPE_VOIDP,  # lpAppName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpKeyName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpString : LPCWSTR optional
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn WriteProfileStringW(
        lpAppName: *const u16,  // LPCWSTR optional
        lpKeyName: *const u16,  // LPCWSTR optional
        lpString: *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 WriteProfileStringW([MarshalAs(UnmanagedType.LPWStr)] string lpAppName, [MarshalAs(UnmanagedType.LPWStr)] string lpKeyName, [MarshalAs(UnmanagedType.LPWStr)] string lpString);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_WriteProfileStringW' -Namespace Win32 -PassThru
# $api::WriteProfileStringW(lpAppName, lpKeyName, lpString)
#uselib "KERNEL32.dll"
#func global WriteProfileStringW "WriteProfileStringW" wptr, wptr, wptr
; WriteProfileStringW lpAppName, lpKeyName, lpString   ; 戻り値は stat
; lpAppName : LPCWSTR optional -> "wptr"
; lpKeyName : LPCWSTR optional -> "wptr"
; lpString : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global WriteProfileStringW "WriteProfileStringW" wstr, wstr, wstr
; res = WriteProfileStringW(lpAppName, lpKeyName, lpString)
; lpAppName : LPCWSTR optional -> "wstr"
; lpKeyName : LPCWSTR optional -> "wstr"
; lpString : LPCWSTR optional -> "wstr"
; BOOL WriteProfileStringW(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpString)
#uselib "KERNEL32.dll"
#cfunc global WriteProfileStringW "WriteProfileStringW" wstr, wstr, wstr
; res = WriteProfileStringW(lpAppName, lpKeyName, lpString)
; lpAppName : LPCWSTR optional -> "wstr"
; lpKeyName : LPCWSTR optional -> "wstr"
; lpString : LPCWSTR optional -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procWriteProfileStringW = kernel32.NewProc("WriteProfileStringW")
)

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