Win32 API 日本語リファレンス
ホーム › Networking.ActiveDirectory › DsMakeSpnW

DsMakeSpnW

関数
構成要素からサービスプリンシパル名を生成する(Unicode版)。
DLLDSPARSE.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

// DSPARSE.dll  (Unicode / -W)
#include <windows.h>

DWORD DsMakeSpnW(
    LPCWSTR ServiceClass,
    LPCWSTR ServiceName,
    LPCWSTR InstanceName,   // optional
    WORD InstancePort,
    LPCWSTR Referrer,   // optional
    DWORD* pcSpnLength,
    LPWSTR pszSpn   // optional
);

パラメーター

名前型方向説明
ServiceClassLPCWSTRinサービスのクラスを指定する、null で終わる定数文字列へのポインター。このパラメーターには、そのサービスに固有の任意の文字列を指定できます。プロトコル名 (たとえば ldap) や、GUID の文字列形式などを使用できます。
ServiceNameLPCWSTRin

DNS 名、NetBIOS 名、または識別名 (DN) を指定する、null で終わる定数文字列へのポインター。このパラメーターは NULL 以外である必要があります。

ServiceName、InstanceName、InstancePort の各パラメーターを使用して SPN を構成する方法の詳細については、後述の「解説」セクションを参照してください。

InstanceNameLPCWSTRinoptional

サービスのインスタンスをホストするコンピューターの DNS 名または IP アドレスを指定する、null で終わる定数文字列へのポインター。

ServiceName がサービスのホストコンピューターの DNS 名または NetBIOS 名を指定している場合、InstanceName パラメーターは NULL である必要があります。

ServiceName が DNS ドメイン名、DNS SRV レコードの名前、またはサービス接続ポイントの DN のような識別名を指定している場合、InstanceName パラメーターにはサービスのホストコンピューターの DNS 名または NetBIOS 名を指定する必要があります。

InstancePortWORDinサービスのインスタンスのポート番号。既定のポートを使用する場合は 0 を指定します。このパラメーターが 0 の場合、SPN にポート番号は含まれません。
ReferrerLPCWSTRinoptionalIP アドレスの参照 (リファーラル) を提供したホストの DNS 名を指定する、null で終わる定数文字列へのポインター。ServiceName パラメーターが IP アドレスを指定している場合を除き、このパラメーターは無視されます。
pcSpnLengthDWORD*inout

構築された新しい SPN を受け取るバッファーの長さ (文字数) を格納する変数へのポインター。最終的に必要なバッファーサイズを事前に取得するために、この値を 0 にすることもできます。

pcSpnLength パラメーターは、終端の null 文字を含む、作成された SPN の実際の長さも受け取ります。

pszSpnLPWSTRoutoptional構築された SPN を受け取る、null で終わる文字列へのポインター。このバッファーは pcSpnLength で指定された長さである必要があります。最終的に必要なバッファーサイズを事前に取得するために、pszSpn パラメーターを NULL にすることもできます。

戻り値の型: DWORD

公式ドキュメント

サービスインスタンスを識別するサービスプリンシパル名 (SPN) を構築します。(Unicode)

戻り値

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

解説(Remarks)

DsMakeSpn 関数が生成する SPN の形式は、入力パラメーターによって異なります。基本的な形式は 2 つあります。どちらの形式も、ServiceClass 文字列で始まり、その後にホストコンピューター名と、省略可能な InstancePort コンポーネントが続きます。

メモ この形式はホストベースのサービスで使用されます。

"<ServiceClass>/<host>" 形式の SPN を生成するには

  1. ServiceName パラメーターに、サービスインスタンスのホストコンピューターの DNS 名を設定します。これが SPN のホストコンポーネントになります。
  2. InstanceName パラメーターと Referrer パラメーターを NULL に設定します。
  3. InstancePort パラメーターを 0 に設定します。InstancePort が 0 以外の場合、SPN は次の形式になります。
    <service class>/<host>:<instance port>/<referrer>
    
メモ この形式はレプリケーション可能なサービスで使用されます。

