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

SHCreateStreamOnFileEx

関数
属性やテンプレート指定でファイルのストリームを生成する。
DLLSHLWAPI.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

// SHLWAPI.dll
#include <windows.h>

HRESULT SHCreateStreamOnFileEx(
    LPCWSTR pszFile,
    DWORD grfMode,
    DWORD dwAttributes,
    BOOL fCreate,
    IStream* pstmTemplate,   // optional
    IStream** ppstm
);

パラメーター

名前方向説明
pszFileLPCWSTRinファイル名を指定する、null で終わる文字列へのポインターです。
grfModeDWORDinファイルアクセスモードと、ストリームを公開するオブジェクトの作成および削除方法を指定するために使用する、1 つ以上の STGM 値です。
dwAttributesDWORDin新しいファイルが作成される場合にファイル属性を指定する、1 つ以上のフラグ値です。指定可能な値の一覧については、CreateFile 関数の dwFlagsAndAttributes パラメーターを参照してください。
fCreateBOOLingrfMode と組み合わせて、ストリーム作成時に既存ファイルをどのように扱うかを指定する BOOL 値です。詳細は「解説」を参照してください。
pstmTemplateIStream*inoptional予約済みです。
ppstmIStream**outファイルに関連付けられたストリームの IStream インターフェイスポインターを受け取ります。

戻り値の型: HRESULT

公式ドキュメント

ファイルを開く、または作成し、そのファイルの読み取りまたは書き込みを行うためのストリームを取得します。

戻り値

型: HRESULT

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

解説(Remarks)

SHCreateStreamOnFileEx 関数は STGM フラグのセマンティクスを拡張し、CreateFile 関数を呼び出した場合と同じ効果を生み出します。

grfMode パラメーターと fCreate パラメーターは連携して、既存ファイルに対する関数の動作を指定します。

grfMode fCreate ファイルが存在するか? 動作
STGM_CREATE 無視されます はい ファイルが再作成されます。
STGM_CREATE 無視されます いいえ ファイルが作成されます。
STGM_FAILIFTHERE FALSE はい ファイルが開かれます。
STGM_FAILIFTHERE FALSE いいえ 呼び出しが失敗します。
STGM_FAILIFTHERE TRUE はい 呼び出しが失敗します。
STGM_FAILIFTHERE TRUE いいえ ファイルが作成されます。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// SHLWAPI.dll
#include <windows.h>

HRESULT SHCreateStreamOnFileEx(
    LPCWSTR pszFile,
    DWORD grfMode,
    DWORD dwAttributes,
    BOOL fCreate,
    IStream* pstmTemplate,   // optional
    IStream** ppstm
);
[DllImport("SHLWAPI.dll", ExactSpelling = true)]
static extern int SHCreateStreamOnFileEx(
    [MarshalAs(UnmanagedType.LPWStr)] string pszFile,   // LPCWSTR
    uint grfMode,   // DWORD
    uint dwAttributes,   // DWORD
    bool fCreate,   // BOOL
    IntPtr pstmTemplate,   // IStream* optional
    IntPtr ppstm   // IStream** out
);
<DllImport("SHLWAPI.dll", ExactSpelling:=True)>
Public Shared Function SHCreateStreamOnFileEx(
    <MarshalAs(UnmanagedType.LPWStr)> pszFile As String,   ' LPCWSTR
    grfMode As UInteger,   ' DWORD
    dwAttributes As UInteger,   ' DWORD
    fCreate As Boolean,   ' BOOL
    pstmTemplate As IntPtr,   ' IStream* optional
    ppstm As IntPtr   ' IStream** out
) As Integer
End Function
' pszFile : LPCWSTR
' grfMode : DWORD
' dwAttributes : DWORD
' fCreate : BOOL
' pstmTemplate : IStream* optional
' ppstm : IStream** out
Declare PtrSafe Function SHCreateStreamOnFileEx Lib "shlwapi" ( _
    ByVal pszFile As LongPtr, _
    ByVal grfMode As Long, _
    ByVal dwAttributes As Long, _
    ByVal fCreate As Long, _
    ByVal pstmTemplate As LongPtr, _
    ByVal ppstm As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

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

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procSHCreateStreamOnFileEx = shlwapi.NewProc("SHCreateStreamOnFileEx")
)

