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

NPAddConnection

関数
ネットワークプロバイダー経由でリソースに接続する。
DLLdavclnt.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

DWORD NPAddConnection(
    NETRESOURCEW* lpNetResource,
    LPWSTR lpPassword,   // optional
    LPWSTR lpUserName   // optional
);

パラメーター

名前方向
lpNetResourceNETRESOURCEW*in
lpPasswordLPWSTRinoptional
lpUserNameLPWSTRinoptional

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD NPAddConnection(
    NETRESOURCEW* lpNetResource,
    LPWSTR lpPassword,   // optional
    LPWSTR lpUserName   // optional
);
[DllImport("davclnt.dll", ExactSpelling = true)]
static extern uint NPAddConnection(
    IntPtr lpNetResource,   // NETRESOURCEW*
    [MarshalAs(UnmanagedType.LPWStr)] string lpPassword,   // LPWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpUserName   // LPWSTR optional
);
<DllImport("davclnt.dll", ExactSpelling:=True)>
Public Shared Function NPAddConnection(
    lpNetResource As IntPtr,   ' NETRESOURCEW*
    <MarshalAs(UnmanagedType.LPWStr)> lpPassword As String,   ' LPWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpUserName As String   ' LPWSTR optional
) As UInteger
End Function
' lpNetResource : NETRESOURCEW*
' lpPassword : LPWSTR optional
' lpUserName : LPWSTR optional
Declare PtrSafe Function NPAddConnection Lib "davclnt" ( _
    ByVal lpNetResource As LongPtr, _
    ByVal lpPassword As LongPtr, _
    ByVal lpUserName As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NPAddConnection = ctypes.windll.davclnt.NPAddConnection
NPAddConnection.restype = wintypes.DWORD
NPAddConnection.argtypes = [
    ctypes.c_void_p,  # lpNetResource : NETRESOURCEW*
    wintypes.LPCWSTR,  # lpPassword : LPWSTR optional
    wintypes.LPCWSTR,  # lpUserName : LPWSTR optional
]
require 'fiddle'
require 'fiddle/import'

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

var (
	davclnt = windows.NewLazySystemDLL("davclnt.dll")
	procNPAddConnection = davclnt.NewProc("NPAddConnection")
)

// lpNetResource (NETRESOURCEW*), lpPassword (LPWSTR optional), lpUserName (LPWSTR optional)
r1, _, err := procNPAddConnection.Call(
	uintptr(lpNetResource),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPassword))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpUserName))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function NPAddConnection(
  lpNetResource: Pointer;   // NETRESOURCEW*
  lpPassword: PWideChar;   // LPWSTR optional
  lpUserName: PWideChar   // LPWSTR optional
): DWORD; stdcall;
  external 'davclnt.dll' name 'NPAddConnection';
result := DllCall("davclnt\NPAddConnection"
    , "Ptr", lpNetResource   ; NETRESOURCEW*
    , "WStr", lpPassword   ; LPWSTR optional
    , "WStr", lpUserName   ; LPWSTR optional
    , "UInt")   ; return: DWORD
●NPAddConnection(lpNetResource, lpPassword, lpUserName) = DLL("davclnt.dll", "dword NPAddConnection(void*, char*, char*)")
# 呼び出し: NPAddConnection(lpNetResource, lpPassword, lpUserName)
# lpNetResource : NETRESOURCEW* -> "void*"
# lpPassword : LPWSTR optional -> "char*"
# lpUserName : LPWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。