Win32 API 日本語リファレンス
ホームSystem.Registry › RegConnectRegistryW

RegConnectRegistryW

関数
リモートコンピューターの定義済みレジストリキーに接続する。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR RegConnectRegistryW(
    LPCWSTR lpMachineName,   // optional
    HKEY hKey,
    HKEY* phkResult
);

パラメーター

名前方向説明
lpMachineNameLPCWSTRinoptional

リモート コンピューターの名前。文字列は次の形式です。

\\computername

呼び出し元はリモート コンピューターへのアクセス権を持っている必要があり、そうでない場合この関数は失敗します。

このパラメーターが NULL の場合、ローカル コンピューター名が使用されます。

hKeyHKEYin

定義済みのレジストリ ハンドル。このパラメーターには、リモート コンピューター上の次の predefined keys(定義済みキー)のいずれかを指定できます。

HKEY_LOCAL_MACHINE HKEY_PERFORMANCE_DATA HKEY_USERS

phkResultHKEY*outリモート コンピューター上の定義済みハンドルを識別するキー ハンドルを受け取る変数へのポインター。

戻り値の型: WIN32_ERROR

公式ドキュメント

別のコンピューター上の定義済みレジストリ キーへの接続を確立します。(Unicode)

戻り値

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

関数が失敗した場合、戻り値は Winerror.h で定義されている 0 以外のエラー コードです。 FormatMessage 関数を FORMAT_MESSAGE_FROM_SYSTEM フラグとともに使用すると、エラーの一般的な説明を取得できます。

解説(Remarks)

RegConnectRegistry を使用するには、リモート コンピューターで Remote Registry サービスが実行されている必要があります。既定では、このサービスは手動で開始するように構成されています。Remote Registry サービスが自動的に開始されるように構成するには、Services.msc を実行し、サービスのスタートアップの種類を自動に変更します。

Windows Server 2003 および Windows XP/2000: Remote Registry サービスは、既定で自動的に開始するように構成されています。

RegConnectRegistry から返されたハンドルが不要になったら、 RegCloseKey を呼び出して閉じる必要があります。

コンピューターがワークグループに参加しており、「ローカル アカウントによるネットワーク ログオンを Guest として認証する」ポリシーが有効になっている場合、この関数は失敗します。コンピューターがワークグループに参加している場合、このポリシーは既定で有効になっている点に注意してください。

現在のユーザーがリモート コンピューターへの適切なアクセス権を持っていない場合、RegConnectRegistry の呼び出しは失敗します。リモート レジストリに接続するには、RegConnectRegistry を呼び出す前に、LOGON32_LOGON_NEW_CREDENTIALS を指定して LogonUser を呼び出し、さらに ImpersonateLoggedOnUser を呼び出します。

Windows 2000: 考えられる回避策の 1 つは、別の資格情報のセットを使用して IPC$ などの管理共有へのセッションを確立することです。現在のユーザー以外の資格情報を指定するには、WNetAddConnection2 関数を使用して共有に接続します。レジストリへのアクセスが終了したら、接続をキャンセルします。

Windows XP Home Edition: この関数を使用して、Windows XP Home Edition を実行しているリモート コンピューターに接続することはできません。この関数は、認証レイヤーをバイパスするため、たとえローカル コンピューターが Windows XP Home Edition を実行している場合でも、ローカル コンピューター名に対しては動作します。

メモ

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

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

各言語での呼び出し定義

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

