Win32 API 日本語リファレンス
ホームSecurity.Credentials › CredUICmdLinePromptForCredentialsW

CredUICmdLinePromptForCredentialsW

関数
コマンドラインで資格情報の入力を求める。
DLLcredui.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

DWORD CredUICmdLinePromptForCredentialsW(
    LPCWSTR pszTargetName,   // optional
    SecHandle* pContext,   // optional
    DWORD dwAuthError,
    LPWSTR UserName,
    DWORD ulUserBufferSize,
    LPWSTR pszPassword,
    DWORD ulPasswordBufferSize,
    BOOL* pfSave,   // optional
    CREDUI_FLAGS dwFlags
);

パラメーター

名前方向説明
pszTargetNameLPCWSTRinoptional資格情報の対象(通常はサーバー名)を含むnull終端文字列へのポインター。DFS 接続の場合、この文字列は ServerName\ShareName の形式になります。pszTargetName パラメーターは対象情報を識別するために使用され、資格情報の格納と取得に使われます。
pContextSecHandle*optional現在は予約されており、NULL を指定する必要があります。
dwAuthErrorDWORDin資格情報の入力を求める理由を指定します。呼び出し元は、別の認証呼び出しから返されたこの Windows エラー値を渡すことで、ダイアログボックスが特定のエラーに対応できるようにします。たとえば、パスワード期限切れのステータスコードが渡された場合、ダイアログボックスはアカウントのパスワードを変更するようユーザーに求めます。
UserNameLPWSTRinout

資格情報のユーザー名を含むnull終端文字列へのポインター。pszUserName に長さが 0 でない文字列を指定した場合、ユーザーにはパスワードの入力のみが求められます。ユーザー名/パスワード以外の資格情報の場合は、マーシャリングされた形式の資格情報を渡すことができます。この文字列は CredMarshalCredential を呼び出して作成します。

この関数は、ユーザーが入力した名前をこのバッファーに書き込み、最大で ulUserNameMaxChars 文字をコピーします。この形式の文字列は、 CredUIParseUsername 関数を呼び出すことでユーザー名/パスワード形式に変換できます。マーシャリングされた形式の文字列は、security support provider(SSP)に直接渡すことができます。

CREDUI_FLAGS_DO_NOT_PERSIST フラグが指定されていない場合、このパラメーターで返される値は、CredUIParseUsername に渡す以外の方法で検査、出力、永続化すべきではない形式になっています。CredUIParseUsername の結果は、WNetAddConnection や SSP API などのクライアント側認証 API にのみ渡すことができます。

ulUserBufferSizeDWORDin

終端のnull文字を含めて pszUserName にコピーできる最大文字数。

注意 CREDUI_MAX_USERNAME_LENGTH には終端のnull文字は含まれません。
pszPasswordLPWSTRinout

資格情報のパスワードを含むnull終端文字列へのポインター。pszPassword に長さが 0 でない文字列を指定した場合、パスワードパラメーターにはその文字列があらかじめ設定されます。

この関数は、ユーザーが入力したパスワードをこのバッファーに書き込み、最大で ulPasswordMaxChars 文字をコピーします。CREDUI_FLAGS_DO_NOT_PERSIST フラグが指定されていない場合、このパラメーターで返される値は、WNetAddConnection や SSP 関数などのクライアント側認証関数に渡す以外の方法で検査、出力、永続化すべきではない形式になっています。

パスワードの使用が終わったら、SecureZeroMemory 関数を呼び出してメモリからパスワードを消去してください。パスワードの保護の詳細については、Handling Passwords を参照してください。

ulPasswordBufferSizeDWORDin

終端のnull文字を含めて pszPassword にコピーできる最大文字数。

注意 CREDUI_MAX_PASSWORD_LENGTH には終端のnull文字は含まれません。
pfSaveBOOL*inoutoptionalSave メッセージの初期状態を指定し、ユーザーがコマンドプロンプトに応答した後の Save メッセージの状態を受け取る BOOL へのポインター。pfSaveNULL でなく、CredUIPromptForCredentialsNO_ERROR を返した場合、pfSaveSave メッセージの状態を返します。CREDUI_FLAGS_PERSIST フラグが指定されている場合、Save メッセージは表示されませんが「y」と見なされます。CREDUI_FLAGS_DO_NOT_PERSIST フラグが指定され、CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX が指定されていない場合、Save メッセージは表示されませんが「n」と見なされます。
dwFlagsCREDUI_FLAGSin

この関数の特別な動作を指定する DWORD 値。この値には、次の値のうち 0 個以上をビット単位の OR で組み合わせて指定できます。

