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

WindowsCreateStringReference

関数
コピーせず既存バッファを参照する高速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 WindowsCreateStringReference(
    LPCWSTR sourceString,   // optional
    DWORD length,
    HSTRING_HEADER* hstringHeader,
    HSTRING* string
);

パラメーター

名前方向説明
sourceStringLPCWSTRinoptional参照元となるUTF-16文字列。呼び出し中は有効である必要がある。
lengthDWORDinソース文字列の文字数(NUL終端を含まない)。
hstringHeaderHSTRING_HEADER*out高速パス参照用のヘッダ領域を格納するHSTRING_HEADERへのポインタ。
stringHSTRING*out出力として生成された参照HSTRINGを受け取るポインタ。

戻り値の型: HRESULT

各言語での呼び出し定義

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

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

WindowsCreateStringReference = ctypes.windll.LoadLibrary("api-ms-win-core-winrt-string-l1-1-0.dll").WindowsCreateStringReference
WindowsCreateStringReference.restype = ctypes.c_int
WindowsCreateStringReference.argtypes = [
    wintypes.LPCWSTR,  # sourceString : LPCWSTR optional
    wintypes.DWORD,  # length : DWORD
    ctypes.c_void_p,  # hstringHeader : HSTRING_HEADER* out
    ctypes.c_void_p,  # string : HSTRING* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('api-ms-win-core-winrt-string-l1-1-0.dll')
WindowsCreateStringReference = Fiddle::Function.new(
  lib['WindowsCreateStringReference'],
  [
    Fiddle::TYPE_VOIDP,  # sourceString : LPCWSTR optional
    -Fiddle::TYPE_INT,  # length : DWORD
    Fiddle::TYPE_VOIDP,  # hstringHeader : HSTRING_HEADER* out
    Fiddle::TYPE_VOIDP,  # string : HSTRING* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "api-ms-win-core-winrt-string-l1-1-0")]
extern "system" {
    fn WindowsCreateStringReference(
        sourceString: *const u16,  // LPCWSTR optional
        length: u32,  // DWORD
        hstringHeader: *mut HSTRING_HEADER,  // HSTRING_HEADER* out
        string: *mut *mut core::ffi::c_void  // HSTRING* 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 WindowsCreateStringReference([MarshalAs(UnmanagedType.LPWStr)] string sourceString, uint length, IntPtr hstringHeader, IntPtr string);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-winrt-string-l1-1-0_WindowsCreateStringReference' -Namespace Win32 -PassThru
# $api::WindowsCreateStringReference(sourceString, length, hstringHeader, string)
#uselib "api-ms-win-core-winrt-string-l1-1-0.dll"
#func global WindowsCreateStringReference "WindowsCreateStringReference" sptr, sptr, sptr, sptr
; WindowsCreateStringReference sourceString, length, varptr(hstringHeader), string   ; 戻り値は stat
; sourceString : LPCWSTR optional -> "sptr"
; length : DWORD -> "sptr"
; hstringHeader : HSTRING_HEADER* out -> "sptr"
; string : HSTRING* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "api-ms-win-core-winrt-string-l1-1-0.dll"
#cfunc global WindowsCreateStringReference "WindowsCreateStringReference" wstr, int, var, sptr
; res = WindowsCreateStringReference(sourceString, length, hstringHeader, string)
; sourceString : LPCWSTR optional -> "wstr"
; length : DWORD -> "int"
; hstringHeader : HSTRING_HEADER* out -> "var"
; string : HSTRING* out -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT WindowsCreateStringReference(LPCWSTR sourceString, DWORD length, HSTRING_HEADER* hstringHeader, HSTRING* string)
#uselib "api-ms-win-core-winrt-string-l1-1-0.dll"
#cfunc global WindowsCreateStringReference "WindowsCreateStringReference" wstr, int, var, intptr
; res = WindowsCreateStringReference(sourceString, length, hstringHeader, string)
; sourceString : LPCWSTR optional -> "wstr"
; length : DWORD -> "int"
; hstringHeader : HSTRING_HEADER* out -> "var"
; string : HSTRING* 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")
	procWindowsCreateStringReference = api_ms_win_core_winrt_string_l1_1_0.NewProc("WindowsCreateStringReference")
)

// sourceString (LPCWSTR optional), length (DWORD), hstringHeader (HSTRING_HEADER* out), string (HSTRING* out)
r1, _, err := procWindowsCreateStringReference.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(sourceString))),
	uintptr(length),
	uintptr(hstringHeader),
	uintptr(string),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function WindowsCreateStringReference(
  sourceString: PWideChar;   // LPCWSTR optional
  length: DWORD;   // DWORD
  hstringHeader: Pointer;   // HSTRING_HEADER* out
  string: Pointer   // HSTRING* out
): Integer; stdcall;
  external 'api-ms-win-core-winrt-string-l1-1-0.dll' name 'WindowsCreateStringReference';
result := DllCall("api-ms-win-core-winrt-string-l1-1-0\WindowsCreateStringReference"
    , "WStr", sourceString   ; LPCWSTR optional
    , "UInt", length   ; DWORD
    , "Ptr", hstringHeader   ; HSTRING_HEADER* out
    , "Ptr", string   ; HSTRING* out
    , "Int")   ; return: HRESULT
●WindowsCreateStringReference(sourceString, length, hstringHeader, string) = DLL("api-ms-win-core-winrt-string-l1-1-0.dll", "int WindowsCreateStringReference(char*, dword, void*, void*)")
# 呼び出し: WindowsCreateStringReference(sourceString, length, hstringHeader, string)
# sourceString : LPCWSTR optional -> "char*"
# length : DWORD -> "dword"
# hstringHeader : HSTRING_HEADER* out -> "void*"
# string : HSTRING* 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 WindowsCreateStringReference(
    sourceString: [*c]const u16, // LPCWSTR optional
    length: u32, // DWORD
    hstringHeader: [*c]HSTRING_HEADER, // HSTRING_HEADER* out
    string: ?*anyopaque // HSTRING* out
) callconv(std.os.windows.WINAPI) i32;
proc WindowsCreateStringReference(
    sourceString: WideCString,  # LPCWSTR optional
    length: uint32,  # DWORD
    hstringHeader: ptr HSTRING_HEADER,  # HSTRING_HEADER* out
    string: pointer  # HSTRING* out
): int32 {.importc: "WindowsCreateStringReference", 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 WindowsCreateStringReference(
    const(wchar)* sourceString,   // LPCWSTR optional
    uint length,   // DWORD
    HSTRING_HEADER* hstringHeader,   // HSTRING_HEADER* out
    void* string   // HSTRING* out
);
ccall((:WindowsCreateStringReference, "api-ms-win-core-winrt-string-l1-1-0.dll"), stdcall, Int32,
      (Cwstring, UInt32, Ptr{HSTRING_HEADER}, Ptr{Cvoid}),
      sourceString, length, hstringHeader, string)
# sourceString : LPCWSTR optional -> Cwstring
# length : DWORD -> UInt32
# hstringHeader : HSTRING_HEADER* out -> Ptr{HSTRING_HEADER}
# string : HSTRING* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WindowsCreateStringReference(
    const uint16_t* sourceString,
    uint32_t length,
    void* hstringHeader,
    void* string);
]]
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.WindowsCreateStringReference(sourceString, length, hstringHeader, string)
-- sourceString : LPCWSTR optional
-- length : DWORD
-- hstringHeader : HSTRING_HEADER* out
-- string : HSTRING* 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 WindowsCreateStringReference = lib.func('__stdcall', 'WindowsCreateStringReference', 'int32_t', ['str16', 'uint32_t', 'void *', 'void *']);
// WindowsCreateStringReference(sourceString, length, hstringHeader, string)
// sourceString : LPCWSTR optional -> 'str16'
// length : DWORD -> 'uint32_t'
// hstringHeader : HSTRING_HEADER* out -> 'void *'
// string : HSTRING* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("api-ms-win-core-winrt-string-l1-1-0.dll", {
  WindowsCreateStringReference: { parameters: ["buffer", "u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WindowsCreateStringReference(sourceString, length, hstringHeader, string)
// sourceString : LPCWSTR optional -> "buffer"
// length : DWORD -> "u32"
// hstringHeader : HSTRING_HEADER* out -> "pointer"
// string : HSTRING* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WindowsCreateStringReference(
    const uint16_t* sourceString,
    uint32_t length,
    void* hstringHeader,
    void* string);
C, "api-ms-win-core-winrt-string-l1-1-0.dll");
// $ffi->WindowsCreateStringReference(sourceString, length, hstringHeader, string);
// sourceString : LPCWSTR optional
// length : DWORD
// hstringHeader : HSTRING_HEADER* out
// string : HSTRING* 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 WindowsCreateStringReference(
        WString sourceString,   // LPCWSTR optional
        int length,   // DWORD
        Pointer hstringHeader,   // HSTRING_HEADER* out
        Pointer string   // HSTRING* out
    );
}
@[Link("api-ms-win-core-winrt-string-l1-1-0")]
lib Libapi-ms-win-core-winrt-string-l1-1-0
  fun WindowsCreateStringReference = WindowsCreateStringReference(
    sourceString : UInt16*,   # LPCWSTR optional
    length : UInt32,   # DWORD
    hstringHeader : HSTRING_HEADER*,   # HSTRING_HEADER* out
    string : Void*   # HSTRING* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WindowsCreateStringReferenceNative = Int32 Function(Pointer<Utf16>, Uint32, Pointer<Void>, Pointer<Void>);
typedef WindowsCreateStringReferenceDart = int Function(Pointer<Utf16>, int, Pointer<Void>, Pointer<Void>);
final WindowsCreateStringReference = DynamicLibrary.open('api-ms-win-core-winrt-string-l1-1-0.dll')
    .lookupFunction<WindowsCreateStringReferenceNative, WindowsCreateStringReferenceDart>('WindowsCreateStringReference');
// sourceString : LPCWSTR optional -> Pointer<Utf16>
// length : DWORD -> Uint32
// hstringHeader : HSTRING_HEADER* out -> Pointer<Void>
// string : HSTRING* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WindowsCreateStringReference(
  sourceString: PWideChar;   // LPCWSTR optional
  length: DWORD;   // DWORD
  hstringHeader: Pointer;   // HSTRING_HEADER* out
  string: Pointer   // HSTRING* out
): Integer; stdcall;
  external 'api-ms-win-core-winrt-string-l1-1-0.dll' name 'WindowsCreateStringReference';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WindowsCreateStringReference"
  c_WindowsCreateStringReference :: CWString -> Word32 -> Ptr () -> Ptr () -> IO Int32
