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

WindowsPreallocateStringBuffer

関数
可変HSTRING用に書き込み可能なバッファを事前確保する。
DLLapi-ms-win-core-winrt-string-l1-1-0.dll呼出規約winapi対応OSwindows8.0

シグネチャ

// api-ms-win-core-winrt-string-l1-1-0.dll
#include <windows.h>

HRESULT WindowsPreallocateStringBuffer(
    DWORD length,
    WORD** charBuffer,
    HSTRING_BUFFER* bufferHandle
);

パラメーター

名前方向説明
lengthDWORDin事前確保する可変長文字列バッファの文字数。
charBufferWORD**out出力として書き込み可能なUTF-16バッファ先頭を受け取るポインタのアドレス。
bufferHandleHSTRING_BUFFER*out出力としてプロモート時に使うHSTRING_BUFFERハンドルを受け取るポインタ。

戻り値の型: HRESULT

各言語での呼び出し定義

// api-ms-win-core-winrt-string-l1-1-0.dll
#include <windows.h>

HRESULT WindowsPreallocateStringBuffer(
    DWORD length,
    WORD** charBuffer,
    HSTRING_BUFFER* bufferHandle
);
[DllImport("api-ms-win-core-winrt-string-l1-1-0.dll", ExactSpelling = true)]
static extern int WindowsPreallocateStringBuffer(
    uint length,   // DWORD
    IntPtr charBuffer,   // WORD** out
    IntPtr bufferHandle   // HSTRING_BUFFER* out
);
<DllImport("api-ms-win-core-winrt-string-l1-1-0.dll", ExactSpelling:=True)>
Public Shared Function WindowsPreallocateStringBuffer(
    length As UInteger,   ' DWORD
    charBuffer As IntPtr,   ' WORD** out
    bufferHandle As IntPtr   ' HSTRING_BUFFER* out
) As Integer
End Function
' length : DWORD
' charBuffer : WORD** out
' bufferHandle : HSTRING_BUFFER* out
Declare PtrSafe Function WindowsPreallocateStringBuffer Lib "api-ms-win-core-winrt-string-l1-1-0" ( _
    ByVal length As Long, _
    ByVal charBuffer As LongPtr, _
    ByVal bufferHandle As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WindowsPreallocateStringBuffer = ctypes.windll.LoadLibrary("api-ms-win-core-winrt-string-l1-1-0.dll").WindowsPreallocateStringBuffer
WindowsPreallocateStringBuffer.restype = ctypes.c_int
WindowsPreallocateStringBuffer.argtypes = [
    wintypes.DWORD,  # length : DWORD
    ctypes.c_void_p,  # charBuffer : WORD** out
    ctypes.c_void_p,  # bufferHandle : HSTRING_BUFFER* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('api-ms-win-core-winrt-string-l1-1-0.dll')
WindowsPreallocateStringBuffer = Fiddle::Function.new(
  lib['WindowsPreallocateStringBuffer'],
  [
    -Fiddle::TYPE_INT,  # length : DWORD
    Fiddle::TYPE_VOIDP,  # charBuffer : WORD** out
    Fiddle::TYPE_VOIDP,  # bufferHandle : HSTRING_BUFFER* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "api-ms-win-core-winrt-string-l1-1-0")]
extern "system" {
    fn WindowsPreallocateStringBuffer(
        length: u32,  // DWORD
        charBuffer: *mut *mut u16,  // WORD** out
        bufferHandle: *mut *mut core::ffi::c_void  // HSTRING_BUFFER* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("api-ms-win-core-winrt-string-l1-1-0.dll")]
public static extern int WindowsPreallocateStringBuffer(uint length, IntPtr charBuffer, IntPtr bufferHandle);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-winrt-string-l1-1-0_WindowsPreallocateStringBuffer' -Namespace Win32 -PassThru
# $api::WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle)
#uselib "api-ms-win-core-winrt-string-l1-1-0.dll"
#func global WindowsPreallocateStringBuffer "WindowsPreallocateStringBuffer" sptr, sptr, sptr
; WindowsPreallocateStringBuffer length, varptr(charBuffer), bufferHandle   ; 戻り値は stat
; length : DWORD -> "sptr"
; charBuffer : WORD** out -> "sptr"
; bufferHandle : HSTRING_BUFFER* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "api-ms-win-core-winrt-string-l1-1-0.dll"
#cfunc global WindowsPreallocateStringBuffer "WindowsPreallocateStringBuffer" int, var, sptr
; res = WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle)
; length : DWORD -> "int"
; charBuffer : WORD** out -> "var"
; bufferHandle : HSTRING_BUFFER* out -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT WindowsPreallocateStringBuffer(DWORD length, WORD** charBuffer, HSTRING_BUFFER* bufferHandle)
#uselib "api-ms-win-core-winrt-string-l1-1-0.dll"
#cfunc global WindowsPreallocateStringBuffer "WindowsPreallocateStringBuffer" int, var, intptr
; res = WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle)
; length : DWORD -> "int"
; charBuffer : WORD** out -> "var"
; bufferHandle : HSTRING_BUFFER* out -> "intptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	api_ms_win_core_winrt_string_l1_1_0 = windows.NewLazySystemDLL("api-ms-win-core-winrt-string-l1-1-0.dll")
	procWindowsPreallocateStringBuffer = api_ms_win_core_winrt_string_l1_1_0.NewProc("WindowsPreallocateStringBuffer")
)

// length (DWORD), charBuffer (WORD** out), bufferHandle (HSTRING_BUFFER* out)
r1, _, err := procWindowsPreallocateStringBuffer.Call(
	uintptr(length),
	uintptr(charBuffer),
	uintptr(bufferHandle),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function WindowsPreallocateStringBuffer(
  length: DWORD;   // DWORD
  charBuffer: Pointer;   // WORD** out
  bufferHandle: Pointer   // HSTRING_BUFFER* out
): Integer; stdcall;
  external 'api-ms-win-core-winrt-string-l1-1-0.dll' name 'WindowsPreallocateStringBuffer';
result := DllCall("api-ms-win-core-winrt-string-l1-1-0\WindowsPreallocateStringBuffer"
    , "UInt", length   ; DWORD
    , "Ptr", charBuffer   ; WORD** out
    , "Ptr", bufferHandle   ; HSTRING_BUFFER* out
    , "Int")   ; return: HRESULT
●WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle) = DLL("api-ms-win-core-winrt-string-l1-1-0.dll", "int WindowsPreallocateStringBuffer(dword, void*, void*)")
# 呼び出し: WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle)
# length : DWORD -> "dword"
# charBuffer : WORD** out -> "void*"
# bufferHandle : HSTRING_BUFFER* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "api-ms-win-core-winrt-string-l1-1-0" fn WindowsPreallocateStringBuffer(
    length: u32, // DWORD
    charBuffer: [*c][*c]u16, // WORD** out
    bufferHandle: ?*anyopaque // HSTRING_BUFFER* out
) callconv(std.os.windows.WINAPI) i32;
proc WindowsPreallocateStringBuffer(
    length: uint32,  # DWORD
    charBuffer: ptr uint16,  # WORD** out
    bufferHandle: pointer  # HSTRING_BUFFER* out
): int32 {.importc: "WindowsPreallocateStringBuffer", stdcall, dynlib: "api-ms-win-core-winrt-string-l1-1-0.dll".}
pragma(lib, "api-ms-win-core-winrt-string-l1-1-0");
extern(Windows)
int WindowsPreallocateStringBuffer(
    uint length,   // DWORD
    ushort** charBuffer,   // WORD** out
    void* bufferHandle   // HSTRING_BUFFER* out
);
ccall((:WindowsPreallocateStringBuffer, "api-ms-win-core-winrt-string-l1-1-0.dll"), stdcall, Int32,
      (UInt32, Ptr{UInt16}, Ptr{Cvoid}),
      length, charBuffer, bufferHandle)
# length : DWORD -> UInt32
# charBuffer : WORD** out -> Ptr{UInt16}
# bufferHandle : HSTRING_BUFFER* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WindowsPreallocateStringBuffer(
    uint32_t length,
    uint16_t** charBuffer,
    void* bufferHandle);
]]
local api-ms-win-core-winrt-string-l1-1-0 = ffi.load("api-ms-win-core-winrt-string-l1-1-0")
-- api-ms-win-core-winrt-string-l1-1-0.WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle)
-- length : DWORD
-- charBuffer : WORD** out
-- bufferHandle : HSTRING_BUFFER* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('api-ms-win-core-winrt-string-l1-1-0.dll');
const WindowsPreallocateStringBuffer = lib.func('__stdcall', 'WindowsPreallocateStringBuffer', 'int32_t', ['uint32_t', 'uint16_t *', 'void *']);
// WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle)
// length : DWORD -> 'uint32_t'
// charBuffer : WORD** out -> 'uint16_t *'
// bufferHandle : HSTRING_BUFFER* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("api-ms-win-core-winrt-string-l1-1-0.dll", {
  WindowsPreallocateStringBuffer: { parameters: ["u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle)
// length : DWORD -> "u32"
// charBuffer : WORD** out -> "pointer"
// bufferHandle : HSTRING_BUFFER* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WindowsPreallocateStringBuffer(
    uint32_t length,
    uint16_t** charBuffer,
    void* bufferHandle);
C, "api-ms-win-core-winrt-string-l1-1-0.dll");
// $ffi->WindowsPreallocateStringBuffer(length, charBuffer, bufferHandle);
// length : DWORD
// charBuffer : WORD** out
// bufferHandle : HSTRING_BUFFER* out
// 構造体/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 Api-ms-win-core-winrt-string-l1-1-0 extends StdCallLibrary {
    Api-ms-win-core-winrt-string-l1-1-0 INSTANCE = Native.load("api-ms-win-core-winrt-string-l1-1-0", Api-ms-win-core-winrt-string-l1-1-0.class);
    int WindowsPreallocateStringBuffer(
        int length,   // DWORD
        Pointer charBuffer,   // WORD** out
        Pointer bufferHandle   // HSTRING_BUFFER* out
    );
}
@[Link("api-ms-win-core-winrt-string-l1-1-0")]
lib Libapi-ms-win-core-winrt-string-l1-1-0
  fun WindowsPreallocateStringBuffer = WindowsPreallocateStringBuffer(
    length : UInt32,   # DWORD
    charBuffer : UInt16**,   # WORD** out
    bufferHandle : Void*   # HSTRING_BUFFER* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WindowsPreallocateStringBufferNative = Int32 Function(Uint32, Pointer<Uint16>, Pointer<Void>);
typedef WindowsPreallocateStringBufferDart = int Function(int, Pointer<Uint16>, Pointer<Void>);
final WindowsPreallocateStringBuffer = DynamicLibrary.open('api-ms-win-core-winrt-string-l1-1-0.dll')
    .lookupFunction<WindowsPreallocateStringBufferNative, WindowsPreallocateStringBufferDart>('WindowsPreallocateStringBuffer');
// length : DWORD -> Uint32
// charBuffer : WORD** out -> Pointer<Uint16>
// bufferHandle : HSTRING_BUFFER* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WindowsPreallocateStringBuffer(
  length: DWORD;   // DWORD
  charBuffer: Pointer;   // WORD** out
  bufferHandle: Pointer   // HSTRING_BUFFER* out
): Integer; stdcall;
  external 'api-ms-win-core-winrt-string-l1-1-0.dll' name 'WindowsPreallocateStringBuffer';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WindowsPreallocateStringBuffer"
  c_WindowsPreallocateStringBuffer :: Word32 -> Ptr Word16 -> Ptr () -> IO Int32