意味
CREDUI_FLAGS_ALWAYS_SHOW_UI
資格情報マネージャー内の既存の資格情報から資格情報を返せる場合でも、ユーザーインターフェイスを表示します。このフラグは、CREDUI_FLAGS_GENERIC_CREDENTIALS も指定されている場合にのみ許可され、CREDUI_FLAGS_GENERIC_CREDENTIALS と組み合わせてのみ使用されます。
CREDUI_FLAGS_DO_NOT_PERSIST
保存メッセージを表示せず、資格情報も格納しません。

CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX を併せて渡すと、保存メッセージのみを表示し、その結果を pfSave で返すことができます。

CREDUI_FLAGS_EXCLUDE_CERTIFICATES
ユーザー名/パスワードの入力を求めます。pszUserName パラメーターが指定されている場合、ユーザー名の入力は省略されます。資格情報が永続化される場合は、渡されたユーザー名を資格情報とともに格納します。
CREDUI_FLAGS_EXPECT_CONFIRMATION
返された資格情報が実際に有効かどうかを判断するために、呼び出し元が CredUIConfirmCredentials を呼び出すことを指定します。これにより、無効な資格情報が資格情報マネージャーに保存されないようにします。CREDUI_FLAGS_DO_NOT_PERSIST が指定されている場合を除き、このフラグを指定してください。
CREDUI_FLAGS_GENERIC_CREDENTIALS
ユーザーが入力した資格情報を汎用資格情報として扱います。
CREDUI_FLAGS_INCORRECT_PASSWORD
このフラグは何も処理せずに無視されます。
CREDUI_FLAGS_PERSIST
保存メッセージは表示しませんが、ユーザーが「y」と答えたかのように資格情報を保存します。
CREDUI_FLAGS_REQUEST_ADMINISTRATOR
このフラグは何も処理せずに無視されます。
CREDUI_FLAGS_REQUIRE_CERTIFICATE
将来の使用のために予約されています。このフラグは渡さないでください。
CREDUI_FLAGS_REQUIRE_SMARTCARD
スマートカードを使用し、PIN の入力を求めます。複数のスマートカードが利用可能な場合は、そのうちの 1 つを選択します。pszUserName パラメーターで空でない文字列が渡された場合、その文字列はいずれかのスマートカード上の証明書に関連付けられた UPN と一致する必要があります。文字列が証明書の UPN 全体と一致する場合、または UPN のアットマーク(@)の左側の部分と一致する場合に、UPN が一致したと見なされます。一致するものがあれば、一致したスマートカードが選択されます。
CREDUI_FLAGS_SERVER_CREDENTIAL
このフラグは、認証が失敗した場合にダイアログボックスにあらかじめ設定するために一致する資格情報を探す場合にのみ意味を持ちます。このフラグが指定されると、ワイルドカードを含む資格情報は一致対象になりません。資格情報を書き込む際には効果がありません。CredUI はワイルドカード文字を含む資格情報を作成しません。見つかったワイルドカード資格情報は、ユーザーが明示的に作成したか、RAS 接続が確立されたときのようにプログラムによって作成されたものです。
CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX
保存メッセージを表示し、ユーザーが「y」と答えた場合は pfSave 出力パラメーターに TRUE を、「n」と答えた場合は FALSE を返します。このフラグを使用するには CREDUI_FLAGS_DO_NOT_PERSIST を指定する必要があります。
CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS
資格情報は run-as 資格情報です。pszTargetName パラメーターは実行されるコマンドまたはプログラムの名前を指定します。これは入力を求める表示の目的にのみ使用されます。

戻り値の型: DWORD

公式ドキュメント

コマンドライン(コンソール)アプリケーションで作業しているユーザーに資格情報の入力を求め、その情報を受け取ります。ユーザーが入力した名前とパスワードは、検証のために呼び出し元アプリケーションに渡されます。(Unicode)

戻り値

戻り値は DWORD で、次のいずれかの値になります。

説明
ERROR_INVALID_FLAGS
このステータスは、無効なフラグの組み合わせのいずれかに対して返されます。
ERROR_INVALID_PARAMETER
pszTargetNameNULL、空文字列、または CREDUI_MAX_DOMAIN_LENGTH より長いか、あるいは pUiInfoNULL でなく、それが指す CredUI_INFO 構造体が次の要件のいずれかを満たしていませんでした。
  • cbSize メンバーは 1 でなければなりません。
  • hbmBanner メンバーが NULL でない場合、その型は OBJ_BITMAP でなければなりません。
  • pszMessageText メンバーが NULL でない場合、CREDUI_MAX_MESSAGE_LENGTH を超えてはなりません。
  • pszCaptionText メンバーが NULL でない場合、CREDUI_MAX_CAPTION_LENGTH を超えてはなりません。
