Win32 API 日本語リファレンス
ホームUI.Shell › CreateProfile

CreateProfile

関数
指定したユーザー用の新しいプロファイルを作成する。
DLLUSERENV.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

HRESULT CreateProfile(
    LPCWSTR pszUserSid,
    LPCWSTR pszUserName,
    LPWSTR pszProfilePath,
    DWORD cchProfilePath
);

パラメーター

名前方向説明
pszUserSidLPCWSTRinユーザーの SID を文字列として表したものへのポインター。
pszUserNameLPCWSTRin新しいユーザーのユーザー名。この名前はプロファイルディレクトリのベース名として使用されます。
pszProfilePathLPWSTRoutこの関数が返るときに、プロファイルのフルパスへのポインターを格納します。
cchProfilePathDWORDinpszProfilePath が指すバッファーのサイズ(文字数単位)。

戻り値の型: HRESULT

公式ドキュメント

新しいユーザープロファイルを作成します。

戻り値

型: HRESULT

成功した場合は S_OK を返し、それ以外の場合は以下を含むエラー値を返します。

戻り値 説明
E_ACCESSDENIED
呼び出し元にプロファイルを作成するための十分な権限レベルがありません。
HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS)
指定されたユーザーのプロファイルが既に存在します。

解説(Remarks)

この関数を呼び出すには、呼び出し元が管理者特権を持っている必要があります。

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

各言語での呼び出し定義

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

HRESULT CreateProfile(
    LPCWSTR pszUserSid,
    LPCWSTR pszUserName,
    LPWSTR pszProfilePath,
    DWORD cchProfilePath
);
[DllImport("USERENV.dll", ExactSpelling = true)]
static extern int CreateProfile(
    [MarshalAs(UnmanagedType.LPWStr)] string pszUserSid,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string pszUserName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszProfilePath,   // LPWSTR out
    uint cchProfilePath   // DWORD
);
<DllImport("USERENV.dll", ExactSpelling:=True)>
Public Shared Function CreateProfile(
    <MarshalAs(UnmanagedType.LPWStr)> pszUserSid As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> pszUserName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> pszProfilePath As System.Text.StringBuilder,   ' LPWSTR out
    cchProfilePath As UInteger   ' DWORD
) As Integer
End Function
' pszUserSid : LPCWSTR
' pszUserName : LPCWSTR
' pszProfilePath : LPWSTR out
' cchProfilePath : DWORD
Declare PtrSafe Function CreateProfile Lib "userenv" ( _
    ByVal pszUserSid As LongPtr, _
    ByVal pszUserName As LongPtr, _
    ByVal pszProfilePath As LongPtr, _
    ByVal cchProfilePath As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateProfile = ctypes.windll.userenv.CreateProfile
CreateProfile.restype = ctypes.c_int
CreateProfile.argtypes = [
    wintypes.LPCWSTR,  # pszUserSid : LPCWSTR
    wintypes.LPCWSTR,  # pszUserName : LPCWSTR
    wintypes.LPWSTR,  # pszProfilePath : LPWSTR out
    wintypes.DWORD,  # cchProfilePath : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USERENV.dll')
CreateProfile = Fiddle::Function.new(
  lib['CreateProfile'],
  [
    Fiddle::TYPE_VOIDP,  # pszUserSid : LPCWSTR
    Fiddle::TYPE_VOIDP,  # pszUserName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # pszProfilePath : LPWSTR out
    -Fiddle::TYPE_INT,  # cchProfilePath : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "userenv")]
extern "system" {
    fn CreateProfile(
        pszUserSid: *const u16,  // LPCWSTR
        pszUserName: *const u16,  // LPCWSTR
        pszProfilePath: *mut u16,  // LPWSTR out
        cchProfilePath: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USERENV.dll")]
public static extern int CreateProfile([MarshalAs(UnmanagedType.LPWStr)] string pszUserSid, [MarshalAs(UnmanagedType.LPWStr)] string pszUserName, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszProfilePath, uint cchProfilePath);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USERENV_CreateProfile' -Namespace Win32 -PassThru
# $api::CreateProfile(pszUserSid, pszUserName, pszProfilePath, cchProfilePath)
#uselib "USERENV.dll"
#func global CreateProfile "CreateProfile" sptr, sptr, sptr, sptr
; CreateProfile pszUserSid, pszUserName, varptr(pszProfilePath), cchProfilePath   ; 戻り値は stat
; pszUserSid : LPCWSTR -> "sptr"
; pszUserName : LPCWSTR -> "sptr"
; pszProfilePath : LPWSTR out -> "sptr"
; cchProfilePath : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USERENV.dll"
#cfunc global CreateProfile "CreateProfile" wstr, wstr, var, int
; res = CreateProfile(pszUserSid, pszUserName, pszProfilePath, cchProfilePath)
; pszUserSid : LPCWSTR -> "wstr"
; pszUserName : LPCWSTR -> "wstr"
; pszProfilePath : LPWSTR out -> "var"
; cchProfilePath : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT CreateProfile(LPCWSTR pszUserSid, LPCWSTR pszUserName, LPWSTR pszProfilePath, DWORD cchProfilePath)
#uselib "USERENV.dll"
#cfunc global CreateProfile "CreateProfile" wstr, wstr, var, int
; res = CreateProfile(pszUserSid, pszUserName, pszProfilePath, cchProfilePath)
; pszUserSid : LPCWSTR -> "wstr"
; pszUserName : LPCWSTR -> "wstr"
; pszProfilePath : LPWSTR out -> "var"
; cchProfilePath : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	userenv = windows.NewLazySystemDLL("USERENV.dll")
	procCreateProfile = userenv.NewProc("CreateProfile")
)

// pszUserSid (LPCWSTR), pszUserName (LPCWSTR), pszProfilePath (LPWSTR out), cchProfilePath (DWORD)
r1, _, err := procCreateProfile.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszUserSid))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszUserName))),
	uintptr(pszProfilePath),
	uintptr(cchProfilePath),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function CreateProfile(
  pszUserSid: PWideChar;   // LPCWSTR
  pszUserName: PWideChar;   // LPCWSTR
  pszProfilePath: PWideChar;   // LPWSTR out
  cchProfilePath: DWORD   // DWORD
): Integer; stdcall;
  external 'USERENV.dll' name 'CreateProfile';
