Win32 API 日本語リファレンス
ホームStorage.FileSystem › SetUserFileEncryptionKey

SetUserFileEncryptionKey

関数
ファイル暗号化に使うユーザーの証明書鍵を設定する。
DLLADVAPI32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

DWORD SetUserFileEncryptionKey(
    ENCRYPTION_CERTIFICATE* pEncryptionCertificate   // optional
);

パラメーター

名前方向説明
pEncryptionCertificateENCRYPTION_CERTIFICATE*inoptionalユーザーのキーとなる証明書へのポインターです。このパラメーターは ENCRYPTION_CERTIFICATE 構造体へのポインターです。

戻り値の型: DWORD

公式ドキュメント

ユーザーの現在のキーを指定した証明書に設定します。

戻り値

関数が成功した場合、戻り値は ERROR_SUCCESS です。

関数が失敗した場合、戻り値はシステムエラーコードです。エラーコードの完全な一覧については、 System Error Codes またはヘッダーファイル WinError.h を参照してください。

解説(Remarks)

Windows 8 および Windows Server 2012 では、この関数は次の技術でサポートされています。

技術 サポート状況
Server Message Block (SMB) 3.0 プロトコル はい
SMB 3.0 Transparent Failover (TFO) いいえ
SMB 3.0 with Scale-out File Shares (SO) いいえ
Cluster Shared Volume File System (CsvFS) いいえ
Resilient File System (ReFS) いいえ

SMB 3.0 は、継続的可用性 (continuous availability) 機能を持つ共有上での EFS をサポートしません。

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

各言語での呼び出し定義

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

DWORD SetUserFileEncryptionKey(
    ENCRYPTION_CERTIFICATE* pEncryptionCertificate   // optional
);
[DllImport("ADVAPI32.dll", ExactSpelling = true)]
static extern uint SetUserFileEncryptionKey(
    IntPtr pEncryptionCertificate   // ENCRYPTION_CERTIFICATE* optional
);
<DllImport("ADVAPI32.dll", ExactSpelling:=True)>
Public Shared Function SetUserFileEncryptionKey(
    pEncryptionCertificate As IntPtr   ' ENCRYPTION_CERTIFICATE* optional
) As UInteger
End Function
' pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional
Declare PtrSafe Function SetUserFileEncryptionKey Lib "advapi32" ( _
    ByVal pEncryptionCertificate As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetUserFileEncryptionKey = ctypes.windll.advapi32.SetUserFileEncryptionKey
SetUserFileEncryptionKey.restype = wintypes.DWORD
SetUserFileEncryptionKey.argtypes = [
    ctypes.c_void_p,  # pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
SetUserFileEncryptionKey = Fiddle::Function.new(
  lib['SetUserFileEncryptionKey'],
  [
    Fiddle::TYPE_VOIDP,  # pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional
  ],
  -Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn SetUserFileEncryptionKey(
        pEncryptionCertificate: *mut ENCRYPTION_CERTIFICATE  // ENCRYPTION_CERTIFICATE* optional
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ADVAPI32.dll")]
public static extern uint SetUserFileEncryptionKey(IntPtr pEncryptionCertificate);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_SetUserFileEncryptionKey' -Namespace Win32 -PassThru
# $api::SetUserFileEncryptionKey(pEncryptionCertificate)
#uselib "ADVAPI32.dll"
#func global SetUserFileEncryptionKey "SetUserFileEncryptionKey" sptr
; SetUserFileEncryptionKey varptr(pEncryptionCertificate)   ; 戻り値は stat
; pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global SetUserFileEncryptionKey "SetUserFileEncryptionKey" var
; res = SetUserFileEncryptionKey(pEncryptionCertificate)
; pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD SetUserFileEncryptionKey(ENCRYPTION_CERTIFICATE* pEncryptionCertificate)
#uselib "ADVAPI32.dll"
#cfunc global SetUserFileEncryptionKey "SetUserFileEncryptionKey" var
; res = SetUserFileEncryptionKey(pEncryptionCertificate)
; pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procSetUserFileEncryptionKey = advapi32.NewProc("SetUserFileEncryptionKey")
)

// pEncryptionCertificate (ENCRYPTION_CERTIFICATE* optional)
r1, _, err := procSetUserFileEncryptionKey.Call(
	uintptr(pEncryptionCertificate),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function SetUserFileEncryptionKey(
  pEncryptionCertificate: Pointer   // ENCRYPTION_CERTIFICATE* optional
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'SetUserFileEncryptionKey';
result := DllCall("ADVAPI32\SetUserFileEncryptionKey"
    , "Ptr", pEncryptionCertificate   ; ENCRYPTION_CERTIFICATE* optional
    , "UInt")   ; return: DWORD
●SetUserFileEncryptionKey(pEncryptionCertificate) = DLL("ADVAPI32.dll", "dword SetUserFileEncryptionKey(void*)")
# 呼び出し: SetUserFileEncryptionKey(pEncryptionCertificate)
# pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef SetUserFileEncryptionKeyNative = Uint32 Function(Pointer<Void>);
typedef SetUserFileEncryptionKeyDart = int Function(Pointer<Void>);
final SetUserFileEncryptionKey = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<SetUserFileEncryptionKeyNative, SetUserFileEncryptionKeyDart>('SetUserFileEncryptionKey');
// pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetUserFileEncryptionKey(
  pEncryptionCertificate: Pointer   // ENCRYPTION_CERTIFICATE* optional
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'SetUserFileEncryptionKey';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetUserFileEncryptionKey"
  c_SetUserFileEncryptionKey :: Ptr () -> IO Word32
-- pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setuserfileencryptionkey =
  foreign "SetUserFileEncryptionKey"
    ((ptr void) @-> returning uint32_t)
(* pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("SetUserFileEncryptionKey" set-user-file-encryption-key :convention :stdcall) :uint32
  (p-encryption-certificate :pointer))   ; ENCRYPTION_CERTIFICATE* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetUserFileEncryptionKey = Win32::API::More->new('ADVAPI32',
    'DWORD SetUserFileEncryptionKey(LPVOID pEncryptionCertificate)');
# my $ret = $SetUserFileEncryptionKey->Call($pEncryptionCertificate);
# pEncryptionCertificate : ENCRYPTION_CERTIFICATE* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
使用する型