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

AssocQueryKeyW

関数
ファイル関連付けに対応するレジストリキーを取得する。
DLLSHLWAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

HRESULT AssocQueryKeyW(
    ASSOCF flags,
    ASSOCKEY key,
    LPCWSTR pszAssoc,
    LPCWSTR pszExtra,   // optional
    HKEY* phkeyOut
);

パラメーター

名前方向
flagsASSOCFin
keyASSOCKEYin
pszAssocLPCWSTRin
pszExtraLPCWSTRinoptional
phkeyOutHKEY*out

戻り値の型: HRESULT

各言語での呼び出し定義

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

HRESULT AssocQueryKeyW(
    ASSOCF flags,
    ASSOCKEY key,
    LPCWSTR pszAssoc,
    LPCWSTR pszExtra,   // optional
    HKEY* phkeyOut
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int AssocQueryKeyW(
    uint flags,   // ASSOCF
    int key,   // ASSOCKEY
    [MarshalAs(UnmanagedType.LPWStr)] string pszAssoc,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string pszExtra,   // LPCWSTR optional
    IntPtr phkeyOut   // HKEY* out
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function AssocQueryKeyW(
    flags As UInteger,   ' ASSOCF
    key As Integer,   ' ASSOCKEY
    <MarshalAs(UnmanagedType.LPWStr)> pszAssoc As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> pszExtra As String,   ' LPCWSTR optional
    phkeyOut As IntPtr   ' HKEY* out
) As Integer
End Function
' flags : ASSOCF
' key : ASSOCKEY
' pszAssoc : LPCWSTR
' pszExtra : LPCWSTR optional
' phkeyOut : HKEY* out
Declare PtrSafe Function AssocQueryKeyW Lib "shlwapi" ( _
    ByVal flags As Long, _
    ByVal key As Long, _
    ByVal pszAssoc As LongPtr, _
    ByVal pszExtra As LongPtr, _
    ByVal phkeyOut 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

AssocQueryKeyW = ctypes.windll.shlwapi.AssocQueryKeyW
AssocQueryKeyW.restype = ctypes.c_int
AssocQueryKeyW.argtypes = [
    wintypes.DWORD,  # flags : ASSOCF
    ctypes.c_int,  # key : ASSOCKEY
    wintypes.LPCWSTR,  # pszAssoc : LPCWSTR
    wintypes.LPCWSTR,  # pszExtra : LPCWSTR optional
    ctypes.c_void_p,  # phkeyOut : HKEY* out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procAssocQueryKeyW = shlwapi.NewProc("AssocQueryKeyW")
)

// flags (ASSOCF), key (ASSOCKEY), pszAssoc (LPCWSTR), pszExtra (LPCWSTR optional), phkeyOut (HKEY* out)
r1, _, err := procAssocQueryKeyW.Call(
	uintptr(flags),
	uintptr(key),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszAssoc))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszExtra))),
	uintptr(phkeyOut),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function AssocQueryKeyW(
  flags: DWORD;   // ASSOCF
  key: Integer;   // ASSOCKEY
  pszAssoc: PWideChar;   // LPCWSTR
  pszExtra: PWideChar;   // LPCWSTR optional
  phkeyOut: Pointer   // HKEY* out
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'AssocQueryKeyW';
result := DllCall("SHLWAPI\AssocQueryKeyW"
    , "UInt", flags   ; ASSOCF
    , "Int", key   ; ASSOCKEY
    , "WStr", pszAssoc   ; LPCWSTR
    , "WStr", pszExtra   ; LPCWSTR optional
    , "Ptr", phkeyOut   ; HKEY* out
    , "Int")   ; return: HRESULT
●AssocQueryKeyW(flags, key, pszAssoc, pszExtra, phkeyOut) = DLL("SHLWAPI.dll", "int AssocQueryKeyW(dword, int, char*, char*, void*)")
# 呼び出し: AssocQueryKeyW(flags, key, pszAssoc, pszExtra, phkeyOut)
# flags : ASSOCF -> "dword"
# key : ASSOCKEY -> "int"
# pszAssoc : LPCWSTR -> "char*"
# pszExtra : LPCWSTR optional -> "char*"
# phkeyOut : HKEY* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。