ホーム › System.Pipes › CreateNamedPipeW
CreateNamedPipeW
関数名前付きパイプのインスタンスを作成する。
シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
HANDLE CreateNamedPipeW(
LPCWSTR lpName,
FILE_FLAGS_AND_ATTRIBUTES dwOpenMode,
NAMED_PIPE_MODE dwPipeMode,
DWORD nMaxInstances,
DWORD nOutBufferSize,
DWORD nInBufferSize,
DWORD nDefaultTimeOut,
SECURITY_ATTRIBUTES* lpSecurityAttributes // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpName | LPCWSTR | in | 作成するパイプの名前(Unicode、\\.\pipe\名 形式)を指定する。 |
| dwOpenMode | FILE_FLAGS_AND_ATTRIBUTES | in | アクセス方向やオーバーラップ等を示すオープンモードフラグを指定する。 |
| dwPipeMode | NAMED_PIPE_MODE | in | バイト/メッセージ型、読取モード、待機モード等を示すパイプモードフラグを指定する。 |
| nMaxInstances | DWORD | in | 作成可能な最大インスタンス数。PIPE_UNLIMITED_INSTANCESで無制限。 |
| nOutBufferSize | DWORD | in | 出力バッファに確保するバイト数を指定する。 |
| nInBufferSize | DWORD | in | 入力バッファに確保するバイト数を指定する。 |
| nDefaultTimeOut | DWORD | in | WaitNamedPipe既定のタイムアウト(ミリ秒)を指定する。 |
| lpSecurityAttributes | SECURITY_ATTRIBUTES* | inoptional | セキュリティ記述子と継承可否を指定するSECURITY_ATTRIBUTES。NULLで既定。 |
戻り値の型: HANDLE
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
HANDLE CreateNamedPipeW(
LPCWSTR lpName,
FILE_FLAGS_AND_ATTRIBUTES dwOpenMode,
NAMED_PIPE_MODE dwPipeMode,
DWORD nMaxInstances,
DWORD nOutBufferSize,
DWORD nInBufferSize,
DWORD nDefaultTimeOut,
SECURITY_ATTRIBUTES* lpSecurityAttributes // optional
);[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr CreateNamedPipeW(
[MarshalAs(UnmanagedType.LPWStr)] string lpName, // LPCWSTR
uint dwOpenMode, // FILE_FLAGS_AND_ATTRIBUTES
uint dwPipeMode, // NAMED_PIPE_MODE
uint nMaxInstances, // DWORD
uint nOutBufferSize, // DWORD
uint nInBufferSize, // DWORD
uint nDefaultTimeOut, // DWORD
IntPtr lpSecurityAttributes // SECURITY_ATTRIBUTES* optional
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function CreateNamedPipeW(
<MarshalAs(UnmanagedType.LPWStr)> lpName As String, ' LPCWSTR
dwOpenMode As UInteger, ' FILE_FLAGS_AND_ATTRIBUTES
dwPipeMode As UInteger, ' NAMED_PIPE_MODE
nMaxInstances As UInteger, ' DWORD
nOutBufferSize As UInteger, ' DWORD
nInBufferSize As UInteger, ' DWORD
nDefaultTimeOut As UInteger, ' DWORD
lpSecurityAttributes As IntPtr ' SECURITY_ATTRIBUTES* optional
) As IntPtr
End Function' lpName : LPCWSTR
' dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES
' dwPipeMode : NAMED_PIPE_MODE
' nMaxInstances : DWORD
' nOutBufferSize : DWORD
' nInBufferSize : DWORD
' nDefaultTimeOut : DWORD
' lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
Declare PtrSafe Function CreateNamedPipeW Lib "kernel32" ( _
ByVal lpName As LongPtr, _
ByVal dwOpenMode As Long, _
ByVal dwPipeMode As Long, _
ByVal nMaxInstances As Long, _
ByVal nOutBufferSize As Long, _
ByVal nInBufferSize As Long, _
ByVal nDefaultTimeOut As Long, _
ByVal lpSecurityAttributes As LongPtr) As LongPtr
' 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
CreateNamedPipeW = ctypes.windll.kernel32.CreateNamedPipeW
CreateNamedPipeW.restype = ctypes.c_void_p
CreateNamedPipeW.argtypes = [
wintypes.LPCWSTR, # lpName : LPCWSTR
wintypes.DWORD, # dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES
wintypes.DWORD, # dwPipeMode : NAMED_PIPE_MODE
wintypes.DWORD, # nMaxInstances : DWORD
wintypes.DWORD, # nOutBufferSize : DWORD
wintypes.DWORD, # nInBufferSize : DWORD
wintypes.DWORD, # nDefaultTimeOut : DWORD
ctypes.c_void_p, # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
CreateNamedPipeW = Fiddle::Function.new(
lib['CreateNamedPipeW'],
[
Fiddle::TYPE_VOIDP, # lpName : LPCWSTR
-Fiddle::TYPE_INT, # dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES
-Fiddle::TYPE_INT, # dwPipeMode : NAMED_PIPE_MODE
-Fiddle::TYPE_INT, # nMaxInstances : DWORD
-Fiddle::TYPE_INT, # nOutBufferSize : DWORD
-Fiddle::TYPE_INT, # nInBufferSize : DWORD
-Fiddle::TYPE_INT, # nDefaultTimeOut : DWORD
Fiddle::TYPE_VOIDP, # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn CreateNamedPipeW(
lpName: *const u16, // LPCWSTR
dwOpenMode: u32, // FILE_FLAGS_AND_ATTRIBUTES
dwPipeMode: u32, // NAMED_PIPE_MODE
nMaxInstances: u32, // DWORD
nOutBufferSize: u32, // DWORD
nInBufferSize: u32, // DWORD
nDefaultTimeOut: u32, // DWORD
lpSecurityAttributes: *mut SECURITY_ATTRIBUTES // SECURITY_ATTRIBUTES* optional
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr CreateNamedPipeW([MarshalAs(UnmanagedType.LPWStr)] string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateNamedPipeW' -Namespace Win32 -PassThru
# $api::CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes)#uselib "KERNEL32.dll"
#func global CreateNamedPipeW "CreateNamedPipeW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; CreateNamedPipeW lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, varptr(lpSecurityAttributes) ; 戻り値は stat
; lpName : LPCWSTR -> "wptr"
; dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> "wptr"
; dwPipeMode : NAMED_PIPE_MODE -> "wptr"
; nMaxInstances : DWORD -> "wptr"
; nOutBufferSize : DWORD -> "wptr"
; nInBufferSize : DWORD -> "wptr"
; nDefaultTimeOut : DWORD -> "wptr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global CreateNamedPipeW "CreateNamedPipeW" wstr, int, int, int, int, int, int, var ; res = CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes) ; lpName : LPCWSTR -> "wstr" ; dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> "int" ; dwPipeMode : NAMED_PIPE_MODE -> "int" ; nMaxInstances : DWORD -> "int" ; nOutBufferSize : DWORD -> "int" ; nInBufferSize : DWORD -> "int" ; nDefaultTimeOut : DWORD -> "int" ; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global CreateNamedPipeW "CreateNamedPipeW" wstr, int, int, int, int, int, int, sptr ; res = CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, varptr(lpSecurityAttributes)) ; lpName : LPCWSTR -> "wstr" ; dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> "int" ; dwPipeMode : NAMED_PIPE_MODE -> "int" ; nMaxInstances : DWORD -> "int" ; nOutBufferSize : DWORD -> "int" ; nInBufferSize : DWORD -> "int" ; nDefaultTimeOut : DWORD -> "int" ; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HANDLE CreateNamedPipeW(LPCWSTR lpName, FILE_FLAGS_AND_ATTRIBUTES dwOpenMode, NAMED_PIPE_MODE dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, SECURITY_ATTRIBUTES* lpSecurityAttributes) #uselib "KERNEL32.dll" #cfunc global CreateNamedPipeW "CreateNamedPipeW" wstr, int, int, int, int, int, int, var ; res = CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes) ; lpName : LPCWSTR -> "wstr" ; dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> "int" ; dwPipeMode : NAMED_PIPE_MODE -> "int" ; nMaxInstances : DWORD -> "int" ; nOutBufferSize : DWORD -> "int" ; nInBufferSize : DWORD -> "int" ; nDefaultTimeOut : DWORD -> "int" ; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HANDLE CreateNamedPipeW(LPCWSTR lpName, FILE_FLAGS_AND_ATTRIBUTES dwOpenMode, NAMED_PIPE_MODE dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, SECURITY_ATTRIBUTES* lpSecurityAttributes) #uselib "KERNEL32.dll" #cfunc global CreateNamedPipeW "CreateNamedPipeW" wstr, int, int, int, int, int, int, intptr ; res = CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, varptr(lpSecurityAttributes)) ; lpName : LPCWSTR -> "wstr" ; dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> "int" ; dwPipeMode : NAMED_PIPE_MODE -> "int" ; nMaxInstances : DWORD -> "int" ; nOutBufferSize : DWORD -> "int" ; nInBufferSize : DWORD -> "int" ; nDefaultTimeOut : DWORD -> "int" ; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procCreateNamedPipeW = kernel32.NewProc("CreateNamedPipeW")
)
// lpName (LPCWSTR), dwOpenMode (FILE_FLAGS_AND_ATTRIBUTES), dwPipeMode (NAMED_PIPE_MODE), nMaxInstances (DWORD), nOutBufferSize (DWORD), nInBufferSize (DWORD), nDefaultTimeOut (DWORD), lpSecurityAttributes (SECURITY_ATTRIBUTES* optional)
r1, _, err := procCreateNamedPipeW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpName))),
uintptr(dwOpenMode),
uintptr(dwPipeMode),
uintptr(nMaxInstances),
uintptr(nOutBufferSize),
uintptr(nInBufferSize),
uintptr(nDefaultTimeOut),
uintptr(lpSecurityAttributes),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HANDLEfunction CreateNamedPipeW(
lpName: PWideChar; // LPCWSTR
dwOpenMode: DWORD; // FILE_FLAGS_AND_ATTRIBUTES
dwPipeMode: DWORD; // NAMED_PIPE_MODE
nMaxInstances: DWORD; // DWORD
nOutBufferSize: DWORD; // DWORD
nInBufferSize: DWORD; // DWORD
nDefaultTimeOut: DWORD; // DWORD
lpSecurityAttributes: Pointer // SECURITY_ATTRIBUTES* optional
): THandle; stdcall;
external 'KERNEL32.dll' name 'CreateNamedPipeW';result := DllCall("KERNEL32\CreateNamedPipeW"
, "WStr", lpName ; LPCWSTR
, "UInt", dwOpenMode ; FILE_FLAGS_AND_ATTRIBUTES
, "UInt", dwPipeMode ; NAMED_PIPE_MODE
, "UInt", nMaxInstances ; DWORD
, "UInt", nOutBufferSize ; DWORD
, "UInt", nInBufferSize ; DWORD
, "UInt", nDefaultTimeOut ; DWORD
, "Ptr", lpSecurityAttributes ; SECURITY_ATTRIBUTES* optional
, "Ptr") ; return: HANDLE●CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes) = DLL("KERNEL32.dll", "void* CreateNamedPipeW(char*, dword, dword, dword, dword, dword, dword, void*)")
# 呼び出し: CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes)
# lpName : LPCWSTR -> "char*"
# dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> "dword"
# dwPipeMode : NAMED_PIPE_MODE -> "dword"
# nMaxInstances : DWORD -> "dword"
# nOutBufferSize : DWORD -> "dword"
# nInBufferSize : DWORD -> "dword"
# nDefaultTimeOut : DWORD -> "dword"
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "kernel32" fn CreateNamedPipeW(
lpName: [*c]const u16, // LPCWSTR
dwOpenMode: u32, // FILE_FLAGS_AND_ATTRIBUTES
dwPipeMode: u32, // NAMED_PIPE_MODE
nMaxInstances: u32, // DWORD
nOutBufferSize: u32, // DWORD
nInBufferSize: u32, // DWORD
nDefaultTimeOut: u32, // DWORD
lpSecurityAttributes: [*c]SECURITY_ATTRIBUTES // SECURITY_ATTRIBUTES* optional
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc CreateNamedPipeW(
lpName: WideCString, # LPCWSTR
dwOpenMode: uint32, # FILE_FLAGS_AND_ATTRIBUTES
dwPipeMode: uint32, # NAMED_PIPE_MODE
nMaxInstances: uint32, # DWORD
nOutBufferSize: uint32, # DWORD
nInBufferSize: uint32, # DWORD
nDefaultTimeOut: uint32, # DWORD
lpSecurityAttributes: ptr SECURITY_ATTRIBUTES # SECURITY_ATTRIBUTES* optional
): pointer {.importc: "CreateNamedPipeW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "kernel32");
extern(Windows)
void* CreateNamedPipeW(
const(wchar)* lpName, // LPCWSTR
uint dwOpenMode, // FILE_FLAGS_AND_ATTRIBUTES
uint dwPipeMode, // NAMED_PIPE_MODE
uint nMaxInstances, // DWORD
uint nOutBufferSize, // DWORD
uint nInBufferSize, // DWORD
uint nDefaultTimeOut, // DWORD
SECURITY_ATTRIBUTES* lpSecurityAttributes // SECURITY_ATTRIBUTES* optional
);ccall((:CreateNamedPipeW, "KERNEL32.dll"), stdcall, Ptr{Cvoid},
(Cwstring, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, Ptr{SECURITY_ATTRIBUTES}),
lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes)
# lpName : LPCWSTR -> Cwstring
# dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> UInt32
# dwPipeMode : NAMED_PIPE_MODE -> UInt32
# nMaxInstances : DWORD -> UInt32
# nOutBufferSize : DWORD -> UInt32
# nInBufferSize : DWORD -> UInt32
# nDefaultTimeOut : DWORD -> UInt32
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> Ptr{SECURITY_ATTRIBUTES}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
void* CreateNamedPipeW(
const uint16_t* lpName,
uint32_t dwOpenMode,
uint32_t dwPipeMode,
uint32_t nMaxInstances,
uint32_t nOutBufferSize,
uint32_t nInBufferSize,
uint32_t nDefaultTimeOut,
void* lpSecurityAttributes);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes)
-- lpName : LPCWSTR
-- dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES
-- dwPipeMode : NAMED_PIPE_MODE
-- nMaxInstances : DWORD
-- nOutBufferSize : DWORD
-- nInBufferSize : DWORD
-- nDefaultTimeOut : DWORD
-- lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const CreateNamedPipeW = lib.func('__stdcall', 'CreateNamedPipeW', 'void *', ['str16', 'uint32_t', 'uint32_t', 'uint32_t', 'uint32_t', 'uint32_t', 'uint32_t', 'void *']);
// CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes)
// lpName : LPCWSTR -> 'str16'
// dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> 'uint32_t'
// dwPipeMode : NAMED_PIPE_MODE -> 'uint32_t'
// nMaxInstances : DWORD -> 'uint32_t'
// nOutBufferSize : DWORD -> 'uint32_t'
// nInBufferSize : DWORD -> 'uint32_t'
// nDefaultTimeOut : DWORD -> 'uint32_t'
// lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
CreateNamedPipeW: { parameters: ["buffer", "u32", "u32", "u32", "u32", "u32", "u32", "pointer"], result: "pointer" },
});
// lib.symbols.CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes)
// lpName : LPCWSTR -> "buffer"
// dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> "u32"
// dwPipeMode : NAMED_PIPE_MODE -> "u32"
// nMaxInstances : DWORD -> "u32"
// nOutBufferSize : DWORD -> "u32"
// nInBufferSize : DWORD -> "u32"
// nDefaultTimeOut : DWORD -> "u32"
// lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* CreateNamedPipeW(
const uint16_t* lpName,
uint32_t dwOpenMode,
uint32_t dwPipeMode,
uint32_t nMaxInstances,
uint32_t nOutBufferSize,
uint32_t nInBufferSize,
uint32_t nDefaultTimeOut,
void* lpSecurityAttributes);
C, "KERNEL32.dll");
// $ffi->CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes);
// lpName : LPCWSTR
// dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES
// dwPipeMode : NAMED_PIPE_MODE
// nMaxInstances : DWORD
// nOutBufferSize : DWORD
// nInBufferSize : DWORD
// nDefaultTimeOut : DWORD
// lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
// 構造体/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.UNICODE_OPTIONS);
Pointer CreateNamedPipeW(
WString lpName, // LPCWSTR
int dwOpenMode, // FILE_FLAGS_AND_ATTRIBUTES
int dwPipeMode, // NAMED_PIPE_MODE
int nMaxInstances, // DWORD
int nOutBufferSize, // DWORD
int nInBufferSize, // DWORD
int nDefaultTimeOut, // DWORD
Pointer lpSecurityAttributes // SECURITY_ATTRIBUTES* optional
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("kernel32")]
lib LibKERNEL32
fun CreateNamedPipeW = CreateNamedPipeW(
lpName : UInt16*, # LPCWSTR
dwOpenMode : UInt32, # FILE_FLAGS_AND_ATTRIBUTES
dwPipeMode : UInt32, # NAMED_PIPE_MODE
nMaxInstances : UInt32, # DWORD
nOutBufferSize : UInt32, # DWORD
nInBufferSize : UInt32, # DWORD
nDefaultTimeOut : UInt32, # DWORD
lpSecurityAttributes : SECURITY_ATTRIBUTES* # SECURITY_ATTRIBUTES* optional
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CreateNamedPipeWNative = Pointer<Void> Function(Pointer<Utf16>, Uint32, Uint32, Uint32, Uint32, Uint32, Uint32, Pointer<Void>);
typedef CreateNamedPipeWDart = Pointer<Void> Function(Pointer<Utf16>, int, int, int, int, int, int, Pointer<Void>);
final CreateNamedPipeW = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<CreateNamedPipeWNative, CreateNamedPipeWDart>('CreateNamedPipeW');
// lpName : LPCWSTR -> Pointer<Utf16>
// dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> Uint32
// dwPipeMode : NAMED_PIPE_MODE -> Uint32
// nMaxInstances : DWORD -> Uint32
// nOutBufferSize : DWORD -> Uint32
// nInBufferSize : DWORD -> Uint32
// nDefaultTimeOut : DWORD -> Uint32
// lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CreateNamedPipeW(
lpName: PWideChar; // LPCWSTR
dwOpenMode: DWORD; // FILE_FLAGS_AND_ATTRIBUTES
dwPipeMode: DWORD; // NAMED_PIPE_MODE
nMaxInstances: DWORD; // DWORD
nOutBufferSize: DWORD; // DWORD
nInBufferSize: DWORD; // DWORD
nDefaultTimeOut: DWORD; // DWORD
lpSecurityAttributes: Pointer // SECURITY_ATTRIBUTES* optional
): THandle; stdcall;
external 'KERNEL32.dll' name 'CreateNamedPipeW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CreateNamedPipeW"
c_CreateNamedPipeW :: CWString -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> Ptr () -> IO (Ptr ())
-- lpName : LPCWSTR -> CWString
-- dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> Word32
-- dwPipeMode : NAMED_PIPE_MODE -> Word32
-- nMaxInstances : DWORD -> Word32
-- nOutBufferSize : DWORD -> Word32
-- nInBufferSize : DWORD -> Word32
-- nDefaultTimeOut : DWORD -> Word32
-- lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let createnamedpipew =
foreign "CreateNamedPipeW"
((ptr uint16_t) @-> uint32_t @-> uint32_t @-> uint32_t @-> uint32_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> returning (ptr void))
(* lpName : LPCWSTR -> (ptr uint16_t) *)
(* dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> uint32_t *)
(* dwPipeMode : NAMED_PIPE_MODE -> uint32_t *)
(* nMaxInstances : DWORD -> uint32_t *)
(* nOutBufferSize : DWORD -> uint32_t *)
(* nInBufferSize : DWORD -> uint32_t *)
(* nDefaultTimeOut : DWORD -> uint32_t *)
(* lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("CreateNamedPipeW" create-named-pipe-w :convention :stdcall) :pointer
(lp-name (:string :encoding :utf-16le)) ; LPCWSTR
(dw-open-mode :uint32) ; FILE_FLAGS_AND_ATTRIBUTES
(dw-pipe-mode :uint32) ; NAMED_PIPE_MODE
(n-max-instances :uint32) ; DWORD
(n-out-buffer-size :uint32) ; DWORD
(n-in-buffer-size :uint32) ; DWORD
(n-default-time-out :uint32) ; DWORD
(lp-security-attributes :pointer)) ; SECURITY_ATTRIBUTES* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CreateNamedPipeW = Win32::API::More->new('KERNEL32',
'HANDLE CreateNamedPipeW(LPCWSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPVOID lpSecurityAttributes)');
# my $ret = $CreateNamedPipeW->Call($lpName, $dwOpenMode, $dwPipeMode, $nMaxInstances, $nOutBufferSize, $nInBufferSize, $nDefaultTimeOut, $lpSecurityAttributes);
# lpName : LPCWSTR -> LPCWSTR
# dwOpenMode : FILE_FLAGS_AND_ATTRIBUTES -> DWORD
# dwPipeMode : NAMED_PIPE_MODE -> DWORD
# nMaxInstances : DWORD -> DWORD
# nOutBufferSize : DWORD -> DWORD
# nInBufferSize : DWORD -> DWORD
# nDefaultTimeOut : DWORD -> DWORD
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f CreateNamedPipeA (ANSI版) — 名前付きパイプのインスタンスを作成する(ANSI版)。