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接続先や種別を記述したNETRESOURCEW構造体へのポインタ。
lpPasswordLPWSTRinoptional接続に用いるパスワードのワイド文字列。既定資格情報ならNULL可。
lpUserNameLPWSTRinoptional接続に用いるユーザー名のワイド文字列。既定ユーザーならNULL可。

戻り値の型: 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)。
const std = @import("std");

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

typedef NPAddConnectionNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>);
typedef NPAddConnectionDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>);
final NPAddConnection = DynamicLibrary.open('davclnt.dll')
    .lookupFunction<NPAddConnectionNative, NPAddConnectionDart>('NPAddConnection');
// lpNetResource : NETRESOURCEW* -> Pointer<Void>
// lpPassword : LPWSTR optional -> Pointer<Utf16>
// lpUserName : LPWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NPAddConnection(
  lpNetResource: Pointer;   // NETRESOURCEW*
  lpPassword: PWideChar;   // LPWSTR optional
  lpUserName: PWideChar   // LPWSTR optional
): DWORD; stdcall;
  external 'davclnt.dll' name 'NPAddConnection';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NPAddConnection"
  c_NPAddConnection :: Ptr () -> CWString -> CWString -> IO Word32
-- lpNetResource : NETRESOURCEW* -> Ptr ()
-- lpPassword : LPWSTR optional -> CWString
-- lpUserName : LPWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let npaddconnection =
  foreign "NPAddConnection"
    ((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning uint32_t)
(* lpNetResource : NETRESOURCEW* -> (ptr void) *)
(* lpPassword : LPWSTR optional -> (ptr uint16_t) *)
(* lpUserName : LPWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library davclnt (t "davclnt.dll"))
(cffi:use-foreign-library davclnt)

(cffi:defcfun ("NPAddConnection" npadd-connection :convention :stdcall) :uint32
  (lp-net-resource :pointer)   ; NETRESOURCEW*
  (lp-password (:string :encoding :utf-16le))   ; LPWSTR optional
  (lp-user-name (:string :encoding :utf-16le)))   ; LPWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NPAddConnection = Win32::API::More->new('davclnt',
    'DWORD NPAddConnection(LPVOID lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserName)');
# my $ret = $NPAddConnection->Call($lpNetResource, $lpPassword, $lpUserName);
# lpNetResource : NETRESOURCEW* -> LPVOID
# lpPassword : LPWSTR optional -> LPCWSTR
# lpUserName : LPWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
使用する型