result := DllCall("USERENV\CreateProfile"
    , "WStr", pszUserSid   ; LPCWSTR
    , "WStr", pszUserName   ; LPCWSTR
    , "Ptr", pszProfilePath   ; LPWSTR out
    , "UInt", cchProfilePath   ; DWORD
    , "Int")   ; return: HRESULT
●CreateProfile(pszUserSid, pszUserName, pszProfilePath, cchProfilePath) = DLL("USERENV.dll", "int CreateProfile(char*, char*, char*, dword)")
# 呼び出し: CreateProfile(pszUserSid, pszUserName, pszProfilePath, cchProfilePath)
# pszUserSid : LPCWSTR -> "char*"
# pszUserName : LPCWSTR -> "char*"
# pszProfilePath : LPWSTR out -> "char*"
# cchProfilePath : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef CreateProfileNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Uint32);
typedef CreateProfileDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int);
final CreateProfile = DynamicLibrary.open('USERENV.dll')
    .lookupFunction<CreateProfileNative, CreateProfileDart>('CreateProfile');
// pszUserSid : LPCWSTR -> Pointer<Utf16>
// pszUserName : LPCWSTR -> Pointer<Utf16>
// pszProfilePath : LPWSTR out -> Pointer<Utf16>
// cchProfilePath : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateProfile(
  pszUserSid: PWideChar;   // LPCWSTR
  pszUserName: PWideChar;   // LPCWSTR
  pszProfilePath: PWideChar;   // LPWSTR out
  cchProfilePath: DWORD   // DWORD
): Integer; stdcall;
  external 'USERENV.dll' name 'CreateProfile';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateProfile"
  c_CreateProfile :: CWString -> CWString -> CWString -> Word32 -> IO Int32
-- pszUserSid : LPCWSTR -> CWString
-- pszUserName : LPCWSTR -> CWString
-- pszProfilePath : LPWSTR out -> CWString
-- cchProfilePath : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let createprofile =
  foreign "CreateProfile"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> returning int32_t)
(* pszUserSid : LPCWSTR -> (ptr uint16_t) *)
(* pszUserName : LPCWSTR -> (ptr uint16_t) *)
(* pszProfilePath : LPWSTR out -> (ptr uint16_t) *)
(* cchProfilePath : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library userenv (t "USERENV.dll"))
(cffi:use-foreign-library userenv)

(cffi:defcfun ("CreateProfile" create-profile :convention :stdcall) :int32
  (psz-user-sid (:string :encoding :utf-16le))   ; LPCWSTR
  (psz-user-name (:string :encoding :utf-16le))   ; LPCWSTR
  (psz-profile-path :pointer)   ; LPWSTR out
  (cch-profile-path :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateProfile = Win32::API::More->new('USERENV',
    'int CreateProfile(LPCWSTR pszUserSid, LPCWSTR pszUserName, LPWSTR pszProfilePath, DWORD cchProfilePath)');
# my $ret = $CreateProfile->Call($pszUserSid, $pszUserName, $pszProfilePath, $cchProfilePath);
# pszUserSid : LPCWSTR -> LPCWSTR
# pszUserName : LPCWSTR -> LPCWSTR
# pszProfilePath : LPWSTR out -> LPWSTR
# cchProfilePath : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。