Win32 API 日本語リファレンス
ホームNetworkManagement.IpHelper › GetTeredoPort

GetTeredoPort

関数
Teredoが使用するUDPポート番号を取得する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

WIN32_ERROR GetTeredoPort(
    WORD* Port
);

パラメーター

名前方向説明
PortWORD*outUDP ポート番号へのポインター。正常に復帰した場合、このパラメーターには Teredo クライアントが使用するポート番号が格納されます。

戻り値の型: WIN32_ERROR

公式ドキュメント

ローカルコンピューター上の Teredo クライアントが使用する動的な UDP ポート番号を取得します。

戻り値

関数が成功した場合、戻り値は NO_ERROR です。

関数が失敗した場合、戻り値は次のいずれかのエラーコードになります。

戻り値 説明
ERROR_INVALID_PARAMETER
無効なパラメーターが関数に渡されました。このエラーは、Port パラメーターに NULL ポインターが渡された場合に返されます。
ERROR_NOT_READY
デバイスの準備ができていません。このエラーは、ローカルコンピューター上で Teredo クライアントが起動していない場合に返されます。
ERROR_NOT_SUPPORTED
要求はサポートされていません。このエラーは、ローカルコンピューターに IPv6 スタックが存在しない場合に返されます。
その他

FormatMessage を使用して、返されたエラーに対応するメッセージ文字列を取得してください。

解説(Remarks)

GetTeredoPort 関数は Windows Vista 以降で定義されています。

GetTeredoPort 関数は、Teredo サービスポート用に Teredo クライアントが現在使用している UDP ポート番号を取得します。Teredo ポートは動的であり、ローカルコンピューター上で Teredo クライアントが再起動されるたびに変化する可能性があります。アプリケーションは、NotifyTeredoPortChange 関数を呼び出すことで、Teredo サービスポートが変化したときに通知を受け取るよう登録できます。

Teredo クライアントは、RFC 4380 で定義されているマルチキャスト IPv4 アドレス 224.0.0.253 に送信されるマルチキャストトラフィックをリッスンするために、静的な UDP ポート 3544 も使用します。詳細については、http://www.ietf.org/rfc/rfc4380.txt を参照してください。

GetTeredoPort 関数は、主にファイアウォールアプリケーションが、Teredo トラフィックの送受信を許可するための適切な例外を構成するために使用されます。

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

各言語での呼び出し定義

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

WIN32_ERROR GetTeredoPort(
    WORD* Port
);
[DllImport("IPHLPAPI.dll", ExactSpelling = true)]
static extern uint GetTeredoPort(
    out ushort Port   // WORD* out
);
<DllImport("IPHLPAPI.dll", ExactSpelling:=True)>
Public Shared Function GetTeredoPort(
    <Out> ByRef Port As UShort   ' WORD* out
) As UInteger
End Function
' Port : WORD* out
Declare PtrSafe Function GetTeredoPort Lib "iphlpapi" ( _
    ByRef Port As Integer) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetTeredoPort = ctypes.windll.iphlpapi.GetTeredoPort
