ホーム › Graphics.Printing › AddPortW
AddPortW
関数サーバーに新しいプリンターポートを追加する。
シグネチャ
// winspool.drv (Unicode / -W)
#include <windows.h>
BOOL AddPortW(
LPCWSTR pName, // optional
HWND hWnd,
LPCWSTR pMonitorName
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pName | LPCWSTR | inoptional | ポートを追加するサーバー名を指すポインタ。ローカルならNULLを指定する。 |
| hWnd | HWND | in | 設定ダイアログの親ウィンドウのハンドル。 |
| pMonitorName | LPCWSTR | in | ポートを管理するポートモニターの名前を指すポインタ。 |
戻り値の型: BOOL
各言語での呼び出し定義
// winspool.drv (Unicode / -W)
#include <windows.h>
BOOL AddPortW(
LPCWSTR pName, // optional
HWND hWnd,
LPCWSTR pMonitorName
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("winspool.drv", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool AddPortW(
[MarshalAs(UnmanagedType.LPWStr)] string pName, // LPCWSTR optional
IntPtr hWnd, // HWND
[MarshalAs(UnmanagedType.LPWStr)] string pMonitorName // LPCWSTR
);<DllImport("winspool.drv", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function AddPortW(
<MarshalAs(UnmanagedType.LPWStr)> pName As String, ' LPCWSTR optional
hWnd As IntPtr, ' HWND
<MarshalAs(UnmanagedType.LPWStr)> pMonitorName As String ' LPCWSTR
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' pName : LPCWSTR optional
' hWnd : HWND
' pMonitorName : LPCWSTR
Declare PtrSafe Function AddPortW Lib "winspool.drv" ( _
ByVal pName As LongPtr, _
ByVal hWnd As LongPtr, _
ByVal pMonitorName 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
AddPortW = ctypes.windll.LoadLibrary("winspool.drv").AddPortW
AddPortW.restype = wintypes.BOOL
AddPortW.argtypes = [
wintypes.LPCWSTR, # pName : LPCWSTR optional
wintypes.HANDLE, # hWnd : HWND
wintypes.LPCWSTR, # pMonitorName : LPCWSTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('winspool.drv')
AddPortW = Fiddle::Function.new(
lib['AddPortW'],
[
Fiddle::TYPE_VOIDP, # pName : LPCWSTR optional
Fiddle::TYPE_VOIDP, # hWnd : HWND
Fiddle::TYPE_VOIDP, # pMonitorName : LPCWSTR
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "winspool.drv")]
extern "system" {
fn AddPortW(
pName: *const u16, // LPCWSTR optional
hWnd: *mut core::ffi::c_void, // HWND
pMonitorName: *const u16 // LPCWSTR
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("winspool.drv", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool AddPortW([MarshalAs(UnmanagedType.LPWStr)] string pName, IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] string pMonitorName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'winspool.drv_AddPortW' -Namespace Win32 -PassThru
# $api::AddPortW(pName, hWnd, pMonitorName)#uselib "winspool.drv"
#func global AddPortW "AddPortW" wptr, wptr, wptr
; AddPortW pName, hWnd, pMonitorName ; 戻り値は stat
; pName : LPCWSTR optional -> "wptr"
; hWnd : HWND -> "wptr"
; pMonitorName : LPCWSTR -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "winspool.drv"
#cfunc global AddPortW "AddPortW" wstr, sptr, wstr
; res = AddPortW(pName, hWnd, pMonitorName)
; pName : LPCWSTR optional -> "wstr"
; hWnd : HWND -> "sptr"
; pMonitorName : LPCWSTR -> "wstr"; BOOL AddPortW(LPCWSTR pName, HWND hWnd, LPCWSTR pMonitorName)
#uselib "winspool.drv"
#cfunc global AddPortW "AddPortW" wstr, intptr, wstr
; res = AddPortW(pName, hWnd, pMonitorName)
; pName : LPCWSTR optional -> "wstr"
; hWnd : HWND -> "intptr"
; pMonitorName : LPCWSTR -> "wstr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
winspool_drv = windows.NewLazySystemDLL("winspool.drv")
procAddPortW = winspool_drv.NewProc("AddPortW")
)
// pName (LPCWSTR optional), hWnd (HWND), pMonitorName (LPCWSTR)
r1, _, err := procAddPortW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pName))),
uintptr(hWnd),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pMonitorName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction AddPortW(
pName: PWideChar; // LPCWSTR optional
hWnd: THandle; // HWND
pMonitorName: PWideChar // LPCWSTR
): BOOL; stdcall;
external 'winspool.drv' name 'AddPortW';result := DllCall("winspool.drv\AddPortW"
, "WStr", pName ; LPCWSTR optional
, "Ptr", hWnd ; HWND
, "WStr", pMonitorName ; LPCWSTR
, "Int") ; return: BOOL●AddPortW(pName, hWnd, pMonitorName) = DLL("winspool.drv", "bool AddPortW(char*, void*, char*)")
# 呼び出し: AddPortW(pName, hWnd, pMonitorName)
# pName : LPCWSTR optional -> "char*"
# hWnd : HWND -> "void*"
# pMonitorName : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "winspool.drv" fn AddPortW(
pName: [*c]const u16, // LPCWSTR optional
hWnd: ?*anyopaque, // HWND
pMonitorName: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc AddPortW(
pName: WideCString, # LPCWSTR optional
hWnd: pointer, # HWND
pMonitorName: WideCString # LPCWSTR
): int32 {.importc: "AddPortW", stdcall, dynlib: "winspool.drv".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "winspool.drv");
extern(Windows)
int AddPortW(
const(wchar)* pName, // LPCWSTR optional
void* hWnd, // HWND
const(wchar)* pMonitorName // LPCWSTR
);ccall((:AddPortW, "winspool.drv"), stdcall, Int32,
(Cwstring, Ptr{Cvoid}, Cwstring),
pName, hWnd, pMonitorName)
# pName : LPCWSTR optional -> Cwstring
# hWnd : HWND -> Ptr{Cvoid}
# pMonitorName : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t AddPortW(
const uint16_t* pName,
void* hWnd,
const uint16_t* pMonitorName);
]]
local winspool.drv = ffi.load("winspool.drv")
-- winspool.drv.AddPortW(pName, hWnd, pMonitorName)
-- pName : LPCWSTR optional
-- hWnd : HWND
-- pMonitorName : LPCWSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('winspool.drv');
const AddPortW = lib.func('__stdcall', 'AddPortW', 'int32_t', ['str16', 'void *', 'str16']);
// AddPortW(pName, hWnd, pMonitorName)
// pName : LPCWSTR optional -> 'str16'
// hWnd : HWND -> 'void *'
// pMonitorName : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("winspool.drv", {
AddPortW: { parameters: ["buffer", "pointer", "buffer"], result: "i32" },
});
// lib.symbols.AddPortW(pName, hWnd, pMonitorName)
// pName : LPCWSTR optional -> "buffer"
// hWnd : HWND -> "pointer"
// pMonitorName : LPCWSTR -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t AddPortW(
const uint16_t* pName,
void* hWnd,
const uint16_t* pMonitorName);
C, "winspool.drv");
// $ffi->AddPortW(pName, hWnd, pMonitorName);
// pName : LPCWSTR optional
// hWnd : HWND
// pMonitorName : LPCWSTR
// 構造体/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 Winspool.drv extends StdCallLibrary {
Winspool.drv INSTANCE = Native.load("winspool.drv", Winspool.drv.class, W32APIOptions.UNICODE_OPTIONS);
boolean AddPortW(
WString pName, // LPCWSTR optional
Pointer hWnd, // HWND
WString pMonitorName // LPCWSTR
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("winspool.drv")]
lib Libwinspool.drv
fun AddPortW = AddPortW(
pName : UInt16*, # LPCWSTR optional
hWnd : Void*, # HWND
pMonitorName : UInt16* # LPCWSTR
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef AddPortWNative = Int32 Function(Pointer<Utf16>, Pointer<Void>, Pointer<Utf16>);
typedef AddPortWDart = int Function(Pointer<Utf16>, Pointer<Void>, Pointer<Utf16>);
final AddPortW = DynamicLibrary.open('winspool.drv')
.lookupFunction<AddPortWNative, AddPortWDart>('AddPortW');
// pName : LPCWSTR optional -> Pointer<Utf16>
// hWnd : HWND -> Pointer<Void>
// pMonitorName : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function AddPortW(
pName: PWideChar; // LPCWSTR optional
hWnd: THandle; // HWND
pMonitorName: PWideChar // LPCWSTR
): BOOL; stdcall;
external 'winspool.drv' name 'AddPortW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "AddPortW"
c_AddPortW :: CWString -> Ptr () -> CWString -> IO CInt
-- pName : LPCWSTR optional -> CWString
-- hWnd : HWND -> Ptr ()
-- pMonitorName : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let addportw =
foreign "AddPortW"
((ptr uint16_t) @-> (ptr void) @-> (ptr uint16_t) @-> returning int32_t)
(* pName : LPCWSTR optional -> (ptr uint16_t) *)
(* hWnd : HWND -> (ptr void) *)
(* pMonitorName : LPCWSTR -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library winspool.drv (t "winspool.drv"))
(cffi:use-foreign-library winspool.drv)
(cffi:defcfun ("AddPortW" add-port-w :convention :stdcall) :int32
(p-name (:string :encoding :utf-16le)) ; LPCWSTR optional
(h-wnd :pointer) ; HWND
(p-monitor-name (:string :encoding :utf-16le))) ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $AddPortW = Win32::API::More->new('winspool.drv',
'BOOL AddPortW(LPCWSTR pName, HANDLE hWnd, LPCWSTR pMonitorName)');
# my $ret = $AddPortW->Call($pName, $hWnd, $pMonitorName);
# pName : LPCWSTR optional -> LPCWSTR
# hWnd : HWND -> HANDLE
# pMonitorName : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f AddPortA (ANSI版) — サーバーに新しいプリンターポートを追加する。