Win32 API 日本語リファレンス
ホームUI.Shell › SHCreateStreamOnFileW

SHCreateStreamOnFileW

関数
指定ファイルに対するIStreamオブジェクトを生成する。
DLLSHLWAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

HRESULT SHCreateStreamOnFileW(
    LPCWSTR pszFile,
    DWORD grfMode,
    IStream** ppstm
);

パラメーター

名前方向説明
pszFileLPCWSTRinファイル名を指定する null 終端文字列へのポインター。
grfModeDWORDinファイルアクセスモード、およびストリームを公開するオブジェクトの作成・削除方法を指定するために使用する、1 つ以上の STGM 値。
ppstmIStream**outファイルに関連付けられたストリームの IStream インターフェイスポインターを受け取ります。

戻り値の型: HRESULT

公式ドキュメント

SHCreateStreamOnFile は変更または利用できなくなる可能性があります。代わりに SHCreateStreamOnFileEx を使用してください。(Unicode)

戻り値

型: HRESULT

この関数が成功した場合は S_OK を返します。それ以外の場合は HRESULT エラーコードを返します。

解説(Remarks)

SHCreateStreamOnFileEx はすべての STGM モードを完全にサポートしており、新しいファイルを作成する場合には呼び出し元がファイル属性を指定できます。

メモ

shlwapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして SHCreateStreamOnFile を定義します。エンコーディング非依存のエイリアスを、エンコーディング非依存でないコードと混在させて使用すると、不整合が生じてコンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