GetTeredoPort.restype = wintypes.DWORD
GetTeredoPort.argtypes = [
    ctypes.POINTER(ctypes.c_ushort),  # Port : WORD* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('IPHLPAPI.dll')
GetTeredoPort = Fiddle::Function.new(
  lib['GetTeredoPort'],
  [
    Fiddle::TYPE_VOIDP,  # Port : WORD* out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "iphlpapi")]
extern "system" {
    fn GetTeredoPort(
        Port: *mut u16  // WORD* out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("IPHLPAPI.dll")]
public static extern uint GetTeredoPort(out ushort Port);
"@
$api = Add-Type -MemberDefinition $sig -Name 'IPHLPAPI_GetTeredoPort' -Namespace Win32 -PassThru
# $api::GetTeredoPort(Port)
#uselib "IPHLPAPI.dll"
#func global GetTeredoPort "GetTeredoPort" sptr
; GetTeredoPort varptr(Port)   ; 戻り値は stat
; Port : WORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "IPHLPAPI.dll"
#cfunc global GetTeredoPort "GetTeredoPort" var
; res = GetTeredoPort(Port)
; Port : WORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; WIN32_ERROR GetTeredoPort(WORD* Port)
#uselib "IPHLPAPI.dll"
#cfunc global GetTeredoPort "GetTeredoPort" var
; res = GetTeredoPort(Port)
; Port : WORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procGetTeredoPort = iphlpapi.NewProc("GetTeredoPort")
)

// Port (WORD* out)
r1, _, err := procGetTeredoPort.Call(
	uintptr(Port),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function GetTeredoPort(
  Port: Pointer   // WORD* out
): DWORD; stdcall;
  external 'IPHLPAPI.dll' name 'GetTeredoPort';
result := DllCall("IPHLPAPI\GetTeredoPort"
    , "Ptr", Port   ; WORD* out
    , "UInt")   ; return: WIN32_ERROR
●GetTeredoPort(Port) = DLL("IPHLPAPI.dll", "dword GetTeredoPort(void*)")
# 呼び出し: GetTeredoPort(Port)
# Port : WORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "iphlpapi" fn GetTeredoPort(
    Port: [*c]u16 // WORD* out
) callconv(std.os.windows.WINAPI) u32;
proc GetTeredoPort(
    Port: ptr uint16  # WORD* out
): uint32 {.importc: "GetTeredoPort", stdcall, dynlib: "IPHLPAPI.dll".}
pragma(lib, "iphlpapi");
extern(Windows)
uint GetTeredoPort(
    ushort* Port   // WORD* out
);
ccall((:GetTeredoPort, "IPHLPAPI.dll"), stdcall, UInt32,
      (Ptr{UInt16},),
      Port)
# Port : WORD* out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetTeredoPort(
    uint16_t* Port);
]]
local iphlpapi = ffi.load("iphlpapi")
-- iphlpapi.GetTeredoPort(Port)
-- Port : WORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('IPHLPAPI.dll');
const GetTeredoPort = lib.func('__stdcall', 'GetTeredoPort', 'uint32_t', ['uint16_t *']);
// GetTeredoPort(Port)
// Port : WORD* out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("IPHLPAPI.dll", {
  GetTeredoPort: { parameters: ["pointer"], result: "u32" },
});
// lib.symbols.GetTeredoPort(Port)
// Port : WORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetTeredoPort(
    uint16_t* Port);
C, "IPHLPAPI.dll");
// $ffi->GetTeredoPort(Port);
// Port : WORD* 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 Iphlpapi extends StdCallLibrary {
    Iphlpapi INSTANCE = Native.load("iphlpapi", Iphlpapi.class);
    int GetTeredoPort(
        ShortByReference Port   // WORD* out
    );
}
@[Link("iphlpapi")]
lib LibIPHLPAPI
  fun GetTeredoPort = GetTeredoPort(
    Port : UInt16*   # WORD* out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetTeredoPortNative = Uint32 Function(Pointer<Uint16>);
typedef GetTeredoPortDart = int Function(Pointer<Uint16>);
final GetTeredoPort = DynamicLibrary.open('IPHLPAPI.dll')
    .lookupFunction<GetTeredoPortNative, GetTeredoPortDart>('GetTeredoPort');
// Port : WORD* out -> Pointer<Uint16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetTeredoPort(
  Port: Pointer   // WORD* out
): DWORD; stdcall;
  external 'IPHLPAPI.dll' name 'GetTeredoPort';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetTeredoPort"
  c_GetTeredoPort :: Ptr Word16 -> IO Word32
-- Port : WORD* out -> Ptr Word16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getteredoport =
  foreign "GetTeredoPort"
    ((ptr uint16_t) @-> returning uint32_t)
(* Port : WORD* out -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library iphlpapi (t "IPHLPAPI.dll"))
(cffi:use-foreign-library iphlpapi)

(cffi:defcfun ("GetTeredoPort" get-teredo-port :convention :stdcall) :uint32
  (port :pointer))   ; WORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetTeredoPort = Win32::API::More->new('IPHLPAPI',
    'DWORD GetTeredoPort(LPVOID Port)');
# my $ret = $GetTeredoPort->Call($Port);
# Port : WORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型