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

DsMakePasswordCredentialsW

関数
ユーザー名とパスワードから認証資格情報を作成する(Unicode版)。
DLLNTDSAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

DWORD DsMakePasswordCredentialsW(
    LPCWSTR User,   // optional
    LPCWSTR Domain,   // optional
    LPCWSTR Password,   // optional
    void** pAuthIdentity
);

パラメーター

名前型方向説明
UserLPCWSTRinoptional資格情報に使用するユーザー名を格納した、null で終わる文字列へのポインター。
DomainLPCWSTRinoptionalユーザーが所属するドメインを格納した、null で終わる文字列へのポインター。
PasswordLPCWSTRinoptional資格情報に使用するパスワードを格納した、null で終わる文字列へのポインター。
pAuthIdentityvoid**out資格情報ハンドルを受け取る RPC_AUTH_IDENTITY_HANDLE 値へのポインター。このハンドルは、後続の DsBindWithCred の呼び出しで使用します。 このハンドルは、不要になった時点で DsFreePasswordCredentials 関数で解放する必要があります。

戻り値の型: DWORD

公式ドキュメント

DsBindWithCred 関数で使用するのに適した資格情報ハンドルを構築します。(Unicode)

戻り値

Windows のエラーコードを返します。主な値は次のとおりです。

解説(Remarks)

User、Domain、Password がすべて NULL の場合は、null の既定の資格情報ハンドルが作成されます。それ以外の場合は、User を指定する必要があります。UPN 形式のユーザー (例: "someone@fabrikam.com") のように User が完全修飾されている場合、Domain パラメーターは NULL でもかまいません。

pAuthIdentity に返されたハンドルを DsBindWithCred に渡した場合は、DsFreePasswordCredentials でハンドルを解放する前に DsUnBind を呼び出す必要があります。 通常の手順は次のとおりです。

  1. DsMakePasswordCredentials を呼び出して資格情報ハンドルを取得します。
  2. DsBindWithCred を呼び出し、資格情報ハンドルを渡します。
  3. バインドが不要になったら DsUnbind を呼び出します。
  4. DsFreePasswordCredentials を呼び出して資格情報ハンドルを解放します。
メモ

ntdsapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に応じてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして DsMakePasswordCredentials を定義しています。エンコード中立のエイリアスと、エンコード中立でないコードを混在させると不一致が生じ、コンパイルエラーや実行時エラーの原因となることがあります。詳細については、「Conventions for Function Prototypes」を参照してください。

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

各言語での呼び出し定義

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