ERROR_NO_SUCH_LOGON_SESSION
資格情報マネージャーを使用できません。通常、このエラーは CredUICmdLinePromptForCredentials を呼び出し、CREDUI_FLAGS_DO_NOT_PERSIST フラグを渡すことで処理されます。
NO_ERROR
ユーザーが OK を選択しました。pszUserNamepszPasswordpfSave の各変数は、前述のとおりの値を返します。

解説(Remarks)

CREDUI_FLAGS_REQUIRE_SMARTCARDCREDUI_FLAGS_REQUIRE_CERTIFICATE、CREDUI_FLAGS_EXCLUDE_CERTIFICATE の各フラグは相互に排他的です。

CREDUI_FLAGS_DO_NOT_PERSIST が指定されている場合、pszTargetName を指定するか、あるいは uiInfo->pszMessageTextuiInfo->pszCaption を指定する必要があります。

CREDUI_FLAG_USERNAME_TARGET_CREDENTIALS フラグと CREDUI_FLAGS_GENERIC_CREDENTIALS フラグは相互に排他的です。どちらも指定されていない場合、資格情報はドメイン資格情報になります。

CREDUI_FLAGS_GENERIC_CREDENTIALS が指定されていない場合、または CREDUI_FLAGS_COMPLETE_USERNAME が指定されている場合、入力された名前は構文チェックされます。構文チェックとは、CredUIParseUserName が暗黙に適用するのと同じ規則が使用されることを意味します。入力された名前が無効な場合、ユーザーは有効な名前の入力を求められます。入力された名前のドメイン部分が欠落している場合は、対象名に基づいて補われます。

CREDUI_FLAGS_GENERIC_CREDENTIALS が指定され、CREDUI_FLAGS_VALIDATE_USERNAME も指定されている場合、入力された名前は構文チェックされます。入力された名前が無効な場合、ユーザーは有効な名前の入力を求められます。

CREDUI_FLAGS_GENERIC_CREDENTIALS が指定され、CREDUI_FLAGS_COMPLETE_USERNAMECREDUI_FLAGS_VALIDATE_USERNAME も指定されていない場合、入力された名前は一切構文チェックされません。

CREDUI_FLAGS_PERSISTCREDUI_FLAGS_DO_NOT_PERSIST のどちらも設定されていない場合、保存メッセージが表示され、それによって資格情報を保存するかどうかが決まります。

CREDUI_FLAGS_PROMPT_FOR_SAVE が指定されている場合、pfSave パラメーターは NULL であってはなりません。

CREDUI_FLAGS_REQUIRE_SMARTCARD フラグと CREDUI_FLAGS_EXCLUDE_CERTIFICATES フラグは相互に排他的です。CredUICmdLinePromptForCredentials は、スマートカード証明書またはパスワードベースの資格情報の入力を求めることをサポートしています。スマートカード証明書以外の証明書には対応しておらず、1 回の呼び出しで両方の入力を求めることもできません。

呼び出しモード

対象情報

対象情報とは、アクセスするリソースの場所に関する情報です。リソースに対するすべての潜在的な対象名の一覧を取得するには、 CredGetTargetInfo を呼び出します。CredGetTargetInfo は、指定された対象に対して Negotiate、NTLM、または Kerberos 認証パッケージのいずれかが認証に使用された際に、それらのパッケージによってキャッシュされた情報を返します。CredGetTargetInfo は、対象に対して次の名前の一部またはすべてを返します。

これらの情報のいずれかは、対象コンピューターに該当しない場合は欠落することがあります。たとえば、ワークグループのメンバーであるコンピューターには NetBIOS ドメイン名がありません。Windows ドメインのメンバーであるコンピューターには DNS ドメイン名や DNS ツリー名がありません。

資格情報は、対象名に基づいて資格情報マネージャーに格納されます。各対象名は、資格情報マネージャーに既に格納されている資格情報と衝突しない範囲で、できるだけ汎用的に格納されます。対象名で資格情報を格納することの重要な効果として、特定のユーザーは資格情報マネージャー内で対象ごとに 1 つの資格情報しか保持できません。

メモ

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

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

各言語での呼び出し定義

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