"<ServiceClass>/<host>:<InstancePort>" 形式の SPN を生成するには

  1. InstanceName パラメーターに、サービスインスタンスのホストコンピューターの DNS 名を設定します。これがホストコンポーネントになります。
  2. ServiceName パラメーターに、サービスのインスタンスを識別する文字列を設定します。たとえば、そのサービスインスタンスのサービス接続ポイントの識別名などを指定できます。
  3. Referrer パラメーターを NULL に設定します。
  4. InstancePort パラメーターを 0 に設定します。InstancePort が 0 以外の場合、SPN は次の形式になります。
    <service class>/<host>:<instance port>/<service name>
    
Referrer パラメーターは、ServiceName パラメーターがサービスのホストコンピューターの IP アドレスを指定している場合にのみ使用されます。この場合、Referrer には、その IP アドレスを参照 (リファーラル) として提供したコンピューターの DNS 名を指定します。SPN は次の形式になります。
<service class>/<host>:<instance port>/<referrer>

ここで、ホストコンポーネントは InstanceName 文字列 (InstanceName が NULL の場合は ServiceName 文字列) であり、InstancePort コンポーネントは省略可能です。

スラッシュ (/) 文字は SPN の各コンポーネントを区切るために使用されるため、文字列パラメーターにこの文字を含めることはできません。

メモ

dsparse.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして、DsMakeSpn を定義しています。エンコーディング中立のエイリアスと、エンコーディング中立でないコードを混在させると、不一致が生じ、コンパイルエラーや実行時エラーの原因になることがあります。詳細については、関数プロトタイプの表記規則を参照してください。

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

各言語での呼び出し定義

// DSPARSE.dll  (Unicode / -W)
#include <windows.h>

