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

WNetGetConnectionA

関数
ローカル名に対応するリモートネットワーク名を取得する(ANSI版)。
DLLMPR.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// MPR.dll  (ANSI / -A)
#include <windows.h>

WIN32_ERROR WNetGetConnectionA(
    LPCSTR lpLocalName,
    LPSTR lpRemoteName,   // optional
    DWORD* lpnLength
);

パラメーター

名前方向説明
lpLocalNameLPCSTRinリモート名を問い合わせる対象のローカルデバイス名のANSI文字列。
lpRemoteNameLPSTRoutoptional対応するリモートネットワーク名を受け取るANSIバッファ。
lpnLengthDWORD*inout入出力でバッファの文字数を渡し、必要文字数を受け取る。

戻り値の型: WIN32_ERROR

各言語での呼び出し定義

// MPR.dll  (ANSI / -A)
#include <windows.h>

WIN32_ERROR WNetGetConnectionA(
    LPCSTR lpLocalName,
    LPSTR lpRemoteName,   // optional
    DWORD* lpnLength
);
[DllImport("MPR.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint WNetGetConnectionA(
    [MarshalAs(UnmanagedType.LPStr)] string lpLocalName,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpRemoteName,   // LPSTR optional, out
    ref uint lpnLength   // DWORD* in/out
);
<DllImport("MPR.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function WNetGetConnectionA(
    <MarshalAs(UnmanagedType.LPStr)> lpLocalName As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> lpRemoteName As System.Text.StringBuilder,   ' LPSTR optional, out
    ByRef lpnLength As UInteger   ' DWORD* in/out
) As UInteger
End Function
' lpLocalName : LPCSTR
' lpRemoteName : LPSTR optional, out
' lpnLength : DWORD* in/out
Declare PtrSafe Function WNetGetConnectionA Lib "mpr" ( _
    ByVal lpLocalName As String, _
    ByVal lpRemoteName As String, _
    ByRef lpnLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WNetGetConnectionA = ctypes.windll.mpr.WNetGetConnectionA
WNetGetConnectionA.restype = wintypes.DWORD
WNetGetConnectionA.argtypes = [
    wintypes.LPCSTR,  # lpLocalName : LPCSTR
    wintypes.LPSTR,  # lpRemoteName : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # lpnLength : DWORD* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('MPR.dll')
WNetGetConnectionA = Fiddle::Function.new(
  lib['WNetGetConnectionA'],
  [
    Fiddle::TYPE_VOIDP,  # lpLocalName : LPCSTR
    Fiddle::TYPE_VOIDP,  # lpRemoteName : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # lpnLength : DWORD* in/out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "mpr")]
extern "system" {
    fn WNetGetConnectionA(
        lpLocalName: *const u8,  // LPCSTR
        lpRemoteName: *mut u8,  // LPSTR optional, out
        lpnLength: *mut u32  // DWORD* in/out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("MPR.dll", CharSet = CharSet.Ansi)]
public static extern uint WNetGetConnectionA([MarshalAs(UnmanagedType.LPStr)] string lpLocalName, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpRemoteName, ref uint lpnLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MPR_WNetGetConnectionA' -Namespace Win32 -PassThru
# $api::WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength)
#uselib "MPR.dll"
#func global WNetGetConnectionA "WNetGetConnectionA" sptr, sptr, sptr
; WNetGetConnectionA lpLocalName, varptr(lpRemoteName), varptr(lpnLength)   ; 戻り値は stat
; lpLocalName : LPCSTR -> "sptr"
; lpRemoteName : LPSTR optional, out -> "sptr"
; lpnLength : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MPR.dll"
#cfunc global WNetGetConnectionA "WNetGetConnectionA" str, var, var
; res = WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength)
; lpLocalName : LPCSTR -> "str"
; lpRemoteName : LPSTR optional, out -> "var"
; lpnLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; WIN32_ERROR WNetGetConnectionA(LPCSTR lpLocalName, LPSTR lpRemoteName, DWORD* lpnLength)
#uselib "MPR.dll"
#cfunc global WNetGetConnectionA "WNetGetConnectionA" str, var, var
; res = WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength)
; lpLocalName : LPCSTR -> "str"
; lpRemoteName : LPSTR optional, out -> "var"
; lpnLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mpr = windows.NewLazySystemDLL("MPR.dll")
	procWNetGetConnectionA = mpr.NewProc("WNetGetConnectionA")
)

// lpLocalName (LPCSTR), lpRemoteName (LPSTR optional, out), lpnLength (DWORD* in/out)
r1, _, err := procWNetGetConnectionA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpLocalName))),
	uintptr(lpRemoteName),
	uintptr(lpnLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function WNetGetConnectionA(
  lpLocalName: PAnsiChar;   // LPCSTR
  lpRemoteName: PAnsiChar;   // LPSTR optional, out
  lpnLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'MPR.dll' name 'WNetGetConnectionA';
result := DllCall("MPR\WNetGetConnectionA"
    , "AStr", lpLocalName   ; LPCSTR
    , "Ptr", lpRemoteName   ; LPSTR optional, out
    , "Ptr", lpnLength   ; DWORD* in/out
    , "UInt")   ; return: WIN32_ERROR
●WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength) = DLL("MPR.dll", "dword WNetGetConnectionA(char*, char*, void*)")
# 呼び出し: WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength)
# lpLocalName : LPCSTR -> "char*"
# lpRemoteName : LPSTR optional, out -> "char*"
# lpnLength : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "mpr" fn WNetGetConnectionA(
    lpLocalName: [*c]const u8, // LPCSTR
    lpRemoteName: [*c]u8, // LPSTR optional, out
    lpnLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) u32;
proc WNetGetConnectionA(
    lpLocalName: cstring,  # LPCSTR
    lpRemoteName: ptr char,  # LPSTR optional, out
    lpnLength: ptr uint32  # DWORD* in/out
): uint32 {.importc: "WNetGetConnectionA", stdcall, dynlib: "MPR.dll".}
pragma(lib, "mpr");
extern(Windows)
uint WNetGetConnectionA(
    const(char)* lpLocalName,   // LPCSTR
    char* lpRemoteName,   // LPSTR optional, out
    uint* lpnLength   // DWORD* in/out
);
ccall((:WNetGetConnectionA, "MPR.dll"), stdcall, UInt32,
      (Cstring, Ptr{UInt8}, Ptr{UInt32}),
      lpLocalName, lpRemoteName, lpnLength)
# lpLocalName : LPCSTR -> Cstring
# lpRemoteName : LPSTR optional, out -> Ptr{UInt8}
# lpnLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t WNetGetConnectionA(
    const char* lpLocalName,
    char* lpRemoteName,
    uint32_t* lpnLength);
]]
local mpr = ffi.load("mpr")
-- mpr.WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength)
-- lpLocalName : LPCSTR
-- lpRemoteName : LPSTR optional, out
-- lpnLength : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('MPR.dll');
const WNetGetConnectionA = lib.func('__stdcall', 'WNetGetConnectionA', 'uint32_t', ['str', 'char *', 'uint32_t *']);
// WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength)
// lpLocalName : LPCSTR -> 'str'
// lpRemoteName : LPSTR optional, out -> 'char *'
// lpnLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("MPR.dll", {
  WNetGetConnectionA: { parameters: ["buffer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength)
// lpLocalName : LPCSTR -> "buffer"
// lpRemoteName : LPSTR optional, out -> "buffer"
// lpnLength : DWORD* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t WNetGetConnectionA(
    const char* lpLocalName,
    char* lpRemoteName,
    uint32_t* lpnLength);
C, "MPR.dll");
// $ffi->WNetGetConnectionA(lpLocalName, lpRemoteName, lpnLength);
// lpLocalName : LPCSTR
// lpRemoteName : LPSTR optional, out
// lpnLength : DWORD* in/out
// 構造体/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 Mpr extends StdCallLibrary {
    Mpr INSTANCE = Native.load("mpr", Mpr.class, W32APIOptions.ASCII_OPTIONS);
    int WNetGetConnectionA(
        String lpLocalName,   // LPCSTR
        byte[] lpRemoteName,   // LPSTR optional, out
        IntByReference lpnLength   // DWORD* in/out
    );
}
@[Link("mpr")]
lib LibMPR
  fun WNetGetConnectionA = WNetGetConnectionA(
    lpLocalName : UInt8*,   # LPCSTR
    lpRemoteName : UInt8*,   # LPSTR optional, out
    lpnLength : UInt32*   # DWORD* in/out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WNetGetConnectionANative = Uint32 Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>);
typedef WNetGetConnectionADart = int Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>);
final WNetGetConnectionA = DynamicLibrary.open('MPR.dll')
    .lookupFunction<WNetGetConnectionANative, WNetGetConnectionADart>('WNetGetConnectionA');