WIN32_ERROR RegConnectRegistryW(
    LPCWSTR lpMachineName,   // optional
    HKEY hKey,
    HKEY* phkResult
);
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegConnectRegistryW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpMachineName,   // LPCWSTR optional
    IntPtr hKey,   // HKEY
    IntPtr phkResult   // HKEY* out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegConnectRegistryW(
    <MarshalAs(UnmanagedType.LPWStr)> lpMachineName As String,   ' LPCWSTR optional
    hKey As IntPtr,   ' HKEY
    phkResult As IntPtr   ' HKEY* out
) As UInteger
End Function
' lpMachineName : LPCWSTR optional
' hKey : HKEY
' phkResult : HKEY* out
Declare PtrSafe Function RegConnectRegistryW Lib "advapi32" ( _
    ByVal lpMachineName As LongPtr, _
    ByVal hKey As LongPtr, _
    ByVal phkResult 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

RegConnectRegistryW = ctypes.windll.advapi32.RegConnectRegistryW
RegConnectRegistryW.restype = wintypes.DWORD
RegConnectRegistryW.argtypes = [
    wintypes.LPCWSTR,  # lpMachineName : LPCWSTR optional
    wintypes.HANDLE,  # hKey : HKEY
    ctypes.c_void_p,  # phkResult : HKEY* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
RegConnectRegistryW = Fiddle::Function.new(
  lib['RegConnectRegistryW'],
  [
    Fiddle::TYPE_VOIDP,  # lpMachineName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # hKey : HKEY
    Fiddle::TYPE_VOIDP,  # phkResult : HKEY* out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn RegConnectRegistryW(
        lpMachineName: *const u16,  // LPCWSTR optional
        hKey: *mut core::ffi::c_void,  // HKEY
        phkResult: *mut *mut core::ffi::c_void  // HKEY* out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode)]
public static extern uint RegConnectRegistryW([MarshalAs(UnmanagedType.LPWStr)] string lpMachineName, IntPtr hKey, IntPtr phkResult);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegConnectRegistryW' -Namespace Win32 -PassThru
# $api::RegConnectRegistryW(lpMachineName, hKey, phkResult)
#uselib "ADVAPI32.dll"
#func global RegConnectRegistryW "RegConnectRegistryW" wptr, wptr, wptr
; RegConnectRegistryW lpMachineName, hKey, phkResult   ; 戻り値は stat
; lpMachineName : LPCWSTR optional -> "wptr"
; hKey : HKEY -> "wptr"
; phkResult : HKEY* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ADVAPI32.dll"
#cfunc global RegConnectRegistryW "RegConnectRegistryW" wstr, sptr, sptr
; res = RegConnectRegistryW(lpMachineName, hKey, phkResult)
; lpMachineName : LPCWSTR optional -> "wstr"
; hKey : HKEY -> "sptr"
; phkResult : HKEY* out -> "sptr"
; WIN32_ERROR RegConnectRegistryW(LPCWSTR lpMachineName, HKEY hKey, HKEY* phkResult)
#uselib "ADVAPI32.dll"
#cfunc global RegConnectRegistryW "RegConnectRegistryW" wstr, intptr, intptr
; res = RegConnectRegistryW(lpMachineName, hKey, phkResult)
; lpMachineName : LPCWSTR optional -> "wstr"
; hKey : HKEY -> "intptr"
; phkResult : HKEY* out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procRegConnectRegistryW = advapi32.NewProc("RegConnectRegistryW")
)

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

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

typedef RegConnectRegistryWNative = Uint32 Function(Pointer<Utf16>, Pointer<Void>, Pointer<Void>);
typedef RegConnectRegistryWDart = int Function(Pointer<Utf16>, Pointer<Void>, Pointer<Void>);
final RegConnectRegistryW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<RegConnectRegistryWNative, RegConnectRegistryWDart>('RegConnectRegistryW');
// lpMachineName : LPCWSTR optional -> Pointer<Utf16>
// hKey : HKEY -> Pointer<Void>
// phkResult : HKEY* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function RegConnectRegistryW(
  lpMachineName: PWideChar;   // LPCWSTR optional
  hKey: THandle;   // HKEY
  phkResult: Pointer   // HKEY* out
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegConnectRegistryW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "RegConnectRegistryW"
  c_RegConnectRegistryW :: CWString -> Ptr () -> Ptr () -> IO Word32
-- lpMachineName : LPCWSTR optional -> CWString
-- hKey : HKEY -> Ptr ()
-- phkResult : HKEY* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let regconnectregistryw =
  foreign "RegConnectRegistryW"
    ((ptr uint16_t) @-> (ptr void) @-> (ptr void) @-> returning uint32_t)
(* lpMachineName : LPCWSTR optional -> (ptr uint16_t) *)
(* hKey : HKEY -> (ptr void) *)
(* phkResult : HKEY* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("RegConnectRegistryW" reg-connect-registry-w :convention :stdcall) :uint32
  (lp-machine-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (h-key :pointer)   ; HKEY
  (phk-result :pointer))   ; HKEY* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RegConnectRegistryW = Win32::API::More->new('ADVAPI32',
    'DWORD RegConnectRegistryW(LPCWSTR lpMachineName, HANDLE hKey, HANDLE phkResult)');
# my $ret = $RegConnectRegistryW->Call($lpMachineName, $hKey, $phkResult);
# lpMachineName : LPCWSTR optional -> LPCWSTR
# hKey : HKEY -> HANDLE
# phkResult : HKEY* out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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