DWORD DsMakeSpnW(
    LPCWSTR ServiceClass,
    LPCWSTR ServiceName,
    LPCWSTR InstanceName,   // optional
    WORD InstancePort,
    LPCWSTR Referrer,   // optional
    DWORD* pcSpnLength,
    LPWSTR pszSpn   // optional
);
[DllImport("DSPARSE.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint DsMakeSpnW(
    [MarshalAs(UnmanagedType.LPWStr)] string ServiceClass,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string ServiceName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string InstanceName,   // LPCWSTR optional
    ushort InstancePort,   // WORD
    [MarshalAs(UnmanagedType.LPWStr)] string Referrer,   // LPCWSTR optional
    ref uint pcSpnLength,   // DWORD* in/out
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszSpn   // LPWSTR optional, out
);
<DllImport("DSPARSE.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DsMakeSpnW(
    <MarshalAs(UnmanagedType.LPWStr)> ServiceClass As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> ServiceName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> InstanceName As String,   ' LPCWSTR optional
    InstancePort As UShort,   ' WORD
    <MarshalAs(UnmanagedType.LPWStr)> Referrer As String,   ' LPCWSTR optional
    ByRef pcSpnLength As UInteger,   ' DWORD* in/out
    <MarshalAs(UnmanagedType.LPWStr)> pszSpn As System.Text.StringBuilder   ' LPWSTR optional, out
) As UInteger
End Function
' ServiceClass : LPCWSTR
' ServiceName : LPCWSTR
' InstanceName : LPCWSTR optional
' InstancePort : WORD
' Referrer : LPCWSTR optional
' pcSpnLength : DWORD* in/out
' pszSpn : LPWSTR optional, out
Declare PtrSafe Function DsMakeSpnW Lib "dsparse" ( _
    ByVal ServiceClass As LongPtr, _
    ByVal ServiceName As LongPtr, _
    ByVal InstanceName As LongPtr, _
    ByVal InstancePort As Integer, _
    ByVal Referrer As LongPtr, _
    ByRef pcSpnLength As Long, _
    ByVal pszSpn 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

DsMakeSpnW = ctypes.windll.dsparse.DsMakeSpnW
DsMakeSpnW.restype = wintypes.DWORD
DsMakeSpnW.argtypes = [
    wintypes.LPCWSTR,  # ServiceClass : LPCWSTR
    wintypes.LPCWSTR,  # ServiceName : LPCWSTR
    wintypes.LPCWSTR,  # InstanceName : LPCWSTR optional
    ctypes.c_ushort,  # InstancePort : WORD
    wintypes.LPCWSTR,  # Referrer : LPCWSTR optional
    ctypes.POINTER(wintypes.DWORD),  # pcSpnLength : DWORD* in/out
    wintypes.LPWSTR,  # pszSpn : LPWSTR optional, out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('DSPARSE.dll')
DsMakeSpnW = Fiddle::Function.new(
  lib['DsMakeSpnW'],
  [
    Fiddle::TYPE_VOIDP,  # ServiceClass : LPCWSTR
    Fiddle::TYPE_VOIDP,  # ServiceName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # InstanceName : LPCWSTR optional
    -Fiddle::TYPE_SHORT,  # InstancePort : WORD
    Fiddle::TYPE_VOIDP,  # Referrer : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # pcSpnLength : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # pszSpn : LPWSTR optional, out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "dsparse")]
extern "system" {
    fn DsMakeSpnW(
        ServiceClass: *const u16,  // LPCWSTR
        ServiceName: *const u16,  // LPCWSTR
        InstanceName: *const u16,  // LPCWSTR optional
        InstancePort: u16,  // WORD
        Referrer: *const u16,  // LPCWSTR optional
        pcSpnLength: *mut u32,  // DWORD* in/out
        pszSpn: *mut u16  // LPWSTR optional, out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("DSPARSE.dll", CharSet = CharSet.Unicode)]
public static extern uint DsMakeSpnW([MarshalAs(UnmanagedType.LPWStr)] string ServiceClass, [MarshalAs(UnmanagedType.LPWStr)] string ServiceName, [MarshalAs(UnmanagedType.LPWStr)] string InstanceName, ushort InstancePort, [MarshalAs(UnmanagedType.LPWStr)] string Referrer, ref uint pcSpnLength, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszSpn);
"@
$api = Add-Type -MemberDefinition $sig -Name 'DSPARSE_DsMakeSpnW' -Namespace Win32 -PassThru
# $api::DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn)
#uselib "DSPARSE.dll"
#func global DsMakeSpnW "DsMakeSpnW" wptr, wptr, wptr, wptr, wptr, wptr, wptr
; DsMakeSpnW ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, varptr(pcSpnLength), varptr(pszSpn)   ; 戻り値は stat
; ServiceClass : LPCWSTR -> "wptr"
; ServiceName : LPCWSTR -> "wptr"
; InstanceName : LPCWSTR optional -> "wptr"
; InstancePort : WORD -> "wptr"
; Referrer : LPCWSTR optional -> "wptr"
; pcSpnLength : DWORD* in/out -> "wptr"
; pszSpn : LPWSTR optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "DSPARSE.dll"
#cfunc global DsMakeSpnW "DsMakeSpnW" wstr, wstr, wstr, int, wstr, var, var
; res = DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn)
; ServiceClass : LPCWSTR -> "wstr"
; ServiceName : LPCWSTR -> "wstr"
; InstanceName : LPCWSTR optional -> "wstr"
; InstancePort : WORD -> "int"
; Referrer : LPCWSTR optional -> "wstr"
; pcSpnLength : DWORD* in/out -> "var"
; pszSpn : LPWSTR optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD DsMakeSpnW(LPCWSTR ServiceClass, LPCWSTR ServiceName, LPCWSTR InstanceName, WORD InstancePort, LPCWSTR Referrer, DWORD* pcSpnLength, LPWSTR pszSpn)
#uselib "DSPARSE.dll"
#cfunc global DsMakeSpnW "DsMakeSpnW" wstr, wstr, wstr, int, wstr, var, var
; res = DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn)
; ServiceClass : LPCWSTR -> "wstr"
; ServiceName : LPCWSTR -> "wstr"
; InstanceName : LPCWSTR optional -> "wstr"
; InstancePort : WORD -> "int"
; Referrer : LPCWSTR optional -> "wstr"
; pcSpnLength : DWORD* in/out -> "var"
; pszSpn : LPWSTR optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	dsparse = windows.NewLazySystemDLL("DSPARSE.dll")
	procDsMakeSpnW = dsparse.NewProc("DsMakeSpnW")
)

// ServiceClass (LPCWSTR), ServiceName (LPCWSTR), InstanceName (LPCWSTR optional), InstancePort (WORD), Referrer (LPCWSTR optional), pcSpnLength (DWORD* in/out), pszSpn (LPWSTR optional, out)
r1, _, err := procDsMakeSpnW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(ServiceClass))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(ServiceName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(InstanceName))),
	uintptr(InstancePort),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(Referrer))),
	uintptr(pcSpnLength),
	uintptr(pszSpn),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function DsMakeSpnW(
  ServiceClass: PWideChar;   // LPCWSTR
  ServiceName: PWideChar;   // LPCWSTR
  InstanceName: PWideChar;   // LPCWSTR optional
  InstancePort: Word;   // WORD
  Referrer: PWideChar;   // LPCWSTR optional
  pcSpnLength: Pointer;   // DWORD* in/out
  pszSpn: PWideChar   // LPWSTR optional, out
): DWORD; stdcall;
  external 'DSPARSE.dll' name 'DsMakeSpnW';