-- length : DWORD -> Word32
-- charBuffer : WORD** out -> Ptr Word16
-- bufferHandle : HSTRING_BUFFER* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let windowspreallocatestringbuffer =
  foreign "WindowsPreallocateStringBuffer"
    (uint32_t @-> (ptr uint16_t) @-> (ptr void) @-> returning int32_t)
(* length : DWORD -> uint32_t *)
(* charBuffer : WORD** out -> (ptr uint16_t) *)
(* bufferHandle : HSTRING_BUFFER* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library api-ms-win-core-winrt-string-l1-1-0 (t "api-ms-win-core-winrt-string-l1-1-0.dll"))
(cffi:use-foreign-library api-ms-win-core-winrt-string-l1-1-0)

(cffi:defcfun ("WindowsPreallocateStringBuffer" windows-preallocate-string-buffer :convention :stdcall) :int32
  (length :uint32)   ; DWORD
  (char-buffer :pointer)   ; WORD** out
  (buffer-handle :pointer))   ; HSTRING_BUFFER* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WindowsPreallocateStringBuffer = Win32::API::More->new('api-ms-win-core-winrt-string-l1-1-0',
    'int WindowsPreallocateStringBuffer(DWORD length, LPVOID charBuffer, HANDLE bufferHandle)');
# my $ret = $WindowsPreallocateStringBuffer->Call($length, $charBuffer, $bufferHandle);
# length : DWORD -> DWORD
# charBuffer : WORD** out -> LPVOID
# bufferHandle : HSTRING_BUFFER* out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。