DWORD CredUICmdLinePromptForCredentialsW(
    LPCWSTR pszTargetName,   // optional
    SecHandle* pContext,   // optional
    DWORD dwAuthError,
    LPWSTR UserName,
    DWORD ulUserBufferSize,
    LPWSTR pszPassword,
    DWORD ulPasswordBufferSize,
    BOOL* pfSave,   // optional
    CREDUI_FLAGS dwFlags
);
[DllImport("credui.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint CredUICmdLinePromptForCredentialsW(
    [MarshalAs(UnmanagedType.LPWStr)] string pszTargetName,   // LPCWSTR optional
    IntPtr pContext,   // SecHandle* optional
    uint dwAuthError,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder UserName,   // LPWSTR in/out
    uint ulUserBufferSize,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPassword,   // LPWSTR in/out
    uint ulPasswordBufferSize,   // DWORD
    IntPtr pfSave,   // BOOL* optional, in/out
    uint dwFlags   // CREDUI_FLAGS
);
<DllImport("credui.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function CredUICmdLinePromptForCredentialsW(
    <MarshalAs(UnmanagedType.LPWStr)> pszTargetName As String,   ' LPCWSTR optional
    pContext As IntPtr,   ' SecHandle* optional
    dwAuthError As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> UserName As System.Text.StringBuilder,   ' LPWSTR in/out
    ulUserBufferSize As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> pszPassword As System.Text.StringBuilder,   ' LPWSTR in/out
    ulPasswordBufferSize As UInteger,   ' DWORD
    pfSave As IntPtr,   ' BOOL* optional, in/out
    dwFlags As UInteger   ' CREDUI_FLAGS
) As UInteger
End Function
' pszTargetName : LPCWSTR optional
' pContext : SecHandle* optional
' dwAuthError : DWORD
' UserName : LPWSTR in/out
' ulUserBufferSize : DWORD
' pszPassword : LPWSTR in/out
' ulPasswordBufferSize : DWORD
' pfSave : BOOL* optional, in/out
' dwFlags : CREDUI_FLAGS
Declare PtrSafe Function CredUICmdLinePromptForCredentialsW Lib "credui" ( _
    ByVal pszTargetName As LongPtr, _
    ByVal pContext As LongPtr, _
    ByVal dwAuthError As Long, _
    ByVal UserName As LongPtr, _
    ByVal ulUserBufferSize As Long, _
    ByVal pszPassword As LongPtr, _
    ByVal ulPasswordBufferSize As Long, _
    ByVal pfSave As LongPtr, _
    ByVal dwFlags As Long) 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

CredUICmdLinePromptForCredentialsW = ctypes.windll.credui.CredUICmdLinePromptForCredentialsW
CredUICmdLinePromptForCredentialsW.restype = wintypes.DWORD
CredUICmdLinePromptForCredentialsW.argtypes = [
    wintypes.LPCWSTR,  # pszTargetName : LPCWSTR optional
    ctypes.c_void_p,  # pContext : SecHandle* optional
    wintypes.DWORD,  # dwAuthError : DWORD
    wintypes.LPWSTR,  # UserName : LPWSTR in/out
    wintypes.DWORD,  # ulUserBufferSize : DWORD
    wintypes.LPWSTR,  # pszPassword : LPWSTR in/out
    wintypes.DWORD,  # ulPasswordBufferSize : DWORD
    ctypes.c_void_p,  # pfSave : BOOL* optional, in/out
    wintypes.DWORD,  # dwFlags : CREDUI_FLAGS
]
require 'fiddle'
require 'fiddle/import'

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

var (
	credui = windows.NewLazySystemDLL("credui.dll")
	procCredUICmdLinePromptForCredentialsW = credui.NewProc("CredUICmdLinePromptForCredentialsW")
)

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

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

typedef CredUICmdLinePromptForCredentialsWNative = Uint32 Function(Pointer<Utf16>, Pointer<Void>, Uint32, Pointer<Utf16>, Uint32, Pointer<Utf16>, Uint32, Pointer<Int32>, Uint32);
typedef CredUICmdLinePromptForCredentialsWDart = int Function(Pointer<Utf16>, Pointer<Void>, int, Pointer<Utf16>, int, Pointer<Utf16>, int, Pointer<Int32>, int);
final CredUICmdLinePromptForCredentialsW = DynamicLibrary.open('credui.dll')
    .lookupFunction<CredUICmdLinePromptForCredentialsWNative, CredUICmdLinePromptForCredentialsWDart>('CredUICmdLinePromptForCredentialsW');
// pszTargetName : LPCWSTR optional -> Pointer<Utf16>
// pContext : SecHandle* optional -> Pointer<Void>
// dwAuthError : DWORD -> Uint32
// UserName : LPWSTR in/out -> Pointer<Utf16>
// ulUserBufferSize : DWORD -> Uint32
// pszPassword : LPWSTR in/out -> Pointer<Utf16>
// ulPasswordBufferSize : DWORD -> Uint32
// pfSave : BOOL* optional, in/out -> Pointer<Int32>
// dwFlags : CREDUI_FLAGS -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredUICmdLinePromptForCredentialsW(
  pszTargetName: PWideChar;   // LPCWSTR optional
  pContext: Pointer;   // SecHandle* optional
  dwAuthError: DWORD;   // DWORD
  UserName: PWideChar;   // LPWSTR in/out
  ulUserBufferSize: DWORD;   // DWORD
  pszPassword: PWideChar;   // LPWSTR in/out
  ulPasswordBufferSize: DWORD;   // DWORD
  pfSave: Pointer;   // BOOL* optional, in/out
  dwFlags: DWORD   // CREDUI_FLAGS
): DWORD; stdcall;
  external 'credui.dll' name 'CredUICmdLinePromptForCredentialsW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CredUICmdLinePromptForCredentialsW"
  c_CredUICmdLinePromptForCredentialsW :: CWString -> Ptr () -> Word32 -> CWString -> Word32 -> CWString -> Word32 -> Ptr CInt -> Word32 -> IO Word32
