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

SHValidateUNC

関数
UNCパスの妥当性を検証し必要なら接続する。
DLLSHELL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

BOOL SHValidateUNC(
    HWND hwndOwner,   // optional
    LPWSTR pszFile,
    DWORD fConnect
);

パラメーター

名前方向説明
hwndOwnerHWNDinoptional親ウィンドウのハンドルで、UI の表示に使用されます。これが不要な場合は、この値に NULL を設定できます。
pszFileLPWSTRinout検証対象の UNC パスを指定する、null で終わる Unicode 文字列へのポインターです。注意: この文字列は定数文字列であってはなりません。
fConnectDWORDin

次の値のうち 1 つ以上を指定します。

VALIDATEUNC_CONNECT (0x0001)

ドライブ文字を割り当てて接続します。このフラグが設定されている場合、pszFile の値は、ローカルマシン上で UNC がマップされたローカルドライブに変更されます。

VALIDATEUNC_NOUI (0x0002)

失敗時でも成功時でも、UI を表示しません。

VALIDATEUNC_PRINT (0x0004)

ディスク共有ではなく、プリンター共有として検証します。

VALIDATEUNC_PERSIST (0x0008)

Windows Vista 以降。接続を永続的なものにします。

VALIDATEUNC_VALID

SHValidateUNC に渡されたフラグが有効であることを確認するために使用するマスク値です。

戻り値の型: BOOL

公式ドキュメント

SHValidateUNC は変更されるか、利用できなくなる可能性があります。

戻り値

型: BOOL

UNC パスが存在する場合は TRUE を返します。UNC パスが存在しない場合、またはその他の何らかの失敗が発生した場合は FALSE を返します。

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

各言語での呼び出し定義

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