result := DllCall("DSPARSE\DsMakeSpnW"
    , "WStr", ServiceClass   ; LPCWSTR
    , "WStr", ServiceName   ; LPCWSTR
    , "WStr", InstanceName   ; LPCWSTR optional
    , "UShort", InstancePort   ; WORD
    , "WStr", Referrer   ; LPCWSTR optional
    , "Ptr", pcSpnLength   ; DWORD* in/out
    , "Ptr", pszSpn   ; LPWSTR optional, out
    , "UInt")   ; return: DWORD
●DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn) = DLL("DSPARSE.dll", "dword DsMakeSpnW(char*, char*, char*, int, char*, void*, char*)")
# 呼び出し: DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn)
# ServiceClass : LPCWSTR -> "char*"
# ServiceName : LPCWSTR -> "char*"
# InstanceName : LPCWSTR optional -> "char*"
# InstancePort : WORD -> "int"
# Referrer : LPCWSTR optional -> "char*"
# pcSpnLength : DWORD* in/out -> "void*"
# pszSpn : LPWSTR optional, out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "dsparse" fn DsMakeSpnW(
    ServiceClass: [*c]const u16, // LPCWSTR
    ServiceName: [*c]const u16, // LPCWSTR
    InstanceName: [*c]const u16, // LPCWSTR optional
    InstancePort: u16, // WORD
    Referrer: [*c]const u16, // LPCWSTR optional
    pcSpnLength: [*c]u32, // DWORD* in/out
    pszSpn: [*c]u16 // LPWSTR optional, out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc DsMakeSpnW(
    ServiceClass: WideCString,  # LPCWSTR
    ServiceName: WideCString,  # LPCWSTR
    InstanceName: WideCString,  # LPCWSTR optional
    InstancePort: uint16,  # WORD
    Referrer: WideCString,  # LPCWSTR optional
    pcSpnLength: ptr uint32,  # DWORD* in/out
    pszSpn: ptr uint16  # LPWSTR optional, out
): uint32 {.importc: "DsMakeSpnW", stdcall, dynlib: "DSPARSE.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "dsparse");
extern(Windows)
uint DsMakeSpnW(
    const(wchar)* ServiceClass,   // LPCWSTR
    const(wchar)* ServiceName,   // LPCWSTR
    const(wchar)* InstanceName,   // LPCWSTR optional
    ushort InstancePort,   // WORD
    const(wchar)* Referrer,   // LPCWSTR optional
    uint* pcSpnLength,   // DWORD* in/out
    wchar* pszSpn   // LPWSTR optional, out
);
ccall((:DsMakeSpnW, "DSPARSE.dll"), stdcall, UInt32,
      (Cwstring, Cwstring, Cwstring, UInt16, Cwstring, Ptr{UInt32}, Ptr{UInt16}),
      ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn)
# ServiceClass : LPCWSTR -> Cwstring
# ServiceName : LPCWSTR -> Cwstring
# InstanceName : LPCWSTR optional -> Cwstring
# InstancePort : WORD -> UInt16
# Referrer : LPCWSTR optional -> Cwstring
# pcSpnLength : DWORD* in/out -> Ptr{UInt32}
# pszSpn : LPWSTR optional, out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t DsMakeSpnW(
    const uint16_t* ServiceClass,
    const uint16_t* ServiceName,
    const uint16_t* InstanceName,
    uint16_t InstancePort,
    const uint16_t* Referrer,
    uint32_t* pcSpnLength,
    uint16_t* pszSpn);
]]
local dsparse = ffi.load("dsparse")
-- dsparse.DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn)
-- ServiceClass : LPCWSTR
-- ServiceName : LPCWSTR
-- InstanceName : LPCWSTR optional
-- InstancePort : WORD
-- Referrer : LPCWSTR optional
-- pcSpnLength : DWORD* in/out
-- pszSpn : LPWSTR optional, out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('DSPARSE.dll');
const DsMakeSpnW = lib.func('__stdcall', 'DsMakeSpnW', 'uint32_t', ['str16', 'str16', 'str16', 'uint16_t', 'str16', 'uint32_t *', 'uint16_t *']);
// DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn)
// ServiceClass : LPCWSTR -> 'str16'
// ServiceName : LPCWSTR -> 'str16'
// InstanceName : LPCWSTR optional -> 'str16'
// InstancePort : WORD -> 'uint16_t'
// Referrer : LPCWSTR optional -> 'str16'
// pcSpnLength : DWORD* in/out -> 'uint32_t *'
// pszSpn : LPWSTR optional, out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("DSPARSE.dll", {
  DsMakeSpnW: { parameters: ["buffer", "buffer", "buffer", "u16", "buffer", "pointer", "buffer"], result: "u32" },
});
// lib.symbols.DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn)
// ServiceClass : LPCWSTR -> "buffer"
// ServiceName : LPCWSTR -> "buffer"
// InstanceName : LPCWSTR optional -> "buffer"
// InstancePort : WORD -> "u16"
// Referrer : LPCWSTR optional -> "buffer"
// pcSpnLength : DWORD* in/out -> "pointer"
// pszSpn : LPWSTR optional, out -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t DsMakeSpnW(
    const uint16_t* ServiceClass,
    const uint16_t* ServiceName,
    const uint16_t* InstanceName,
    uint16_t InstancePort,
    const uint16_t* Referrer,
    uint32_t* pcSpnLength,
    uint16_t* pszSpn);
