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

WriteProfileSectionA

関数
win.iniの指定セクション全体をANSIで書き込む旧式関数。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

BOOL WriteProfileSectionA(
    LPCSTR lpAppName,
    LPCSTR lpString
);

パラメーター

名前方向説明
lpAppNameLPCSTRinwin.iniに書き込むセクション名(ANSI)。
lpStringLPCSTRinセクションへ書き込むキー=値ペア群(ANSI)。NUL区切りで二重NUL終端。

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

BOOL WriteProfileSectionA(
    LPCSTR lpAppName,
    LPCSTR lpString
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool WriteProfileSectionA(
    [MarshalAs(UnmanagedType.LPStr)] string lpAppName,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] string lpString   // LPCSTR
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WriteProfileSectionA(
    <MarshalAs(UnmanagedType.LPStr)> lpAppName As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> lpString As String   ' LPCSTR
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpAppName : LPCSTR
' lpString : LPCSTR
Declare PtrSafe Function WriteProfileSectionA Lib "kernel32" ( _
    ByVal lpAppName As String, _
    ByVal lpString As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

lib = Fiddle.dlopen('KERNEL32.dll')
WriteProfileSectionA = Fiddle::Function.new(
  lib['WriteProfileSectionA'],
  [
    Fiddle::TYPE_VOIDP,  # lpAppName : LPCSTR
    Fiddle::TYPE_VOIDP,  # lpString : LPCSTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn WriteProfileSectionA(
        lpAppName: *const u8,  // LPCSTR
        lpString: *const u8  // LPCSTR
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool WriteProfileSectionA([MarshalAs(UnmanagedType.LPStr)] string lpAppName, [MarshalAs(UnmanagedType.LPStr)] string lpString);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_WriteProfileSectionA' -Namespace Win32 -PassThru
# $api::WriteProfileSectionA(lpAppName, lpString)
#uselib "KERNEL32.dll"
#func global WriteProfileSectionA "WriteProfileSectionA" sptr, sptr
; WriteProfileSectionA lpAppName, lpString   ; 戻り値は stat
; lpAppName : LPCSTR -> "sptr"
; lpString : LPCSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global WriteProfileSectionA "WriteProfileSectionA" str, str
; res = WriteProfileSectionA(lpAppName, lpString)
; lpAppName : LPCSTR -> "str"
; lpString : LPCSTR -> "str"
; BOOL WriteProfileSectionA(LPCSTR lpAppName, LPCSTR lpString)
#uselib "KERNEL32.dll"
#cfunc global WriteProfileSectionA "WriteProfileSectionA" str, str
; res = WriteProfileSectionA(lpAppName, lpString)
; lpAppName : LPCSTR -> "str"
; lpString : LPCSTR -> "str"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procWriteProfileSectionA = kernel32.NewProc("WriteProfileSectionA")
)

// lpAppName (LPCSTR), lpString (LPCSTR)
r1, _, err := procWriteProfileSectionA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpAppName))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpString))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function WriteProfileSectionA(
  lpAppName: PAnsiChar;   // LPCSTR
  lpString: PAnsiChar   // LPCSTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'WriteProfileSectionA';
result := DllCall("KERNEL32\WriteProfileSectionA"
    , "AStr", lpAppName   ; LPCSTR
    , "AStr", lpString   ; LPCSTR
    , "Int")   ; return: BOOL
●WriteProfileSectionA(lpAppName, lpString) = DLL("KERNEL32.dll", "bool WriteProfileSectionA(char*, char*)")
# 呼び出し: WriteProfileSectionA(lpAppName, lpString)
# lpAppName : LPCSTR -> "char*"
# lpString : LPCSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn WriteProfileSectionA(
    lpAppName: [*c]const u8, // LPCSTR
    lpString: [*c]const u8 // LPCSTR
) callconv(std.os.windows.WINAPI) i32;
proc WriteProfileSectionA(
    lpAppName: cstring,  # LPCSTR
    lpString: cstring  # LPCSTR
): int32 {.importc: "WriteProfileSectionA", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int WriteProfileSectionA(
    const(char)* lpAppName,   // LPCSTR
    const(char)* lpString   // LPCSTR
);
ccall((:WriteProfileSectionA, "KERNEL32.dll"), stdcall, Int32,
      (Cstring, Cstring),
      lpAppName, lpString)
# lpAppName : LPCSTR -> Cstring
# lpString : LPCSTR -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WriteProfileSectionA(
    const char* lpAppName,
    const char* lpString);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.WriteProfileSectionA(lpAppName, lpString)
-- lpAppName : LPCSTR
-- lpString : LPCSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const WriteProfileSectionA = lib.func('__stdcall', 'WriteProfileSectionA', 'int32_t', ['str', 'str']);
// WriteProfileSectionA(lpAppName, lpString)
// lpAppName : LPCSTR -> 'str'
// lpString : LPCSTR -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  WriteProfileSectionA: { parameters: ["buffer", "buffer"], result: "i32" },
});
// lib.symbols.WriteProfileSectionA(lpAppName, lpString)
// lpAppName : LPCSTR -> "buffer"
// lpString : LPCSTR -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WriteProfileSectionA(
    const char* lpAppName,
    const char* lpString);
C, "KERNEL32.dll");
// $ffi->WriteProfileSectionA(lpAppName, lpString);
// lpAppName : LPCSTR
// lpString : LPCSTR
// 構造体/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.ASCII_OPTIONS);
    boolean WriteProfileSectionA(
        String lpAppName,   // LPCSTR
        String lpString   // LPCSTR
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun WriteProfileSectionA = WriteProfileSectionA(
    lpAppName : UInt8*,   # LPCSTR
    lpString : UInt8*   # LPCSTR
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WriteProfileSectionANative = Int32 Function(Pointer<Utf8>, Pointer<Utf8>);
typedef WriteProfileSectionADart = int Function(Pointer<Utf8>, Pointer<Utf8>);
final WriteProfileSectionA = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<WriteProfileSectionANative, WriteProfileSectionADart>('WriteProfileSectionA');
// lpAppName : LPCSTR -> Pointer<Utf8>
// lpString : LPCSTR -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WriteProfileSectionA(
  lpAppName: PAnsiChar;   // LPCSTR
  lpString: PAnsiChar   // LPCSTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'WriteProfileSectionA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WriteProfileSectionA"
  c_WriteProfileSectionA :: CString -> CString -> IO CInt
-- lpAppName : LPCSTR -> CString
-- lpString : LPCSTR -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let writeprofilesectiona =
  foreign "WriteProfileSectionA"
    (string @-> string @-> returning int32_t)
(* lpAppName : LPCSTR -> string *)
(* lpString : LPCSTR -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("WriteProfileSectionA" write-profile-section-a :convention :stdcall) :int32
  (lp-app-name :string)   ; LPCSTR
  (lp-string :string))   ; LPCSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WriteProfileSectionA = Win32::API::More->new('KERNEL32',
    'BOOL WriteProfileSectionA(LPCSTR lpAppName, LPCSTR lpString)');
# my $ret = $WriteProfileSectionA->Call($lpAppName, $lpString);
# lpAppName : LPCSTR -> LPCSTR
# lpString : LPCSTR -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い