-- sourceString : LPCWSTR optional -> CWString
-- length : DWORD -> Word32
-- hstringHeader : HSTRING_HEADER* out -> Ptr ()
-- string : HSTRING* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let windowscreatestringreference =
  foreign "WindowsCreateStringReference"
    ((ptr uint16_t) @-> uint32_t @-> (ptr void) @-> (ptr void) @-> returning int32_t)
(* sourceString : LPCWSTR optional -> (ptr uint16_t) *)
(* length : DWORD -> uint32_t *)
(* hstringHeader : HSTRING_HEADER* out -> (ptr void) *)
(* string : HSTRING* 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 ("WindowsCreateStringReference" windows-create-string-reference :convention :stdcall) :int32
  (source-string (:string :encoding :utf-16le))   ; LPCWSTR optional
  (length :uint32)   ; DWORD
  (hstring-header :pointer)   ; HSTRING_HEADER* out
  (string :pointer))   ; HSTRING* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WindowsCreateStringReference = Win32::API::More->new('api-ms-win-core-winrt-string-l1-1-0',
    'int WindowsCreateStringReference(LPCWSTR sourceString, DWORD length, LPVOID hstringHeader, HANDLE string)');
# my $ret = $WindowsCreateStringReference->Call($sourceString, $length, $hstringHeader, $string);
# sourceString : LPCWSTR optional -> LPCWSTR
# length : DWORD -> DWORD
# hstringHeader : HSTRING_HEADER* out -> LPVOID
# string : HSTRING* out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型