NetUserChangePassword
関数シグネチャ
// NETAPI32.dll
#include <windows.h>
DWORD NetUserChangePassword(
LPCWSTR domainname, // optional
LPCWSTR username, // optional
LPCWSTR oldpassword,
LPCWSTR newpassword
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| domainname | LPCWSTR | inoptional | 関数を実行するリモートサーバーまたはドメインの DNS 名または NetBIOS 名を指定する定数文字列へのポインターです。このパラメーターが NULL の場合、呼び出し元のログオンドメインが使用されます。 |
| username | LPCWSTR | inoptional | ユーザー名を指定する定数文字列へのポインターです。 NetUserChangePassword 関数は、指定されたユーザーのパスワードを変更します。 このパラメーターが NULL の場合、呼び出し元のログオン名が使用されます。詳細については、以降の「解説」セクションを参照してください。 |
| oldpassword | LPCWSTR | in | ユーザーの古いパスワードを指定する定数文字列へのポインターです。 |
| newpassword | LPCWSTR | in | ユーザーの新しいパスワードを指定する定数文字列へのポインターです。 |
戻り値の型: DWORD
公式ドキュメント
NetUserChangePassword 関数は、指定されたネットワークサーバーまたはドメイン上のユーザーのパスワードを変更します。
戻り値
関数が成功した場合、戻り値は NERR_Success です。
関数が失敗した場合、戻り値は次のいずれかのエラーコードになります。
| 戻り値 | 説明 |
|---|---|
| ユーザーは、要求された情報へのアクセス権を持っていません。 | |
| ユーザーが無効なパスワードを入力しました。 | |
|
コンピューター名が無効です。 |
|
この操作は、ドメインのプライマリドメインコントローラー上でのみ許可されます。 |
|
ユーザー名が見つかりませんでした。 |
|
パスワードが必要な長さよりも短すぎます。(パスワードが長すぎる、変更履歴において直近すぎる、一意な文字が十分でない、またはその他のパスワードポリシー要件を満たしていない可能性もあります。) |
解説(Remarks)
Active Directory 向けにプログラミングを行う場合、ネットワーク管理ユーザー関数を呼び出すことで実現できるのと同じ結果を、特定の Active Directory Service Interface (ADSI) メソッドを呼び出すことで得られる場合があります。詳細については、 IADsUser および IADsComputer を参照してください。
アプリケーションが Active Directory を実行しているドメインコントローラー上で NetUserChangePassword 関数を呼び出す場合、アクセスの許可または拒否は セキュリティ保護可能なオブジェクト のアクセス制御リスト (ACL) に基づいて行われます。既定の ACL では、Domain Admins および Account Operators のみがこの関数を呼び出すことを許可します。メンバーサーバーまたはワークステーションでは、Administrators および Power Users のみがこの関数を呼び出すことができます。ユーザーは自分自身のパスワードを変更できます。詳細については、 Security Requirements for the Network Management Functions を参照してください。ACL、ACE、およびアクセストークンの詳細については、 Access Control Model を参照してください。
この関数のアクセスチェックには、User オブジェクトのセキュリティ記述子が使用されます。さらに、呼び出し元は User オブジェクトに対する「パスワードの変更」制御アクセス権 を持っている必要があります。この権利は、既定で Anonymous Logon および Everyone に付与されています。
関数を成功させるには、oldpassword パラメーターが現在存在するパスワードと一致している必要があることに注意してください。
場合によっては、 NetUserChangePassword 関数を呼び出すプロセスに SE_CHANGE_NOTIFY_NAME 特権が有効になっている必要もあります。そうでない場合、 NetUserChangePassword は失敗し、 GetLastError は ERROR_ACCESS_DENIED を返します。この特権は、 LocalSystem アカウント や administrators グループのメンバーであるアカウントには必要ありません。既定では、SE_CHANGE_NOTIFY_NAME はすべてのユーザーに対して有効になっていますが、一部の管理者が全員に対してこの特権を無効にしている場合があります。アカウント特権の詳細については、 Privileges および Authorization Constants を参照してください。
NetUserGetInfo および NetUserSetInfo 関数を使用して、次回のログオン時にユーザーにログオンパスワードの変更を強制する方法を示すコードサンプルについては、 Forcing a User to Change the Logon Password を参照してください。
ユーザーアカウント名は 20 文字、グループ名は 256 文字に制限されています。さらに、アカウント名はピリオドで終わることはできず、カンマや次の印字可能文字を含めることはできません: ", /, , [, ], :, |, <, >, +, =, ;, ?, *。また、名前には印字不可能な 1~31 の範囲の文字を含めることもできません。
NetUserChangePassword 関数は、oldpassword および newpassword パラメーターがネットワーク経由でリモートサーバーに送信される際にどのように保護されるかを制御しません。これらのパラメーターの暗号化は、ネットワークトランスポートを提供するネットワークリダイレクターによってサポートされるリモートプロシージャコール (RPC) メカニズムによって処理されます。暗号化はまた、ローカルコンピューターによってサポートされるセキュリティメカニズム、および domainname パラメーターで指定されたリモートネットワークサーバーまたはドメインによってサポートされるセキュリティメカニズムによっても制御されます。Microsoft ネットワークリダイレクターが使用され、リモートネットワークサーバーが Microsoft Windows を実行している場合のセキュリティの詳細については、MS-RPCE、MS-SAMR、MS-SPNG、および MS-NLMP のプロトコルドキュメントを参照してください。
例
次のコードサンプルは、NetUserChangePassword 関数の呼び出しによってユーザーのパスワードを変更する方法を示しています。この関数のすべてのパラメーターが必須です。
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")
#include <stdio.h>
#include <windows.h>
#include <lm.h>
int wmain(int argc, wchar_t *argv[])
{
DWORD dwError = 0;
NET_API_STATUS nStatus;
//
// All parameters are required.
//
if (argc != 5)
{
fwprintf(stderr, L"Usage: %s \\\\ServerName UserName OldPassword NewPassword\n", argv[0]);
exit(1);
}
//
// Call the NetUserChangePassword function.
//
nStatus = NetUserChangePassword(argv[1], argv[2], argv[3], argv[4]);
//
// If the call succeeds, inform the user.
//
if (nStatus == NERR_Success)
fwprintf(stderr, L"User password has been changed successfully\n");
//
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
return 0;
}
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// NETAPI32.dll
#include <windows.h>
DWORD NetUserChangePassword(
LPCWSTR domainname, // optional
LPCWSTR username, // optional
LPCWSTR oldpassword,
LPCWSTR newpassword
);[DllImport("NETAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern uint NetUserChangePassword(
[MarshalAs(UnmanagedType.LPWStr)] string domainname, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string username, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string oldpassword, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string newpassword // LPCWSTR
);<DllImport("NETAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function NetUserChangePassword(
<MarshalAs(UnmanagedType.LPWStr)> domainname As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> username As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> oldpassword As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> newpassword As String ' LPCWSTR
) As UInteger
End Function' domainname : LPCWSTR optional
' username : LPCWSTR optional
' oldpassword : LPCWSTR
' newpassword : LPCWSTR
Declare PtrSafe Function NetUserChangePassword Lib "netapi32" ( _
ByVal domainname As LongPtr, _
ByVal username As LongPtr, _
ByVal oldpassword As LongPtr, _
ByVal newpassword As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
NetUserChangePassword = ctypes.windll.netapi32.NetUserChangePassword
NetUserChangePassword.restype = wintypes.DWORD
NetUserChangePassword.argtypes = [
wintypes.LPCWSTR, # domainname : LPCWSTR optional
wintypes.LPCWSTR, # username : LPCWSTR optional
wintypes.LPCWSTR, # oldpassword : LPCWSTR
wintypes.LPCWSTR, # newpassword : LPCWSTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('NETAPI32.dll')
NetUserChangePassword = Fiddle::Function.new(
lib['NetUserChangePassword'],
[
Fiddle::TYPE_VOIDP, # domainname : LPCWSTR optional
Fiddle::TYPE_VOIDP, # username : LPCWSTR optional
Fiddle::TYPE_VOIDP, # oldpassword : LPCWSTR
Fiddle::TYPE_VOIDP, # newpassword : LPCWSTR
],
-Fiddle::TYPE_INT)#[link(name = "netapi32")]
extern "system" {
fn NetUserChangePassword(
domainname: *const u16, // LPCWSTR optional
username: *const u16, // LPCWSTR optional
oldpassword: *const u16, // LPCWSTR
newpassword: *const u16 // LPCWSTR
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("NETAPI32.dll", SetLastError = true)]
public static extern uint NetUserChangePassword([MarshalAs(UnmanagedType.LPWStr)] string domainname, [MarshalAs(UnmanagedType.LPWStr)] string username, [MarshalAs(UnmanagedType.LPWStr)] string oldpassword, [MarshalAs(UnmanagedType.LPWStr)] string newpassword);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NETAPI32_NetUserChangePassword' -Namespace Win32 -PassThru
# $api::NetUserChangePassword(domainname, username, oldpassword, newpassword)#uselib "NETAPI32.dll"
#func global NetUserChangePassword "NetUserChangePassword" sptr, sptr, sptr, sptr
; NetUserChangePassword domainname, username, oldpassword, newpassword ; 戻り値は stat
; domainname : LPCWSTR optional -> "sptr"
; username : LPCWSTR optional -> "sptr"
; oldpassword : LPCWSTR -> "sptr"
; newpassword : LPCWSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "NETAPI32.dll"
#cfunc global NetUserChangePassword "NetUserChangePassword" wstr, wstr, wstr, wstr
; res = NetUserChangePassword(domainname, username, oldpassword, newpassword)
; domainname : LPCWSTR optional -> "wstr"
; username : LPCWSTR optional -> "wstr"
; oldpassword : LPCWSTR -> "wstr"
; newpassword : LPCWSTR -> "wstr"; DWORD NetUserChangePassword(LPCWSTR domainname, LPCWSTR username, LPCWSTR oldpassword, LPCWSTR newpassword)
#uselib "NETAPI32.dll"
#cfunc global NetUserChangePassword "NetUserChangePassword" wstr, wstr, wstr, wstr
; res = NetUserChangePassword(domainname, username, oldpassword, newpassword)
; domainname : LPCWSTR optional -> "wstr"
; username : LPCWSTR optional -> "wstr"
; oldpassword : LPCWSTR -> "wstr"
; newpassword : LPCWSTR -> "wstr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
procNetUserChangePassword = netapi32.NewProc("NetUserChangePassword")
)
// domainname (LPCWSTR optional), username (LPCWSTR optional), oldpassword (LPCWSTR), newpassword (LPCWSTR)
r1, _, err := procNetUserChangePassword.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(domainname))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(username))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(oldpassword))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(newpassword))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction NetUserChangePassword(
domainname: PWideChar; // LPCWSTR optional
username: PWideChar; // LPCWSTR optional
oldpassword: PWideChar; // LPCWSTR
newpassword: PWideChar // LPCWSTR
): DWORD; stdcall;
external 'NETAPI32.dll' name 'NetUserChangePassword';result := DllCall("NETAPI32\NetUserChangePassword"
, "WStr", domainname ; LPCWSTR optional
, "WStr", username ; LPCWSTR optional
, "WStr", oldpassword ; LPCWSTR
, "WStr", newpassword ; LPCWSTR
, "UInt") ; return: DWORD●NetUserChangePassword(domainname, username, oldpassword, newpassword) = DLL("NETAPI32.dll", "dword NetUserChangePassword(char*, char*, char*, char*)")
# 呼び出し: NetUserChangePassword(domainname, username, oldpassword, newpassword)
# domainname : LPCWSTR optional -> "char*"
# username : LPCWSTR optional -> "char*"
# oldpassword : LPCWSTR -> "char*"
# newpassword : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "netapi32" fn NetUserChangePassword(
domainname: [*c]const u16, // LPCWSTR optional
username: [*c]const u16, // LPCWSTR optional
oldpassword: [*c]const u16, // LPCWSTR
newpassword: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) u32;proc NetUserChangePassword(
domainname: WideCString, # LPCWSTR optional
username: WideCString, # LPCWSTR optional
oldpassword: WideCString, # LPCWSTR
newpassword: WideCString # LPCWSTR
): uint32 {.importc: "NetUserChangePassword", stdcall, dynlib: "NETAPI32.dll".}pragma(lib, "netapi32");
extern(Windows)
uint NetUserChangePassword(
const(wchar)* domainname, // LPCWSTR optional
const(wchar)* username, // LPCWSTR optional
const(wchar)* oldpassword, // LPCWSTR
const(wchar)* newpassword // LPCWSTR
);ccall((:NetUserChangePassword, "NETAPI32.dll"), stdcall, UInt32,
(Cwstring, Cwstring, Cwstring, Cwstring),
domainname, username, oldpassword, newpassword)
# domainname : LPCWSTR optional -> Cwstring
# username : LPCWSTR optional -> Cwstring
# oldpassword : LPCWSTR -> Cwstring
# newpassword : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t NetUserChangePassword(
const uint16_t* domainname,
const uint16_t* username,
const uint16_t* oldpassword,
const uint16_t* newpassword);
]]
local netapi32 = ffi.load("netapi32")
-- netapi32.NetUserChangePassword(domainname, username, oldpassword, newpassword)
-- domainname : LPCWSTR optional
-- username : LPCWSTR optional
-- oldpassword : LPCWSTR
-- newpassword : LPCWSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('NETAPI32.dll');
const NetUserChangePassword = lib.func('__stdcall', 'NetUserChangePassword', 'uint32_t', ['str16', 'str16', 'str16', 'str16']);
// NetUserChangePassword(domainname, username, oldpassword, newpassword)
// domainname : LPCWSTR optional -> 'str16'
// username : LPCWSTR optional -> 'str16'
// oldpassword : LPCWSTR -> 'str16'
// newpassword : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("NETAPI32.dll", {
NetUserChangePassword: { parameters: ["buffer", "buffer", "buffer", "buffer"], result: "u32" },
});
// lib.symbols.NetUserChangePassword(domainname, username, oldpassword, newpassword)
// domainname : LPCWSTR optional -> "buffer"
// username : LPCWSTR optional -> "buffer"
// oldpassword : LPCWSTR -> "buffer"
// newpassword : LPCWSTR -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t NetUserChangePassword(
const uint16_t* domainname,
const uint16_t* username,
const uint16_t* oldpassword,
const uint16_t* newpassword);
C, "NETAPI32.dll");
// $ffi->NetUserChangePassword(domainname, username, oldpassword, newpassword);
// domainname : LPCWSTR optional
// username : LPCWSTR optional
// oldpassword : LPCWSTR
// newpassword : 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 Netapi32 extends StdCallLibrary {
Netapi32 INSTANCE = Native.load("netapi32", Netapi32.class);
int NetUserChangePassword(
WString domainname, // LPCWSTR optional
WString username, // LPCWSTR optional
WString oldpassword, // LPCWSTR
WString newpassword // LPCWSTR
);
}@[Link("netapi32")]
lib LibNETAPI32
fun NetUserChangePassword = NetUserChangePassword(
domainname : UInt16*, # LPCWSTR optional
username : UInt16*, # LPCWSTR optional
oldpassword : UInt16*, # LPCWSTR
newpassword : UInt16* # LPCWSTR
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef NetUserChangePasswordNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef NetUserChangePasswordDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final NetUserChangePassword = DynamicLibrary.open('NETAPI32.dll')
.lookupFunction<NetUserChangePasswordNative, NetUserChangePasswordDart>('NetUserChangePassword');
// domainname : LPCWSTR optional -> Pointer<Utf16>
// username : LPCWSTR optional -> Pointer<Utf16>
// oldpassword : LPCWSTR -> Pointer<Utf16>
// newpassword : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function NetUserChangePassword(
domainname: PWideChar; // LPCWSTR optional
username: PWideChar; // LPCWSTR optional
oldpassword: PWideChar; // LPCWSTR
newpassword: PWideChar // LPCWSTR
): DWORD; stdcall;
external 'NETAPI32.dll' name 'NetUserChangePassword';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "NetUserChangePassword"
c_NetUserChangePassword :: CWString -> CWString -> CWString -> CWString -> IO Word32
-- domainname : LPCWSTR optional -> CWString
-- username : LPCWSTR optional -> CWString
-- oldpassword : LPCWSTR -> CWString
-- newpassword : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let netuserchangepassword =
foreign "NetUserChangePassword"
((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning uint32_t)
(* domainname : LPCWSTR optional -> (ptr uint16_t) *)
(* username : LPCWSTR optional -> (ptr uint16_t) *)
(* oldpassword : LPCWSTR -> (ptr uint16_t) *)
(* newpassword : LPCWSTR -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library netapi32 (t "NETAPI32.dll"))
(cffi:use-foreign-library netapi32)
(cffi:defcfun ("NetUserChangePassword" net-user-change-password :convention :stdcall) :uint32
(domainname (:string :encoding :utf-16le)) ; LPCWSTR optional
(username (:string :encoding :utf-16le)) ; LPCWSTR optional
(oldpassword (:string :encoding :utf-16le)) ; LPCWSTR
(newpassword (:string :encoding :utf-16le))) ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $NetUserChangePassword = Win32::API::More->new('NETAPI32',
'DWORD NetUserChangePassword(LPCWSTR domainname, LPCWSTR username, LPCWSTR oldpassword, LPCWSTR newpassword)');
# my $ret = $NetUserChangePassword->Call($domainname, $username, $oldpassword, $newpassword);
# domainname : LPCWSTR optional -> LPCWSTR
# username : LPCWSTR optional -> LPCWSTR
# oldpassword : LPCWSTR -> LPCWSTR
# newpassword : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f NetUserGetInfo — 指定したユーザーアカウントの情報を取得する。
- f NetUserSetInfo — 指定したユーザーアカウントの情報を設定する。