HRESULT SHCreateStreamOnFileW(
    LPCWSTR pszFile,
    DWORD grfMode,
    IStream** ppstm
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int SHCreateStreamOnFileW(
    [MarshalAs(UnmanagedType.LPWStr)] string pszFile,   // LPCWSTR
    uint grfMode,   // DWORD
    IntPtr ppstm   // IStream** out
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function SHCreateStreamOnFileW(
    <MarshalAs(UnmanagedType.LPWStr)> pszFile As String,   ' LPCWSTR
    grfMode As UInteger,   ' DWORD
    ppstm As IntPtr   ' IStream** out
) As Integer
End Function
' pszFile : LPCWSTR
' grfMode : DWORD
' ppstm : IStream** out
Declare PtrSafe Function SHCreateStreamOnFileW Lib "shlwapi" ( _
    ByVal pszFile As LongPtr, _
    ByVal grfMode As Long, _
    ByVal ppstm 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

SHCreateStreamOnFileW = ctypes.windll.shlwapi.SHCreateStreamOnFileW
SHCreateStreamOnFileW.restype = ctypes.c_int
SHCreateStreamOnFileW.argtypes = [
    wintypes.LPCWSTR,  # pszFile : LPCWSTR
    wintypes.DWORD,  # grfMode : DWORD
    ctypes.c_void_p,  # ppstm : IStream** out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHLWAPI.dll')
SHCreateStreamOnFileW = Fiddle::Function.new(
  lib['SHCreateStreamOnFileW'],
  [
    Fiddle::TYPE_VOIDP,  # pszFile : LPCWSTR
    -Fiddle::TYPE_INT,  # grfMode : DWORD
    Fiddle::TYPE_VOIDP,  # ppstm : IStream** out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "shlwapi")]
extern "system" {
    fn SHCreateStreamOnFileW(
        pszFile: *const u16,  // LPCWSTR
        grfMode: u32,  // DWORD
        ppstm: *mut *mut core::ffi::c_void  // IStream** out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern int SHCreateStreamOnFileW([MarshalAs(UnmanagedType.LPWStr)] string pszFile, uint grfMode, IntPtr ppstm);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_SHCreateStreamOnFileW' -Namespace Win32 -PassThru
# $api::SHCreateStreamOnFileW(pszFile, grfMode, ppstm)
#uselib "SHLWAPI.dll"
#func global SHCreateStreamOnFileW "SHCreateStreamOnFileW" wptr, wptr, wptr
; SHCreateStreamOnFileW pszFile, grfMode, ppstm   ; 戻り値は stat
; pszFile : LPCWSTR -> "wptr"
; grfMode : DWORD -> "wptr"
; ppstm : IStream** out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "SHLWAPI.dll"
#cfunc global SHCreateStreamOnFileW "SHCreateStreamOnFileW" wstr, int, sptr
; res = SHCreateStreamOnFileW(pszFile, grfMode, ppstm)
; pszFile : LPCWSTR -> "wstr"
; grfMode : DWORD -> "int"
; ppstm : IStream** out -> "sptr"
; HRESULT SHCreateStreamOnFileW(LPCWSTR pszFile, DWORD grfMode, IStream** ppstm)
#uselib "SHLWAPI.dll"
#cfunc global SHCreateStreamOnFileW "SHCreateStreamOnFileW" wstr, int, intptr
; res = SHCreateStreamOnFileW(pszFile, grfMode, ppstm)
; pszFile : LPCWSTR -> "wstr"
; grfMode : DWORD -> "int"
; ppstm : IStream** out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procSHCreateStreamOnFileW = shlwapi.NewProc("SHCreateStreamOnFileW")
)

// pszFile (LPCWSTR), grfMode (DWORD), ppstm (IStream** out)
r1, _, err := procSHCreateStreamOnFileW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszFile))),
	uintptr(grfMode),
	uintptr(ppstm),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function SHCreateStreamOnFileW(
  pszFile: PWideChar;   // LPCWSTR
  grfMode: DWORD;   // DWORD
  ppstm: Pointer   // IStream** out
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'SHCreateStreamOnFileW';
result := DllCall("SHLWAPI\SHCreateStreamOnFileW"
    , "WStr", pszFile   ; LPCWSTR
    , "UInt", grfMode   ; DWORD
    , "Ptr", ppstm   ; IStream** out
    , "Int")   ; return: HRESULT
●SHCreateStreamOnFileW(pszFile, grfMode, ppstm) = DLL("SHLWAPI.dll", "int SHCreateStreamOnFileW(char*, dword, void*)")
# 呼び出し: SHCreateStreamOnFileW(pszFile, grfMode, ppstm)
# pszFile : LPCWSTR -> "char*"
# grfMode : DWORD -> "dword"
# ppstm : IStream** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "shlwapi" fn SHCreateStreamOnFileW(
    pszFile: [*c]const u16, // LPCWSTR
    grfMode: u32, // DWORD
    ppstm: ?*anyopaque // IStream** out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc SHCreateStreamOnFileW(
    pszFile: WideCString,  # LPCWSTR
    grfMode: uint32,  # DWORD
    ppstm: pointer  # IStream** out
): int32 {.importc: "SHCreateStreamOnFileW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "shlwapi");
extern(Windows)
int SHCreateStreamOnFileW(
    const(wchar)* pszFile,   // LPCWSTR
    uint grfMode,   // DWORD
    void* ppstm   // IStream** out
);
ccall((:SHCreateStreamOnFileW, "SHLWAPI.dll"), stdcall, Int32,
      (Cwstring, UInt32, Ptr{Cvoid}),
      pszFile, grfMode, ppstm)
# pszFile : LPCWSTR -> Cwstring
# grfMode : DWORD -> UInt32
# ppstm : IStream** out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t SHCreateStreamOnFileW(
    const uint16_t* pszFile,
    uint32_t grfMode,
    void* ppstm);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.SHCreateStreamOnFileW(pszFile, grfMode, ppstm)
-- pszFile : LPCWSTR
-- grfMode : DWORD
-- ppstm : IStream** out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const SHCreateStreamOnFileW = lib.func('__stdcall', 'SHCreateStreamOnFileW', 'int32_t', ['str16', 'uint32_t', 'void *']);
// SHCreateStreamOnFileW(pszFile, grfMode, ppstm)
// pszFile : LPCWSTR -> 'str16'
// grfMode : DWORD -> 'uint32_t'
// ppstm : IStream** out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  SHCreateStreamOnFileW: { parameters: ["buffer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.SHCreateStreamOnFileW(pszFile, grfMode, ppstm)
// pszFile : LPCWSTR -> "buffer"
// grfMode : DWORD -> "u32"
// ppstm : IStream** out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SHCreateStreamOnFileW(
    const uint16_t* pszFile,
    uint32_t grfMode,
    void* ppstm);
C, "SHLWAPI.dll");
// $ffi->SHCreateStreamOnFileW(pszFile, grfMode, ppstm);
// pszFile : LPCWSTR
// grfMode : DWORD
// ppstm : IStream** 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 Shlwapi extends StdCallLibrary {
    Shlwapi INSTANCE = Native.load("shlwapi", Shlwapi.class, W32APIOptions.UNICODE_OPTIONS);
    int SHCreateStreamOnFileW(
        WString pszFile,   // LPCWSTR
        int grfMode,   // DWORD
        Pointer ppstm   // IStream** out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("shlwapi")]
lib LibSHLWAPI
  fun SHCreateStreamOnFileW = SHCreateStreamOnFileW(
    pszFile : UInt16*,   # LPCWSTR
    grfMode : UInt32,   # DWORD
    ppstm : Void*   # IStream** out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SHCreateStreamOnFileWNative = Int32 Function(Pointer<Utf16>, Uint32, Pointer<Void>);
typedef SHCreateStreamOnFileWDart = int Function(Pointer<Utf16>, int, Pointer<Void>);
final SHCreateStreamOnFileW = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<SHCreateStreamOnFileWNative, SHCreateStreamOnFileWDart>('SHCreateStreamOnFileW');
// pszFile : LPCWSTR -> Pointer<Utf16>
// grfMode : DWORD -> Uint32
// ppstm : IStream** out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SHCreateStreamOnFileW(
  pszFile: PWideChar;   // LPCWSTR
  grfMode: DWORD;   // DWORD
  ppstm: Pointer   // IStream** out
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'SHCreateStreamOnFileW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SHCreateStreamOnFileW"
  c_SHCreateStreamOnFileW :: CWString -> Word32 -> Ptr () -> IO Int32
-- pszFile : LPCWSTR -> CWString
-- grfMode : DWORD -> Word32
-- ppstm : IStream** out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shcreatestreamonfilew =
  foreign "SHCreateStreamOnFileW"
    ((ptr uint16_t) @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* pszFile : LPCWSTR -> (ptr uint16_t) *)
(* grfMode : DWORD -> uint32_t *)
(* ppstm : IStream** out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)

(cffi:defcfun ("SHCreateStreamOnFileW" shcreate-stream-on-file-w :convention :stdcall) :int32
  (psz-file (:string :encoding :utf-16le))   ; LPCWSTR
  (grf-mode :uint32)   ; DWORD
  (ppstm :pointer))   ; IStream** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHCreateStreamOnFileW = Win32::API::More->new('SHLWAPI',
    'int SHCreateStreamOnFileW(LPCWSTR pszFile, DWORD grfMode, LPVOID ppstm)');
# my $ret = $SHCreateStreamOnFileW->Call($pszFile, $grfMode, $ppstm);
# pszFile : LPCWSTR -> LPCWSTR
# grfMode : DWORD -> DWORD
# ppstm : IStream** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API
使用する型