// lpLocalName : LPCSTR -> Pointer<Utf8>
// lpRemoteName : LPSTR optional, out -> Pointer<Utf8>
// lpnLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WNetGetConnectionA(
  lpLocalName: PAnsiChar;   // LPCSTR
  lpRemoteName: PAnsiChar;   // LPSTR optional, out
  lpnLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'MPR.dll' name 'WNetGetConnectionA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WNetGetConnectionA"
  c_WNetGetConnectionA :: CString -> CString -> Ptr Word32 -> IO Word32
-- lpLocalName : LPCSTR -> CString
-- lpRemoteName : LPSTR optional, out -> CString
-- lpnLength : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let wnetgetconnectiona =
  foreign "WNetGetConnectionA"
    (string @-> string @-> (ptr uint32_t) @-> returning uint32_t)
(* lpLocalName : LPCSTR -> string *)
(* lpRemoteName : LPSTR optional, out -> string *)
(* lpnLength : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library mpr (t "MPR.dll"))
(cffi:use-foreign-library mpr)

(cffi:defcfun ("WNetGetConnectionA" wnet-get-connection-a :convention :stdcall) :uint32
  (lp-local-name :string)   ; LPCSTR
  (lp-remote-name :pointer)   ; LPSTR optional, out
  (lpn-length :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WNetGetConnectionA = Win32::API::More->new('MPR',
    'DWORD WNetGetConnectionA(LPCSTR lpLocalName, LPSTR lpRemoteName, LPVOID lpnLength)');
# my $ret = $WNetGetConnectionA->Call($lpLocalName, $lpRemoteName, $lpnLength);
# lpLocalName : LPCSTR -> LPCSTR
# lpRemoteName : LPSTR optional, out -> LPSTR
# lpnLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
使用する型