C, "DSPARSE.dll");
// $ffi->DsMakeSpnW(ServiceClass, ServiceName, InstanceName, InstancePort, Referrer, pcSpnLength, pszSpn);
// ServiceClass : LPCWSTR
// ServiceName : LPCWSTR
// InstanceName : LPCWSTR optional
// InstancePort : WORD
// Referrer : LPCWSTR optional
// pcSpnLength : DWORD* in/out
// pszSpn : LPWSTR optional, 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 Dsparse extends StdCallLibrary {
    Dsparse INSTANCE = Native.load("dsparse", Dsparse.class, W32APIOptions.UNICODE_OPTIONS);
    int DsMakeSpnW(
        WString ServiceClass,   // LPCWSTR
        WString ServiceName,   // LPCWSTR
        WString InstanceName,   // LPCWSTR optional
        short InstancePort,   // WORD
        WString Referrer,   // LPCWSTR optional
        IntByReference pcSpnLength,   // DWORD* in/out
        char[] pszSpn   // LPWSTR optional, out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("dsparse")]
lib LibDSPARSE
  fun DsMakeSpnW = DsMakeSpnW(
    ServiceClass : UInt16*,   # LPCWSTR
    ServiceName : UInt16*,   # LPCWSTR
    InstanceName : UInt16*,   # LPCWSTR optional
    InstancePort : UInt16,   # WORD
    Referrer : UInt16*,   # LPCWSTR optional
    pcSpnLength : UInt32*,   # DWORD* in/out
    pszSpn : UInt16*   # LPWSTR optional, out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef DsMakeSpnWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Uint16, Pointer<Utf16>, Pointer<Uint32>, Pointer<Utf16>);
typedef DsMakeSpnWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Uint32>, Pointer<Utf16>);
final DsMakeSpnW = DynamicLibrary.open('DSPARSE.dll')
    .lookupFunction<DsMakeSpnWNative, DsMakeSpnWDart>('DsMakeSpnW');
// ServiceClass : LPCWSTR -> Pointer<Utf16>
// ServiceName : LPCWSTR -> Pointer<Utf16>
// InstanceName : LPCWSTR optional -> Pointer<Utf16>
// InstancePort : WORD -> Uint16
// Referrer : LPCWSTR optional -> Pointer<Utf16>
// pcSpnLength : DWORD* in/out -> Pointer<Uint32>
// pszSpn : LPWSTR optional, out -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DsMakeSpnW(
  ServiceClass: PWideChar;   // LPCWSTR
  ServiceName: PWideChar;   // LPCWSTR
  InstanceName: PWideChar;   // LPCWSTR optional
  InstancePort: Word;   // WORD
  Referrer: PWideChar;   // LPCWSTR optional
  pcSpnLength: Pointer;   // DWORD* in/out
  pszSpn: PWideChar   // LPWSTR optional, out
): DWORD; stdcall;
  external 'DSPARSE.dll' name 'DsMakeSpnW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DsMakeSpnW"
  c_DsMakeSpnW :: CWString -> CWString -> CWString -> Word16 -> CWString -> Ptr Word32 -> CWString -> IO Word32