// pszFile (LPCWSTR), grfMode (DWORD), dwAttributes (DWORD), fCreate (BOOL), pstmTemplate (IStream* optional), ppstm (IStream** out)
r1, _, err := procSHCreateStreamOnFileEx.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszFile))),
	uintptr(grfMode),
	uintptr(dwAttributes),
	uintptr(fCreate),
	uintptr(pstmTemplate),
	uintptr(ppstm),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function SHCreateStreamOnFileEx(
  pszFile: PWideChar;   // LPCWSTR
  grfMode: DWORD;   // DWORD
  dwAttributes: DWORD;   // DWORD
  fCreate: BOOL;   // BOOL
  pstmTemplate: Pointer;   // IStream* optional
  ppstm: Pointer   // IStream** out
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'SHCreateStreamOnFileEx';
result := DllCall("SHLWAPI\SHCreateStreamOnFileEx"
    , "WStr", pszFile   ; LPCWSTR
    , "UInt", grfMode   ; DWORD
    , "UInt", dwAttributes   ; DWORD
    , "Int", fCreate   ; BOOL
    , "Ptr", pstmTemplate   ; IStream* optional
    , "Ptr", ppstm   ; IStream** out
    , "Int")   ; return: HRESULT
●SHCreateStreamOnFileEx(pszFile, grfMode, dwAttributes, fCreate, pstmTemplate, ppstm) = DLL("SHLWAPI.dll", "int SHCreateStreamOnFileEx(char*, dword, dword, bool, void*, void*)")
# 呼び出し: SHCreateStreamOnFileEx(pszFile, grfMode, dwAttributes, fCreate, pstmTemplate, ppstm)
# pszFile : LPCWSTR -> "char*"
# grfMode : DWORD -> "dword"
# dwAttributes : DWORD -> "dword"
# fCreate : BOOL -> "bool"
# pstmTemplate : IStream* optional -> "void*"
# ppstm : IStream** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shlwapi" fn SHCreateStreamOnFileEx(
    pszFile: [*c]const u16, // LPCWSTR
    grfMode: u32, // DWORD
    dwAttributes: u32, // DWORD
    fCreate: i32, // BOOL
    pstmTemplate: ?*anyopaque, // IStream* optional
    ppstm: ?*anyopaque // IStream** out
) callconv(std.os.windows.WINAPI) i32;
proc SHCreateStreamOnFileEx(
    pszFile: WideCString,  # LPCWSTR
    grfMode: uint32,  # DWORD
    dwAttributes: uint32,  # DWORD
    fCreate: int32,  # BOOL
    pstmTemplate: pointer,  # IStream* optional
    ppstm: pointer  # IStream** out
): int32 {.importc: "SHCreateStreamOnFileEx", stdcall, dynlib: "SHLWAPI.dll".}
pragma(lib, "shlwapi");
extern(Windows)
int SHCreateStreamOnFileEx(
    const(wchar)* pszFile,   // LPCWSTR
    uint grfMode,   // DWORD
    uint dwAttributes,   // DWORD
    int fCreate,   // BOOL
    void* pstmTemplate,   // IStream* optional
    void* ppstm   // IStream** out
);
ccall((:SHCreateStreamOnFileEx, "SHLWAPI.dll"), stdcall, Int32,
      (Cwstring, UInt32, UInt32, Int32, Ptr{Cvoid}, Ptr{Cvoid}),
      pszFile, grfMode, dwAttributes, fCreate, pstmTemplate, ppstm)
# pszFile : LPCWSTR -> Cwstring
# grfMode : DWORD -> UInt32
# dwAttributes : DWORD -> UInt32
# fCreate : BOOL -> Int32
# pstmTemplate : IStream* optional -> Ptr{Cvoid}
# ppstm : IStream** out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SHCreateStreamOnFileEx(
    const uint16_t* pszFile,
    uint32_t grfMode,
    uint32_t dwAttributes,
    int32_t fCreate,
    void* pstmTemplate,
    void* ppstm);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.SHCreateStreamOnFileEx(pszFile, grfMode, dwAttributes, fCreate, pstmTemplate, ppstm)
-- pszFile : LPCWSTR
-- grfMode : DWORD
-- dwAttributes : DWORD
-- fCreate : BOOL
-- pstmTemplate : IStream* optional
-- ppstm : IStream** out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const SHCreateStreamOnFileEx = lib.func('__stdcall', 'SHCreateStreamOnFileEx', 'int32_t', ['str16', 'uint32_t', 'uint32_t', 'int32_t', 'void *', 'void *']);
// SHCreateStreamOnFileEx(pszFile, grfMode, dwAttributes, fCreate, pstmTemplate, ppstm)
// pszFile : LPCWSTR -> 'str16'
// grfMode : DWORD -> 'uint32_t'
// dwAttributes : DWORD -> 'uint32_t'
// fCreate : BOOL -> 'int32_t'
// pstmTemplate : IStream* optional -> 'void *'
// ppstm : IStream** out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  SHCreateStreamOnFileEx: { parameters: ["buffer", "u32", "u32", "i32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.SHCreateStreamOnFileEx(pszFile, grfMode, dwAttributes, fCreate, pstmTemplate, ppstm)
// pszFile : LPCWSTR -> "buffer"
// grfMode : DWORD -> "u32"
// dwAttributes : DWORD -> "u32"
// fCreate : BOOL -> "i32"
// pstmTemplate : IStream* optional -> "pointer"
// ppstm : IStream** out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SHCreateStreamOnFileEx(
    const uint16_t* pszFile,
    uint32_t grfMode,
    uint32_t dwAttributes,
    int32_t fCreate,
    void* pstmTemplate,
    void* ppstm);
