Win32 API 日本語リファレンス
ホームNetworkManagement.WebDav › DavAddConnection

DavAddConnection

関数
WebDAVサーバーへの接続を資格情報付きで確立する。
DLLNETAPI32.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

DWORD DavAddConnection(
    HANDLE* ConnectionHandle,
    LPCWSTR RemoteName,
    LPCWSTR UserName,   // optional
    LPCWSTR Password,   // optional
    BYTE* ClientCert,
    DWORD CertSize
);

パラメーター

名前方向説明
ConnectionHandleHANDLE*inout確立したWebDAV接続のハンドルを受け取る出力先。
RemoteNameLPCWSTRin接続先のリモート名(UNCパスまたはHTTP URL)。
UserNameLPCWSTRinoptional認証に使用するユーザー名。既定資格情報を使う場合はNULL可。
PasswordLPCWSTRinoptional認証に使用するパスワード。NULL可。
ClientCertBYTE*inクライアント証明書のバイト列バッファ。不要ならNULL。
CertSizeDWORDinクライアント証明書バッファのサイズ(バイト数)。

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD DavAddConnection(
    HANDLE* ConnectionHandle,
    LPCWSTR RemoteName,
    LPCWSTR UserName,   // optional
    LPCWSTR Password,   // optional
    BYTE* ClientCert,
    DWORD CertSize
);
[DllImport("NETAPI32.dll", ExactSpelling = true)]
static extern uint DavAddConnection(
    IntPtr ConnectionHandle,   // HANDLE* in/out
    [MarshalAs(UnmanagedType.LPWStr)] string RemoteName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string UserName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string Password,   // LPCWSTR optional
    IntPtr ClientCert,   // BYTE*
    uint CertSize   // DWORD
);
<DllImport("NETAPI32.dll", ExactSpelling:=True)>
Public Shared Function DavAddConnection(
    ConnectionHandle As IntPtr,   ' HANDLE* in/out
    <MarshalAs(UnmanagedType.LPWStr)> RemoteName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> UserName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> Password As String,   ' LPCWSTR optional
    ClientCert As IntPtr,   ' BYTE*
    CertSize As UInteger   ' DWORD
) As UInteger
End Function
' ConnectionHandle : HANDLE* in/out
' RemoteName : LPCWSTR
' UserName : LPCWSTR optional
' Password : LPCWSTR optional
' ClientCert : BYTE*
' CertSize : DWORD
Declare PtrSafe Function DavAddConnection Lib "netapi32" ( _
    ByVal ConnectionHandle As LongPtr, _
    ByVal RemoteName As LongPtr, _
    ByVal UserName As LongPtr, _
    ByVal Password As LongPtr, _
    ByVal ClientCert As LongPtr, _
    ByVal CertSize As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

DavAddConnection = ctypes.windll.netapi32.DavAddConnection
DavAddConnection.restype = wintypes.DWORD
DavAddConnection.argtypes = [
    ctypes.c_void_p,  # ConnectionHandle : HANDLE* in/out
    wintypes.LPCWSTR,  # RemoteName : LPCWSTR
    wintypes.LPCWSTR,  # UserName : LPCWSTR optional
    wintypes.LPCWSTR,  # Password : LPCWSTR optional
    ctypes.POINTER(ctypes.c_ubyte),  # ClientCert : BYTE*
    wintypes.DWORD,  # CertSize : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('NETAPI32.dll')
DavAddConnection = Fiddle::Function.new(
  lib['DavAddConnection'],
  [
    Fiddle::TYPE_VOIDP,  # ConnectionHandle : HANDLE* in/out
    Fiddle::TYPE_VOIDP,  # RemoteName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # UserName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # Password : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # ClientCert : BYTE*
    -Fiddle::TYPE_INT,  # CertSize : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "netapi32")]
extern "system" {
    fn DavAddConnection(
        ConnectionHandle: *mut *mut core::ffi::c_void,  // HANDLE* in/out
        RemoteName: *const u16,  // LPCWSTR
        UserName: *const u16,  // LPCWSTR optional
        Password: *const u16,  // LPCWSTR optional
        ClientCert: *mut u8,  // BYTE*
        CertSize: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("NETAPI32.dll")]
public static extern uint DavAddConnection(IntPtr ConnectionHandle, [MarshalAs(UnmanagedType.LPWStr)] string RemoteName, [MarshalAs(UnmanagedType.LPWStr)] string UserName, [MarshalAs(UnmanagedType.LPWStr)] string Password, IntPtr ClientCert, uint CertSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NETAPI32_DavAddConnection' -Namespace Win32 -PassThru
# $api::DavAddConnection(ConnectionHandle, RemoteName, UserName, Password, ClientCert, CertSize)
#uselib "NETAPI32.dll"
#func global DavAddConnection "DavAddConnection" sptr, sptr, sptr, sptr, sptr, sptr
; DavAddConnection ConnectionHandle, RemoteName, UserName, Password, varptr(ClientCert), CertSize   ; 戻り値は stat
; ConnectionHandle : HANDLE* in/out -> "sptr"
; RemoteName : LPCWSTR -> "sptr"
; UserName : LPCWSTR optional -> "sptr"
; Password : LPCWSTR optional -> "sptr"
; ClientCert : BYTE* -> "sptr"
; CertSize : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "NETAPI32.dll"
#cfunc global DavAddConnection "DavAddConnection" sptr, wstr, wstr, wstr, var, int
; res = DavAddConnection(ConnectionHandle, RemoteName, UserName, Password, ClientCert, CertSize)
; ConnectionHandle : HANDLE* in/out -> "sptr"
; RemoteName : LPCWSTR -> "wstr"
; UserName : LPCWSTR optional -> "wstr"
; Password : LPCWSTR optional -> "wstr"
; ClientCert : BYTE* -> "var"
; CertSize : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD DavAddConnection(HANDLE* ConnectionHandle, LPCWSTR RemoteName, LPCWSTR UserName, LPCWSTR Password, BYTE* ClientCert, DWORD CertSize)
#uselib "NETAPI32.dll"
#cfunc global DavAddConnection "DavAddConnection" intptr, wstr, wstr, wstr, var, int
; res = DavAddConnection(ConnectionHandle, RemoteName, UserName, Password, ClientCert, CertSize)
; ConnectionHandle : HANDLE* in/out -> "intptr"
; RemoteName : LPCWSTR -> "wstr"
; UserName : LPCWSTR optional -> "wstr"
; Password : LPCWSTR optional -> "wstr"
; ClientCert : BYTE* -> "var"
; CertSize : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
	procDavAddConnection = netapi32.NewProc("DavAddConnection")
)

// ConnectionHandle (HANDLE* in/out), RemoteName (LPCWSTR), UserName (LPCWSTR optional), Password (LPCWSTR optional), ClientCert (BYTE*), CertSize (DWORD)
r1, _, err := procDavAddConnection.Call(
	uintptr(ConnectionHandle),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(RemoteName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(UserName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(Password))),
	uintptr(ClientCert),
	uintptr(CertSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function DavAddConnection(
  ConnectionHandle: Pointer;   // HANDLE* in/out
  RemoteName: PWideChar;   // LPCWSTR
  UserName: PWideChar;   // LPCWSTR optional
  Password: PWideChar;   // LPCWSTR optional
  ClientCert: Pointer;   // BYTE*
  CertSize: DWORD   // DWORD
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'DavAddConnection';
result := DllCall("NETAPI32\DavAddConnection"
    , "Ptr", ConnectionHandle   ; HANDLE* in/out
    , "WStr", RemoteName   ; LPCWSTR
    , "WStr", UserName   ; LPCWSTR optional
    , "WStr", Password   ; LPCWSTR optional
    , "Ptr", ClientCert   ; BYTE*
    , "UInt", CertSize   ; DWORD
    , "UInt")   ; return: DWORD
●DavAddConnection(ConnectionHandle, RemoteName, UserName, Password, ClientCert, CertSize) = DLL("NETAPI32.dll", "dword DavAddConnection(void*, char*, char*, char*, void*, dword)")
# 呼び出し: DavAddConnection(ConnectionHandle, RemoteName, UserName, Password, ClientCert, CertSize)
# ConnectionHandle : HANDLE* in/out -> "void*"
# RemoteName : LPCWSTR -> "char*"
# UserName : LPCWSTR optional -> "char*"
# Password : LPCWSTR optional -> "char*"
# ClientCert : BYTE* -> "void*"
# CertSize : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef DavAddConnectionNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint8>, Uint32);
typedef DavAddConnectionDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint8>, int);
final DavAddConnection = DynamicLibrary.open('NETAPI32.dll')
    .lookupFunction<DavAddConnectionNative, DavAddConnectionDart>('DavAddConnection');
// ConnectionHandle : HANDLE* in/out -> Pointer<Void>
// RemoteName : LPCWSTR -> Pointer<Utf16>
// UserName : LPCWSTR optional -> Pointer<Utf16>
// Password : LPCWSTR optional -> Pointer<Utf16>
// ClientCert : BYTE* -> Pointer<Uint8>
// CertSize : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DavAddConnection(
  ConnectionHandle: Pointer;   // HANDLE* in/out
  RemoteName: PWideChar;   // LPCWSTR
  UserName: PWideChar;   // LPCWSTR optional
  Password: PWideChar;   // LPCWSTR optional
  ClientCert: Pointer;   // BYTE*
  CertSize: DWORD   // DWORD
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'DavAddConnection';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DavAddConnection"
  c_DavAddConnection :: Ptr () -> CWString -> CWString -> CWString -> Ptr Word8 -> Word32 -> IO Word32
-- ConnectionHandle : HANDLE* in/out -> Ptr ()
-- RemoteName : LPCWSTR -> CWString
-- UserName : LPCWSTR optional -> CWString
-- Password : LPCWSTR optional -> CWString
-- ClientCert : BYTE* -> Ptr Word8
-- CertSize : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let davaddconnection =
  foreign "DavAddConnection"
    ((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint8_t) @-> uint32_t @-> returning uint32_t)
(* ConnectionHandle : HANDLE* in/out -> (ptr void) *)
(* RemoteName : LPCWSTR -> (ptr uint16_t) *)
(* UserName : LPCWSTR optional -> (ptr uint16_t) *)
(* Password : LPCWSTR optional -> (ptr uint16_t) *)
(* ClientCert : BYTE* -> (ptr uint8_t) *)
(* CertSize : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library netapi32 (t "NETAPI32.dll"))
(cffi:use-foreign-library netapi32)

(cffi:defcfun ("DavAddConnection" dav-add-connection :convention :stdcall) :uint32
  (connection-handle :pointer)   ; HANDLE* in/out
  (remote-name (:string :encoding :utf-16le))   ; LPCWSTR
  (user-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (password (:string :encoding :utf-16le))   ; LPCWSTR optional
  (client-cert :pointer)   ; BYTE*
  (cert-size :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DavAddConnection = Win32::API::More->new('NETAPI32',
    'DWORD DavAddConnection(HANDLE ConnectionHandle, LPCWSTR RemoteName, LPCWSTR UserName, LPCWSTR Password, LPVOID ClientCert, DWORD CertSize)');
# my $ret = $DavAddConnection->Call($ConnectionHandle, $RemoteName, $UserName, $Password, $ClientCert, $CertSize);
# ConnectionHandle : HANDLE* in/out -> HANDLE
# RemoteName : LPCWSTR -> LPCWSTR
# UserName : LPCWSTR optional -> LPCWSTR
# Password : LPCWSTR optional -> LPCWSTR
# ClientCert : BYTE* -> LPVOID
# CertSize : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。