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 | 一意のパイプ名。この文字列は次の形式である必要があります。 \\.\pipe\pipename 名前のうち pipename の部分には、バックスラッシュ以外の任意の文字を含めることができ、数字や特殊文字も使用できます。パイプ名の文字列全体は最大 256 文字です。パイプ名は大文字と小文字を区別しません。 | ||||||||||||||||||||||||
| dwOpenMode | FILE_FLAGS_AND_ATTRIBUTES | in | オープンモード。 dwOpenMode に 0 または次の表に示すフラグ以外を指定した場合、この関数は失敗します。 このパラメーターには、次のパイプアクセスモードのいずれかを指定する必要があります。パイプの各インスタンスには同じモードを指定する必要があります。
このパラメーターには、ライトスルーモードおよびオーバーラップモードを有効にする次のフラグを 1 つ以上含めることもできます。これらのモードは、同じパイプであってもインスタンスごとに異なっていてかまいません。
このパラメーターには、次のセキュリティアクセスモードを任意に組み合わせて含めることができます。これらのモードは、同じパイプであってもインスタンスごとに異なっていてかまいません。
| ||||||||||||||||||||||||
| dwPipeMode | NAMED_PIPE_MODE | in | パイプモード。 dwPipeMode に 0 または次の表に示すフラグ以外を指定した場合、この関数は失敗します。 次のタイプモードのいずれかを指定できます。パイプの各インスタンスには同じタイプモードを指定する必要があります。
次の読み取りモードのいずれかを指定できます。同じパイプであってもインスタンスごとに異なる読み取りモードを指定できます。
次の待機モードのいずれかを指定できます。同じパイプであってもインスタンスごとに異なる待機モードを指定できます。
次のリモートクライアントモードのいずれかを指定できます。同じパイプであってもインスタンスごとに異なるリモートクライアントモードを指定できます。
| ||||||||||||||||||||||||
| nMaxInstances | DWORD | in | このパイプに対して作成できるインスタンスの最大数。パイプの最初のインスタンスでこの値を指定できます。パイプの他のインスタンスにも同じ数を指定する必要があります。指定できる値は 1 から PIPE_UNLIMITED_INSTANCES (255) までの範囲です。 このパラメーターが PIPE_UNLIMITED_INSTANCES の場合、作成できるパイプインスタンスの数は、システムリソースの空き状況によってのみ制限されます。nMaxInstances が PIPE_UNLIMITED_INSTANCES より大きい場合、戻り値は INVALID_HANDLE_VALUE となり、GetLastError は ERROR_INVALID_PARAMETER を返します。 | ||||||||||||||||||||||||
| nOutBufferSize | DWORD | in | 出力バッファー用に予約するバイト数。名前付きパイプのバッファーサイズの決め方については、後述の「解説」セクションを参照してください。 | ||||||||||||||||||||||||
| nInBufferSize | DWORD | in | 入力バッファー用に予約するバイト数。名前付きパイプのバッファーサイズの決め方については、後述の「解説」セクションを参照してください。 | ||||||||||||||||||||||||
| nDefaultTimeOut | DWORD | in | 既定のタイムアウト値 (ミリ秒単位)。 WaitNamedPipe 関数が NMPWAIT_USE_DEFAULT_WAIT を指定した場合に使用されます。名前付きパイプの各インスタンスには同じ値を指定する必要があります。 値を 0 にすると、既定のタイムアウトは 50 ミリ秒になります。 | ||||||||||||||||||||||||
| lpSecurityAttributes | SECURITY_ATTRIBUTES* | inoptional | 新しい名前付きパイプのセキュリティ記述子を指定し、返されるハンドルを子プロセスが継承できるかどうかを決定する SECURITY_ATTRIBUTES 構造体へのポインター。lpSecurityAttributes が NULL の場合、名前付きパイプには既定のセキュリティ記述子が設定され、ハンドルは継承できません。名前付きパイプの既定のセキュリティ記述子に含まれる ACL は、LocalSystem アカウント、Administrators、および作成者所有者にフルコントロールを許可します。また、Everyone グループのメンバーと匿名アカウントには読み取りアクセスを許可します。 |
戻り値の型: HANDLE
公式ドキュメント
CreateNamedPipeW (Unicode) 関数 (winbase.h) は、名前付きパイプのインスタンスを作成し、以降のパイプ操作で使用するハンドルを返します。
戻り値
関数が成功した場合、戻り値は名前付きパイプインスタンスのサーバー側のハンドルです。
関数が失敗した場合、戻り値は INVALID_HANDLE_VALUE です。拡張エラー情報を取得するには、GetLastError を呼び出します。
解説(Remarks)
CreateNamedPipe を使用して名前付きパイプのインスタンスを作成するには、ユーザーが名前付きパイプオブジェクトに対する FILE_CREATE_PIPE_INSTANCE アクセス権を持っている必要があります。新しい名前付きパイプを作成する場合、セキュリティ属性パラメーターのアクセス制御リスト (ACL) が、その名前付きパイプに対する任意アクセス制御を定義します。
名前付きパイプのすべてのインスタンスには、同じパイプの種類 (バイト型またはメッセージ型)、パイプアクセス (双方向、受信、または送信)、インスタンス数、およびタイムアウト値を指定する必要があります。異なる値を使用した場合、この関数は失敗し、GetLastError は ERROR_ACCESS_DENIED を返します。
クライアントプロセスは、CreateFile 関数または CallNamedPipe 関数を使用して名前付きパイプに接続します。名前付きパイプのクライアント側は、サーバー側がメッセージモードであっても、最初はバイトモードで開始されます。データ受信時の問題を避けるため、クライアント側もメッセージモードに設定してください。パイプのモードを変更するには、パイプクライアントは GENERIC_READ および FILE_WRITE_ATTRIBUTES アクセスで読み取り専用パイプを開く必要があります。
パイプサーバーは、パイプクライアントが開始するまでブロッキング読み取り操作を実行すべきではありません。そうしないと、競合状態が発生する可能性があります。これは通常、C ランタイムなどの初期化コードが、継承されたハンドルをロックして検査する必要がある場合に発生します。
名前付きパイプが作成されるたびに、システムは、カーネルが使用する物理メモリである非ページプールを使用して、受信バッファーと送信バッファーの一方または両方を作成します。作成できるパイプインスタンスの数 (およびスレッドやプロセスなどのオブジェクトの数) は、利用可能な非ページプールによって制限されます。読み取り要求または書き込み要求ごとに、読み取りデータまたは書き込みデータ用のバッファー領域に加えて、内部データ構造用の追加領域が必要になります。
入力バッファーと出力バッファーのサイズはあくまで目安です。名前付きパイプの各端に対して実際に予約されるバッファーサイズは、システムの既定値、システムの最小値または最大値、あるいは指定したサイズを次の割り当て境界に切り上げた値のいずれかになります。指定するバッファーサイズは、プロセスが非ページプールを使い果たさない程度に小さく、かつ一般的な要求に対応できる程度に大きくする必要があります。
パイプへの書き込み操作が行われるたびに、システムはまずパイプ書き込みクォータに対してメモリを課金しようとします。残りのパイプ書き込みクォータが要求を満たすのに十分であれば、書き込み操作はすぐに完了します。残りのパイプ書き込みクォータが要求を満たすには小さすぎる場合、システムは、プロセス用に予約された非ページプールを使用して、データを収めるためにバッファーの拡張を試みます。書き込み操作は、追加のバッファークォータを解放できるようにパイプからデータが読み取られるまでブロックされます。したがって、指定したバッファーサイズが小さすぎる場合、システムは必要に応じてバッファーを拡張しますが、その代わりに操作がブロックされるという欠点があります。操作がオーバーラップ操作である場合はシステムスレッドがブロックされ、そうでない場合はアプリケーションのスレッドがブロックされます。
名前付きパイプが使用しているリソースを解放するため、アプリケーションはハンドルが不要になったら常に閉じる必要があります。これは、CloseHandle 関数を呼び出すか、インスタンスのハンドルに関連付けられたプロセスが終了することで行われます。名前付きパイプのインスタンスには、複数のハンドルが関連付けられている場合があることに注意してください。名前付きパイプのインスタンスは、そのインスタンスに対する最後のハンドルが閉じられた時点で必ず削除されます。
Windows 10 バージョン 1709: パイプはアプリコンテナー内でのみサポートされます。つまり、同じアプリの一部である UWP プロセスから別の UWP プロセスへの通信でのみ使用できます。また、名前付きパイプのパイプ名には \\.\pipe\LOCAL\ という構文を使用する必要があります。
例
例については、マルチスレッドパイプサーバー を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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版)。
- f ConnectNamedPipe — 名前付きパイプサーバーがクライアント接続を待ち受ける。
- f CreateFileW — ファイルやデバイスを作成または開いてハンドルを返す。
- f ReadFile — ファイルやデバイスからデータを読み込む。
- f ReadFileEx — 完了ルーチンを使い非同期でファイルを読み込む。
- f TransactNamedPipe — 名前付きパイプへの書き込みと読み取りを一括実行する。
- f WaitNamedPipeW — 名前付きパイプが利用可能になるまで待機する。
- f WriteFile — ファイルやデバイスにデータを書き込む。
- f WriteFileEx — 完了ルーチンを使い非同期でファイルに書き込む。