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

DeleteProfileW

関数
指定ユーザーのプロファイルを削除する(Unicode版)。
DLLUSERENV.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL DeleteProfileW(
    LPCWSTR lpSidString,
    LPCWSTR lpProfilePath,   // optional
    LPCWSTR lpComputerName   // optional
);

パラメーター

名前方向説明
lpSidStringLPCWSTRinユーザーの SID を指定する文字列へのポインター。
lpProfilePathLPCWSTRinoptionalプロファイルパスを指定する文字列へのポインター。このパラメーターが NULL の場合、関数はレジストリからパスを取得します。
lpComputerNameLPCWSTRinoptional

プロファイルを削除する対象のコンピューター名を指定する文字列へのポインター。このパラメーターが NULL の場合、ローカルコンピューター名が使用されます。

注意 Windows Vista 以降では、このパラメーターは NULL でなければなりません。NULL でない場合、この関数はエラーコード ERROR_INVALID_PARAMETER で失敗します。

戻り値の型: BOOL

公式ドキュメント

指定したコンピューターから、ユーザープロファイルおよびユーザー関連のすべての設定を削除します。ユーザーのプロファイルを削除するには、呼び出し元が管理者権限を持っている必要があります。(Unicode)

戻り値

型: BOOL

成功した場合は TRUE、それ以外の場合は FALSE。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

DeleteProfile は、ローカルシステムアカウント (S-1-5-18) のセキュリティ識別子 (SID) を渡した場合に失敗することがあります。

メモ

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

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

各言語での呼び出し定義

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

BOOL DeleteProfileW(
    LPCWSTR lpSidString,
    LPCWSTR lpProfilePath,   // optional
    LPCWSTR lpComputerName   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USERENV.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool DeleteProfileW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpSidString,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string lpProfilePath,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpComputerName   // LPCWSTR optional
);
<DllImport("USERENV.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function DeleteProfileW(
    <MarshalAs(UnmanagedType.LPWStr)> lpSidString As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> lpProfilePath As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpComputerName As String   ' LPCWSTR optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpSidString : LPCWSTR
' lpProfilePath : LPCWSTR optional
' lpComputerName : LPCWSTR optional
Declare PtrSafe Function DeleteProfileW Lib "userenv" ( _
    ByVal lpSidString As LongPtr, _
    ByVal lpProfilePath As LongPtr, _
    ByVal lpComputerName 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

DeleteProfileW = ctypes.windll.userenv.DeleteProfileW
DeleteProfileW.restype = wintypes.BOOL
DeleteProfileW.argtypes = [
    wintypes.LPCWSTR,  # lpSidString : LPCWSTR
    wintypes.LPCWSTR,  # lpProfilePath : LPCWSTR optional
    wintypes.LPCWSTR,  # lpComputerName : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USERENV.dll')
DeleteProfileW = Fiddle::Function.new(
  lib['DeleteProfileW'],
  [
    Fiddle::TYPE_VOIDP,  # lpSidString : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpProfilePath : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpComputerName : LPCWSTR optional
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "userenv")]
extern "system" {
    fn DeleteProfileW(
        lpSidString: *const u16,  // LPCWSTR
        lpProfilePath: *const u16,  // LPCWSTR optional
        lpComputerName: *const u16  // LPCWSTR optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USERENV.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool DeleteProfileW([MarshalAs(UnmanagedType.LPWStr)] string lpSidString, [MarshalAs(UnmanagedType.LPWStr)] string lpProfilePath, [MarshalAs(UnmanagedType.LPWStr)] string lpComputerName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USERENV_DeleteProfileW' -Namespace Win32 -PassThru
# $api::DeleteProfileW(lpSidString, lpProfilePath, lpComputerName)
#uselib "USERENV.dll"
#func global DeleteProfileW "DeleteProfileW" wptr, wptr, wptr
; DeleteProfileW lpSidString, lpProfilePath, lpComputerName   ; 戻り値は stat
; lpSidString : LPCWSTR -> "wptr"
; lpProfilePath : LPCWSTR optional -> "wptr"
; lpComputerName : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USERENV.dll"
#cfunc global DeleteProfileW "DeleteProfileW" wstr, wstr, wstr
; res = DeleteProfileW(lpSidString, lpProfilePath, lpComputerName)
; lpSidString : LPCWSTR -> "wstr"
; lpProfilePath : LPCWSTR optional -> "wstr"
; lpComputerName : LPCWSTR optional -> "wstr"
; BOOL DeleteProfileW(LPCWSTR lpSidString, LPCWSTR lpProfilePath, LPCWSTR lpComputerName)
#uselib "USERENV.dll"
#cfunc global DeleteProfileW "DeleteProfileW" wstr, wstr, wstr
; res = DeleteProfileW(lpSidString, lpProfilePath, lpComputerName)
; lpSidString : LPCWSTR -> "wstr"
; lpProfilePath : LPCWSTR optional -> "wstr"
; lpComputerName : LPCWSTR optional -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	userenv = windows.NewLazySystemDLL("USERENV.dll")
	procDeleteProfileW = userenv.NewProc("DeleteProfileW")
)

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

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

typedef DeleteProfileWNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef DeleteProfileWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final DeleteProfileW = DynamicLibrary.open('USERENV.dll')
    .lookupFunction<DeleteProfileWNative, DeleteProfileWDart>('DeleteProfileW');
// lpSidString : LPCWSTR -> Pointer<Utf16>
// lpProfilePath : LPCWSTR optional -> Pointer<Utf16>
// lpComputerName : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DeleteProfileW(
  lpSidString: PWideChar;   // LPCWSTR
  lpProfilePath: PWideChar;   // LPCWSTR optional
  lpComputerName: PWideChar   // LPCWSTR optional
): BOOL; stdcall;
  external 'USERENV.dll' name 'DeleteProfileW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DeleteProfileW"
  c_DeleteProfileW :: CWString -> CWString -> CWString -> IO CInt
-- lpSidString : LPCWSTR -> CWString
-- lpProfilePath : LPCWSTR optional -> CWString
-- lpComputerName : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let deleteprofilew =
  foreign "DeleteProfileW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning int32_t)
(* lpSidString : LPCWSTR -> (ptr uint16_t) *)
(* lpProfilePath : LPCWSTR optional -> (ptr uint16_t) *)
(* lpComputerName : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library userenv (t "USERENV.dll"))
(cffi:use-foreign-library userenv)

(cffi:defcfun ("DeleteProfileW" delete-profile-w :convention :stdcall) :int32
  (lp-sid-string (:string :encoding :utf-16le))   ; LPCWSTR
  (lp-profile-path (:string :encoding :utf-16le))   ; LPCWSTR optional
  (lp-computer-name (:string :encoding :utf-16le)))   ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DeleteProfileW = Win32::API::More->new('USERENV',
    'BOOL DeleteProfileW(LPCWSTR lpSidString, LPCWSTR lpProfilePath, LPCWSTR lpComputerName)');
# my $ret = $DeleteProfileW->Call($lpSidString, $lpProfilePath, $lpComputerName);
# lpSidString : LPCWSTR -> LPCWSTR
# lpProfilePath : LPCWSTR optional -> LPCWSTR
# lpComputerName : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い