-- ServiceClass : LPCWSTR -> CWString
-- ServiceName : LPCWSTR -> CWString
-- InstanceName : LPCWSTR optional -> CWString
-- InstancePort : WORD -> Word16
-- Referrer : LPCWSTR optional -> CWString
-- pcSpnLength : DWORD* in/out -> Ptr Word32
-- pszSpn : LPWSTR optional, out -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let dsmakespnw =
  foreign "DsMakeSpnW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint16_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> (ptr uint16_t) @-> returning uint32_t)
(* ServiceClass : LPCWSTR -> (ptr uint16_t) *)
(* ServiceName : LPCWSTR -> (ptr uint16_t) *)
(* InstanceName : LPCWSTR optional -> (ptr uint16_t) *)
(* InstancePort : WORD -> uint16_t *)
(* Referrer : LPCWSTR optional -> (ptr uint16_t) *)
(* pcSpnLength : DWORD* in/out -> (ptr uint32_t) *)
(* pszSpn : LPWSTR optional, out -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library dsparse (t "DSPARSE.dll"))
(cffi:use-foreign-library dsparse)

(cffi:defcfun ("DsMakeSpnW" ds-make-spn-w :convention :stdcall) :uint32
  (service-class (:string :encoding :utf-16le))   ; LPCWSTR
  (service-name (:string :encoding :utf-16le))   ; LPCWSTR
  (instance-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (instance-port :uint16)   ; WORD
  (referrer (:string :encoding :utf-16le))   ; LPCWSTR optional
  (pc-spn-length :pointer)   ; DWORD* in/out
  (psz-spn :pointer))   ; LPWSTR optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DsMakeSpnW = Win32::API::More->new('DSPARSE',
    'DWORD DsMakeSpnW(LPCWSTR ServiceClass, LPCWSTR ServiceName, LPCWSTR InstanceName, WORD InstancePort, LPCWSTR Referrer, LPVOID pcSpnLength, LPWSTR pszSpn)');
# my $ret = $DsMakeSpnW->Call($ServiceClass, $ServiceName, $InstanceName, $InstancePort, $Referrer, $pcSpnLength, $pszSpn);
# ServiceClass : LPCWSTR -> LPCWSTR
# ServiceName : LPCWSTR -> LPCWSTR
# InstanceName : LPCWSTR optional -> LPCWSTR
# InstancePort : WORD -> WORD
# Referrer : LPCWSTR optional -> LPCWSTR
# pcSpnLength : DWORD* in/out -> LPVOID
# pszSpn : LPWSTR optional, out -> LPWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目