C, "SHLWAPI.dll");
// $ffi->SHCreateStreamOnFileEx(pszFile, grfMode, dwAttributes, fCreate, pstmTemplate, ppstm);
// pszFile : LPCWSTR
// grfMode : DWORD
// dwAttributes : DWORD
// fCreate : BOOL
// pstmTemplate : IStream* optional
// 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);
    int SHCreateStreamOnFileEx(
        WString pszFile,   // LPCWSTR
        int grfMode,   // DWORD
        int dwAttributes,   // DWORD
        boolean fCreate,   // BOOL
        Pointer pstmTemplate,   // IStream* optional
        Pointer ppstm   // IStream** out
    );
}
@[Link("shlwapi")]
lib LibSHLWAPI
  fun SHCreateStreamOnFileEx = SHCreateStreamOnFileEx(
    pszFile : UInt16*,   # LPCWSTR
    grfMode : UInt32,   # DWORD
    dwAttributes : UInt32,   # DWORD
    fCreate : Int32,   # BOOL
    pstmTemplate : Void*,   # IStream* optional
    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 SHCreateStreamOnFileExNative = Int32 Function(Pointer<Utf16>, Uint32, Uint32, Int32, Pointer<Void>, Pointer<Void>);
typedef SHCreateStreamOnFileExDart = int Function(Pointer<Utf16>, int, int, int, Pointer<Void>, Pointer<Void>);
final SHCreateStreamOnFileEx = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<SHCreateStreamOnFileExNative, SHCreateStreamOnFileExDart>('SHCreateStreamOnFileEx');
// pszFile : LPCWSTR -> Pointer<Utf16>
// grfMode : DWORD -> Uint32
// dwAttributes : DWORD -> Uint32
// fCreate : BOOL -> Int32
// pstmTemplate : IStream* optional -> Pointer<Void>
// ppstm : IStream** out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SHCreateStreamOnFileEx(
  pszFile: PWideChar;   // LPCWSTR
  grfMode: DWORD;   // DWORD
  dwAttributes: DWORD;   // DWORD
  fCreate: BOOL;   // BOOL
  pstmTemplate: Pointer;   // IStream* optional
  ppstm: Pointer   // IStream** out
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'SHCreateStreamOnFileEx';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let shcreatestreamonfileex =
  foreign "SHCreateStreamOnFileEx"
    ((ptr uint16_t) @-> uint32_t @-> uint32_t @-> int32_t @-> (ptr void) @-> (ptr void) @-> returning int32_t)
(* pszFile : LPCWSTR -> (ptr uint16_t) *)
(* grfMode : DWORD -> uint32_t *)
(* dwAttributes : DWORD -> uint32_t *)
(* fCreate : BOOL -> int32_t *)
(* pstmTemplate : IStream* optional -> (ptr void) *)
(* 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 ("SHCreateStreamOnFileEx" shcreate-stream-on-file-ex :convention :stdcall) :int32
  (psz-file (:string :encoding :utf-16le))   ; LPCWSTR
  (grf-mode :uint32)   ; DWORD
  (dw-attributes :uint32)   ; DWORD
  (f-create :int32)   ; BOOL
  (pstm-template :pointer)   ; IStream* optional
  (ppstm :pointer))   ; IStream** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHCreateStreamOnFileEx = Win32::API::More->new('SHLWAPI',
    'int SHCreateStreamOnFileEx(LPCWSTR pszFile, DWORD grfMode, DWORD dwAttributes, BOOL fCreate, LPVOID pstmTemplate, LPVOID ppstm)');
# my $ret = $SHCreateStreamOnFileEx->Call($pszFile, $grfMode, $dwAttributes, $fCreate, $pstmTemplate, $ppstm);
# pszFile : LPCWSTR -> LPCWSTR
# grfMode : DWORD -> DWORD
# dwAttributes : DWORD -> DWORD
# fCreate : BOOL -> BOOL
# pstmTemplate : IStream* optional -> LPVOID
# ppstm : IStream** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
使用する型