BOOL SHValidateUNC(
    HWND hwndOwner,   // optional
    LPWSTR pszFile,
    DWORD fConnect
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHELL32.dll", ExactSpelling = true)]
static extern bool SHValidateUNC(
    IntPtr hwndOwner,   // HWND optional
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszFile,   // LPWSTR in/out
    uint fConnect   // DWORD
);
<DllImport("SHELL32.dll", ExactSpelling:=True)>
Public Shared Function SHValidateUNC(
    hwndOwner As IntPtr,   ' HWND optional
    <MarshalAs(UnmanagedType.LPWStr)> pszFile As System.Text.StringBuilder,   ' LPWSTR in/out
    fConnect As UInteger   ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hwndOwner : HWND optional
' pszFile : LPWSTR in/out
' fConnect : DWORD
Declare PtrSafe Function SHValidateUNC Lib "shell32" ( _
    ByVal hwndOwner As LongPtr, _
    ByVal pszFile As LongPtr, _
    ByVal fConnect As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SHValidateUNC = ctypes.windll.shell32.SHValidateUNC
SHValidateUNC.restype = wintypes.BOOL
SHValidateUNC.argtypes = [
    wintypes.HANDLE,  # hwndOwner : HWND optional
    wintypes.LPWSTR,  # pszFile : LPWSTR in/out
    wintypes.DWORD,  # fConnect : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
SHValidateUNC = Fiddle::Function.new(
  lib['SHValidateUNC'],
  [
    Fiddle::TYPE_VOIDP,  # hwndOwner : HWND optional
    Fiddle::TYPE_VOIDP,  # pszFile : LPWSTR in/out
    -Fiddle::TYPE_INT,  # fConnect : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "shell32")]
extern "system" {
    fn SHValidateUNC(
        hwndOwner: *mut core::ffi::c_void,  // HWND optional
        pszFile: *mut u16,  // LPWSTR in/out
        fConnect: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHELL32.dll")]
public static extern bool SHValidateUNC(IntPtr hwndOwner, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszFile, uint fConnect);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_SHValidateUNC' -Namespace Win32 -PassThru
# $api::SHValidateUNC(hwndOwner, pszFile, fConnect)
#uselib "SHELL32.dll"
#func global SHValidateUNC "SHValidateUNC" sptr, sptr, sptr
; SHValidateUNC hwndOwner, varptr(pszFile), fConnect   ; 戻り値は stat
; hwndOwner : HWND optional -> "sptr"
; pszFile : LPWSTR in/out -> "sptr"
; fConnect : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHELL32.dll"
#cfunc global SHValidateUNC "SHValidateUNC" sptr, var, int
; res = SHValidateUNC(hwndOwner, pszFile, fConnect)
; hwndOwner : HWND optional -> "sptr"
; pszFile : LPWSTR in/out -> "var"
; fConnect : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SHValidateUNC(HWND hwndOwner, LPWSTR pszFile, DWORD fConnect)
#uselib "SHELL32.dll"
#cfunc global SHValidateUNC "SHValidateUNC" intptr, var, int
; res = SHValidateUNC(hwndOwner, pszFile, fConnect)
; hwndOwner : HWND optional -> "intptr"
; pszFile : LPWSTR in/out -> "var"
; fConnect : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procSHValidateUNC = shell32.NewProc("SHValidateUNC")
)

// hwndOwner (HWND optional), pszFile (LPWSTR in/out), fConnect (DWORD)
r1, _, err := procSHValidateUNC.Call(
	uintptr(hwndOwner),
	uintptr(pszFile),
	uintptr(fConnect),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SHValidateUNC(
  hwndOwner: THandle;   // HWND optional
  pszFile: PWideChar;   // LPWSTR in/out
  fConnect: DWORD   // DWORD
): BOOL; stdcall;
  external 'SHELL32.dll' name 'SHValidateUNC';
result := DllCall("SHELL32\SHValidateUNC"
    , "Ptr", hwndOwner   ; HWND optional
    , "Ptr", pszFile   ; LPWSTR in/out
    , "UInt", fConnect   ; DWORD
    , "Int")   ; return: BOOL
●SHValidateUNC(hwndOwner, pszFile, fConnect) = DLL("SHELL32.dll", "bool SHValidateUNC(void*, char*, dword)")
# 呼び出し: SHValidateUNC(hwndOwner, pszFile, fConnect)
# hwndOwner : HWND optional -> "void*"
# pszFile : LPWSTR in/out -> "char*"
# fConnect : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shell32" fn SHValidateUNC(
    hwndOwner: ?*anyopaque, // HWND optional
    pszFile: [*c]u16, // LPWSTR in/out
    fConnect: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc SHValidateUNC(
    hwndOwner: pointer,  # HWND optional
    pszFile: ptr uint16,  # LPWSTR in/out
    fConnect: uint32  # DWORD
): int32 {.importc: "SHValidateUNC", stdcall, dynlib: "SHELL32.dll".}
pragma(lib, "shell32");
extern(Windows)
int SHValidateUNC(
    void* hwndOwner,   // HWND optional
    wchar* pszFile,   // LPWSTR in/out
    uint fConnect   // DWORD
);
ccall((:SHValidateUNC, "SHELL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{UInt16}, UInt32),
      hwndOwner, pszFile, fConnect)
# hwndOwner : HWND optional -> Ptr{Cvoid}
# pszFile : LPWSTR in/out -> Ptr{UInt16}
# fConnect : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SHValidateUNC(
    void* hwndOwner,
    uint16_t* pszFile,
    uint32_t fConnect);
]]
local shell32 = ffi.load("shell32")
-- shell32.SHValidateUNC(hwndOwner, pszFile, fConnect)
-- hwndOwner : HWND optional
-- pszFile : LPWSTR in/out
-- fConnect : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const SHValidateUNC = lib.func('__stdcall', 'SHValidateUNC', 'int32_t', ['void *', 'uint16_t *', 'uint32_t']);
// SHValidateUNC(hwndOwner, pszFile, fConnect)
// hwndOwner : HWND optional -> 'void *'
// pszFile : LPWSTR in/out -> 'uint16_t *'
// fConnect : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  SHValidateUNC: { parameters: ["pointer", "buffer", "u32"], result: "i32" },
});
// lib.symbols.SHValidateUNC(hwndOwner, pszFile, fConnect)
// hwndOwner : HWND optional -> "pointer"
// pszFile : LPWSTR in/out -> "buffer"
// fConnect : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SHValidateUNC(
    void* hwndOwner,
    uint16_t* pszFile,
    uint32_t fConnect);
C, "SHELL32.dll");
// $ffi->SHValidateUNC(hwndOwner, pszFile, fConnect);
// hwndOwner : HWND optional
// pszFile : LPWSTR in/out
// fConnect : 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 Shell32 extends StdCallLibrary {
    Shell32 INSTANCE = Native.load("shell32", Shell32.class);
    boolean SHValidateUNC(
        Pointer hwndOwner,   // HWND optional
        char[] pszFile,   // LPWSTR in/out
        int fConnect   // DWORD
    );
}
@[Link("shell32")]
lib LibSHELL32
  fun SHValidateUNC = SHValidateUNC(
    hwndOwner : Void*,   # HWND optional
    pszFile : UInt16*,   # LPWSTR in/out
    fConnect : 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 SHValidateUNCNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Uint32);
typedef SHValidateUNCDart = int Function(Pointer<Void>, Pointer<Utf16>, int);
final SHValidateUNC = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<SHValidateUNCNative, SHValidateUNCDart>('SHValidateUNC');
// hwndOwner : HWND optional -> Pointer<Void>
// pszFile : LPWSTR in/out -> Pointer<Utf16>
// fConnect : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SHValidateUNC(
  hwndOwner: THandle;   // HWND optional
  pszFile: PWideChar;   // LPWSTR in/out
  fConnect: DWORD   // DWORD
): BOOL; stdcall;
  external 'SHELL32.dll' name 'SHValidateUNC';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SHValidateUNC"
  c_SHValidateUNC :: Ptr () -> CWString -> Word32 -> IO CInt
-- hwndOwner : HWND optional -> Ptr ()
-- pszFile : LPWSTR in/out -> CWString
-- fConnect : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shvalidateunc =
  foreign "SHValidateUNC"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> returning int32_t)
(* hwndOwner : HWND optional -> (ptr void) *)
(* pszFile : LPWSTR in/out -> (ptr uint16_t) *)
(* fConnect : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("SHValidateUNC" shvalidate-unc :convention :stdcall) :int32
  (hwnd-owner :pointer)   ; HWND optional
  (psz-file :pointer)   ; LPWSTR in/out
  (f-connect :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHValidateUNC = Win32::API::More->new('SHELL32',
    'BOOL SHValidateUNC(HANDLE hwndOwner, LPWSTR pszFile, DWORD fConnect)');
# my $ret = $SHValidateUNC->Call($hwndOwner, $pszFile, $fConnect);
# hwndOwner : HWND optional -> HANDLE
# pszFile : LPWSTR in/out -> LPWSTR
# fConnect : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。