-- pszTargetName : LPCWSTR optional -> CWString
-- pContext : SecHandle* optional -> Ptr ()
-- dwAuthError : DWORD -> Word32
-- UserName : LPWSTR in/out -> CWString
-- ulUserBufferSize : DWORD -> Word32
-- pszPassword : LPWSTR in/out -> CWString
-- ulPasswordBufferSize : DWORD -> Word32
-- pfSave : BOOL* optional, in/out -> Ptr CInt
-- dwFlags : CREDUI_FLAGS -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let creduicmdlinepromptforcredentialsw =
  foreign "CredUICmdLinePromptForCredentialsW"
    ((ptr uint16_t) @-> (ptr void) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> (ptr int32_t) @-> uint32_t @-> returning uint32_t)
(* pszTargetName : LPCWSTR optional -> (ptr uint16_t) *)
(* pContext : SecHandle* optional -> (ptr void) *)
(* dwAuthError : DWORD -> uint32_t *)
(* UserName : LPWSTR in/out -> (ptr uint16_t) *)
(* ulUserBufferSize : DWORD -> uint32_t *)
(* pszPassword : LPWSTR in/out -> (ptr uint16_t) *)
(* ulPasswordBufferSize : DWORD -> uint32_t *)
(* pfSave : BOOL* optional, in/out -> (ptr int32_t) *)
(* dwFlags : CREDUI_FLAGS -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library credui (t "credui.dll"))
(cffi:use-foreign-library credui)

(cffi:defcfun ("CredUICmdLinePromptForCredentialsW" cred-uicmd-line-prompt-for-credentials-w :convention :stdcall) :uint32
  (psz-target-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (p-context :pointer)   ; SecHandle* optional
  (dw-auth-error :uint32)   ; DWORD
  (user-name :pointer)   ; LPWSTR in/out
  (ul-user-buffer-size :uint32)   ; DWORD
  (psz-password :pointer)   ; LPWSTR in/out
  (ul-password-buffer-size :uint32)   ; DWORD
  (pf-save :pointer)   ; BOOL* optional, in/out
  (dw-flags :uint32))   ; CREDUI_FLAGS
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CredUICmdLinePromptForCredentialsW = Win32::API::More->new('credui',
    'DWORD CredUICmdLinePromptForCredentialsW(LPCWSTR pszTargetName, LPVOID pContext, DWORD dwAuthError, LPWSTR UserName, DWORD ulUserBufferSize, LPWSTR pszPassword, DWORD ulPasswordBufferSize, LPVOID pfSave, DWORD dwFlags)');
# my $ret = $CredUICmdLinePromptForCredentialsW->Call($pszTargetName, $pContext, $dwAuthError, $UserName, $ulUserBufferSize, $pszPassword, $ulPasswordBufferSize, $pfSave, $dwFlags);
# pszTargetName : LPCWSTR optional -> LPCWSTR
# pContext : SecHandle* optional -> LPVOID
# dwAuthError : DWORD -> DWORD
# UserName : LPWSTR in/out -> LPWSTR
# ulUserBufferSize : DWORD -> DWORD
# pszPassword : LPWSTR in/out -> LPWSTR
# ulPasswordBufferSize : DWORD -> DWORD
# pfSave : BOOL* optional, in/out -> LPVOID
# dwFlags : CREDUI_FLAGS -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目
使用する型