DWORD DsMakePasswordCredentialsW(
    LPCWSTR User,   // optional
    LPCWSTR Domain,   // optional
    LPCWSTR Password,   // optional
    void** pAuthIdentity
);
[DllImport("NTDSAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint DsMakePasswordCredentialsW(
    [MarshalAs(UnmanagedType.LPWStr)] string User,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string Domain,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string Password,   // LPCWSTR optional
    IntPtr pAuthIdentity   // void** out
);
<DllImport("NTDSAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DsMakePasswordCredentialsW(
    <MarshalAs(UnmanagedType.LPWStr)> User As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> Domain As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> Password As String,   ' LPCWSTR optional
    pAuthIdentity As IntPtr   ' void** out
) As UInteger
End Function
' User : LPCWSTR optional
' Domain : LPCWSTR optional
' Password : LPCWSTR optional
' pAuthIdentity : void** out
Declare PtrSafe Function DsMakePasswordCredentialsW Lib "ntdsapi" ( _
    ByVal User As LongPtr, _
    ByVal Domain As LongPtr, _
    ByVal Password As LongPtr, _
    ByVal pAuthIdentity 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

DsMakePasswordCredentialsW = ctypes.windll.ntdsapi.DsMakePasswordCredentialsW
DsMakePasswordCredentialsW.restype = wintypes.DWORD
DsMakePasswordCredentialsW.argtypes = [
    wintypes.LPCWSTR,  # User : LPCWSTR optional
    wintypes.LPCWSTR,  # Domain : LPCWSTR optional
    wintypes.LPCWSTR,  # Password : LPCWSTR optional
    ctypes.c_void_p,  # pAuthIdentity : void** out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('NTDSAPI.dll')
DsMakePasswordCredentialsW = Fiddle::Function.new(
  lib['DsMakePasswordCredentialsW'],
  [
    Fiddle::TYPE_VOIDP,  # User : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # Domain : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # Password : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # pAuthIdentity : void** out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "ntdsapi")]
extern "system" {
    fn DsMakePasswordCredentialsW(
        User: *const u16,  // LPCWSTR optional
        Domain: *const u16,  // LPCWSTR optional
        Password: *const u16,  // LPCWSTR optional
        pAuthIdentity: *mut *mut ()  // void** out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("NTDSAPI.dll", CharSet = CharSet.Unicode)]
public static extern uint DsMakePasswordCredentialsW([MarshalAs(UnmanagedType.LPWStr)] string User, [MarshalAs(UnmanagedType.LPWStr)] string Domain, [MarshalAs(UnmanagedType.LPWStr)] string Password, IntPtr pAuthIdentity);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NTDSAPI_DsMakePasswordCredentialsW' -Namespace Win32 -PassThru
# $api::DsMakePasswordCredentialsW(User, Domain, Password, pAuthIdentity)
#uselib "NTDSAPI.dll"
#func global DsMakePasswordCredentialsW "DsMakePasswordCredentialsW" wptr, wptr, wptr, wptr
; DsMakePasswordCredentialsW User, Domain, Password, pAuthIdentity   ; 戻り値は stat
; User : LPCWSTR optional -> "wptr"
; Domain : LPCWSTR optional -> "wptr"
; Password : LPCWSTR optional -> "wptr"
; pAuthIdentity : void** out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "NTDSAPI.dll"
#cfunc global DsMakePasswordCredentialsW "DsMakePasswordCredentialsW" wstr, wstr, wstr, sptr
; res = DsMakePasswordCredentialsW(User, Domain, Password, pAuthIdentity)
; User : LPCWSTR optional -> "wstr"
; Domain : LPCWSTR optional -> "wstr"
; Password : LPCWSTR optional -> "wstr"
; pAuthIdentity : void** out -> "sptr"
; DWORD DsMakePasswordCredentialsW(LPCWSTR User, LPCWSTR Domain, LPCWSTR Password, void** pAuthIdentity)
#uselib "NTDSAPI.dll"
#cfunc global DsMakePasswordCredentialsW "DsMakePasswordCredentialsW" wstr, wstr, wstr, intptr
; res = DsMakePasswordCredentialsW(User, Domain, Password, pAuthIdentity)
; User : LPCWSTR optional -> "wstr"
; Domain : LPCWSTR optional -> "wstr"
; Password : LPCWSTR optional -> "wstr"
; pAuthIdentity : void** out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ntdsapi = windows.NewLazySystemDLL("NTDSAPI.dll")
	procDsMakePasswordCredentialsW = ntdsapi.NewProc("DsMakePasswordCredentialsW")
)

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

extern "ntdsapi" fn DsMakePasswordCredentialsW(
    User: [*c]const u16, // LPCWSTR optional
    Domain: [*c]const u16, // LPCWSTR optional
    Password: [*c]const u16, // LPCWSTR optional
    pAuthIdentity: ?*anyopaque // void** out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc DsMakePasswordCredentialsW(
    User: WideCString,  # LPCWSTR optional
    Domain: WideCString,  # LPCWSTR optional
    Password: WideCString,  # LPCWSTR optional
    pAuthIdentity: pointer  # void** out
): uint32 {.importc: "DsMakePasswordCredentialsW", stdcall, dynlib: "NTDSAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "ntdsapi");
extern(Windows)
uint DsMakePasswordCredentialsW(
    const(wchar)* User,   // LPCWSTR optional
    const(wchar)* Domain,   // LPCWSTR optional
    const(wchar)* Password,   // LPCWSTR optional
    void** pAuthIdentity   // void** out
);
ccall((:DsMakePasswordCredentialsW, "NTDSAPI.dll"), stdcall, UInt32,
      (Cwstring, Cwstring, Cwstring, Ptr{Cvoid}),
      User, Domain, Password, pAuthIdentity)
# User : LPCWSTR optional -> Cwstring
# Domain : LPCWSTR optional -> Cwstring
# Password : LPCWSTR optional -> Cwstring
# pAuthIdentity : void** out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t DsMakePasswordCredentialsW(
    const uint16_t* User,
    const uint16_t* Domain,
    const uint16_t* Password,
    void** pAuthIdentity);
]]
local ntdsapi = ffi.load("ntdsapi")
-- ntdsapi.DsMakePasswordCredentialsW(User, Domain, Password, pAuthIdentity)
-- User : LPCWSTR optional
-- Domain : LPCWSTR optional
-- Password : LPCWSTR optional
-- pAuthIdentity : void** 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('NTDSAPI.dll');
const DsMakePasswordCredentialsW = lib.func('__stdcall', 'DsMakePasswordCredentialsW', 'uint32_t', ['str16', 'str16', 'str16', 'void *']);
// DsMakePasswordCredentialsW(User, Domain, Password, pAuthIdentity)
// User : LPCWSTR optional -> 'str16'
// Domain : LPCWSTR optional -> 'str16'
// Password : LPCWSTR optional -> 'str16'
// pAuthIdentity : void** out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("NTDSAPI.dll", {
  DsMakePasswordCredentialsW: { parameters: ["buffer", "buffer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.DsMakePasswordCredentialsW(User, Domain, Password, pAuthIdentity)
// User : LPCWSTR optional -> "buffer"
// Domain : LPCWSTR optional -> "buffer"
// Password : LPCWSTR optional -> "buffer"
// pAuthIdentity : void** out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t DsMakePasswordCredentialsW(
    const uint16_t* User,
    const uint16_t* Domain,
    const uint16_t* Password,
    void** pAuthIdentity);
C, "NTDSAPI.dll");
// $ffi->DsMakePasswordCredentialsW(User, Domain, Password, pAuthIdentity);
// User : LPCWSTR optional
// Domain : LPCWSTR optional
// Password : LPCWSTR optional
// pAuthIdentity : void** 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 Ntdsapi extends StdCallLibrary {
    Ntdsapi INSTANCE = Native.load("ntdsapi", Ntdsapi.class, W32APIOptions.UNICODE_OPTIONS);
    int DsMakePasswordCredentialsW(
        WString User,   // LPCWSTR optional
        WString Domain,   // LPCWSTR optional
        WString Password,   // LPCWSTR optional
        Pointer pAuthIdentity   // void** out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("ntdsapi")]
lib LibNTDSAPI
  fun DsMakePasswordCredentialsW = DsMakePasswordCredentialsW(
    User : UInt16*,   # LPCWSTR optional
    Domain : UInt16*,   # LPCWSTR optional
    Password : UInt16*,   # LPCWSTR optional
    pAuthIdentity : Void**   # void** out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef DsMakePasswordCredentialsWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>);
typedef DsMakePasswordCredentialsWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>);
final DsMakePasswordCredentialsW = DynamicLibrary.open('NTDSAPI.dll')
    .lookupFunction<DsMakePasswordCredentialsWNative, DsMakePasswordCredentialsWDart>('DsMakePasswordCredentialsW');
// User : LPCWSTR optional -> Pointer<Utf16>
// Domain : LPCWSTR optional -> Pointer<Utf16>
// Password : LPCWSTR optional -> Pointer<Utf16>
// pAuthIdentity : void** out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DsMakePasswordCredentialsW(
  User: PWideChar;   // LPCWSTR optional
  Domain: PWideChar;   // LPCWSTR optional
  Password: PWideChar;   // LPCWSTR optional
  pAuthIdentity: Pointer   // void** out
): DWORD; stdcall;
  external 'NTDSAPI.dll' name 'DsMakePasswordCredentialsW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DsMakePasswordCredentialsW"
  c_DsMakePasswordCredentialsW :: CWString -> CWString -> CWString -> Ptr () -> IO Word32
-- User : LPCWSTR optional -> CWString
-- Domain : LPCWSTR optional -> CWString
-- Password : LPCWSTR optional -> CWString
-- pAuthIdentity : void** out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let dsmakepasswordcredentialsw =
  foreign "DsMakePasswordCredentialsW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr void) @-> returning uint32_t)
(* User : LPCWSTR optional -> (ptr uint16_t) *)
(* Domain : LPCWSTR optional -> (ptr uint16_t) *)
(* Password : LPCWSTR optional -> (ptr uint16_t) *)
(* pAuthIdentity : void** out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library ntdsapi (t "NTDSAPI.dll"))
(cffi:use-foreign-library ntdsapi)

(cffi:defcfun ("DsMakePasswordCredentialsW" ds-make-password-credentials-w :convention :stdcall) :uint32
  (user (:string :encoding :utf-16le))   ; LPCWSTR optional
  (domain (:string :encoding :utf-16le))   ; LPCWSTR optional
  (password (:string :encoding :utf-16le))   ; LPCWSTR optional
  (p-auth-identity :pointer))   ; void** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DsMakePasswordCredentialsW = Win32::API::More->new('NTDSAPI',
    'DWORD DsMakePasswordCredentialsW(LPCWSTR User, LPCWSTR Domain, LPCWSTR Password, LPVOID pAuthIdentity)');
# my $ret = $DsMakePasswordCredentialsW->Call($User, $Domain, $Password, $pAuthIdentity);
# User : LPCWSTR optional -> LPCWSTR
# Domain : LPCWSTR optional -> LPCWSTR
# Password : LPCWSTR optional -> LPCWSTR
# pAuthIdentity : void** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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