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

SnmpMgrOpen

関数
SNMPエージェントへの管理セッションを開いて接続する。
DLLmgmtapi.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

void* SnmpMgrOpen(
    LPSTR lpAgentAddress,   // optional
    LPSTR lpAgentCommunity,   // optional
    INT nTimeOut,
    INT nRetries
);

パラメーター

名前方向説明
lpAgentAddressLPSTRinoptional接続先SNMPエージェントのアドレス(IPアドレスまたはホスト名)文字列。
lpAgentCommunityLPSTRinoptional認証に用いるコミュニティ名文字列(例: public)。
nTimeOutINTin要求の応答待ちタイムアウト(ミリ秒)。
nRetriesINTin応答が無い場合の再送回数。

戻り値の型: void*

各言語での呼び出し定義

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

void* SnmpMgrOpen(
    LPSTR lpAgentAddress,   // optional
    LPSTR lpAgentCommunity,   // optional
    INT nTimeOut,
    INT nRetries
);
[DllImport("mgmtapi.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr SnmpMgrOpen(
    [MarshalAs(UnmanagedType.LPStr)] string lpAgentAddress,   // LPSTR optional
    [MarshalAs(UnmanagedType.LPStr)] string lpAgentCommunity,   // LPSTR optional
    int nTimeOut,   // INT
    int nRetries   // INT
);
<DllImport("mgmtapi.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SnmpMgrOpen(
    <MarshalAs(UnmanagedType.LPStr)> lpAgentAddress As String,   ' LPSTR optional
    <MarshalAs(UnmanagedType.LPStr)> lpAgentCommunity As String,   ' LPSTR optional
    nTimeOut As Integer,   ' INT
    nRetries As Integer   ' INT
) As IntPtr
End Function
' lpAgentAddress : LPSTR optional
' lpAgentCommunity : LPSTR optional
' nTimeOut : INT
' nRetries : INT
Declare PtrSafe Function SnmpMgrOpen Lib "mgmtapi" ( _
    ByVal lpAgentAddress As String, _
    ByVal lpAgentCommunity As String, _
    ByVal nTimeOut As Long, _
    ByVal nRetries As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SnmpMgrOpen = ctypes.windll.mgmtapi.SnmpMgrOpen
SnmpMgrOpen.restype = ctypes.c_void_p
SnmpMgrOpen.argtypes = [
    wintypes.LPCSTR,  # lpAgentAddress : LPSTR optional
    wintypes.LPCSTR,  # lpAgentCommunity : LPSTR optional
    ctypes.c_int,  # nTimeOut : INT
    ctypes.c_int,  # nRetries : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('mgmtapi.dll')
SnmpMgrOpen = Fiddle::Function.new(
  lib['SnmpMgrOpen'],
  [
    Fiddle::TYPE_VOIDP,  # lpAgentAddress : LPSTR optional
    Fiddle::TYPE_VOIDP,  # lpAgentCommunity : LPSTR optional
    Fiddle::TYPE_INT,  # nTimeOut : INT
    Fiddle::TYPE_INT,  # nRetries : INT
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "mgmtapi")]
extern "system" {
    fn SnmpMgrOpen(
        lpAgentAddress: *mut u8,  // LPSTR optional
        lpAgentCommunity: *mut u8,  // LPSTR optional
        nTimeOut: i32,  // INT
        nRetries: i32  // INT
    ) -> *mut ();
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("mgmtapi.dll", SetLastError = true)]
public static extern IntPtr SnmpMgrOpen([MarshalAs(UnmanagedType.LPStr)] string lpAgentAddress, [MarshalAs(UnmanagedType.LPStr)] string lpAgentCommunity, int nTimeOut, int nRetries);
"@
$api = Add-Type -MemberDefinition $sig -Name 'mgmtapi_SnmpMgrOpen' -Namespace Win32 -PassThru
# $api::SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries)
#uselib "mgmtapi.dll"
#func global SnmpMgrOpen "SnmpMgrOpen" sptr, sptr, sptr, sptr
; SnmpMgrOpen lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries   ; 戻り値は stat
; lpAgentAddress : LPSTR optional -> "sptr"
; lpAgentCommunity : LPSTR optional -> "sptr"
; nTimeOut : INT -> "sptr"
; nRetries : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "mgmtapi.dll"
#cfunc global SnmpMgrOpen "SnmpMgrOpen" str, str, int, int
; res = SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries)
; lpAgentAddress : LPSTR optional -> "str"
; lpAgentCommunity : LPSTR optional -> "str"
; nTimeOut : INT -> "int"
; nRetries : INT -> "int"
; void* SnmpMgrOpen(LPSTR lpAgentAddress, LPSTR lpAgentCommunity, INT nTimeOut, INT nRetries)
#uselib "mgmtapi.dll"
#cfunc global SnmpMgrOpen "SnmpMgrOpen" str, str, int, int
; res = SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries)
; lpAgentAddress : LPSTR optional -> "str"
; lpAgentCommunity : LPSTR optional -> "str"
; nTimeOut : INT -> "int"
; nRetries : INT -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mgmtapi = windows.NewLazySystemDLL("mgmtapi.dll")
	procSnmpMgrOpen = mgmtapi.NewProc("SnmpMgrOpen")
)

// lpAgentAddress (LPSTR optional), lpAgentCommunity (LPSTR optional), nTimeOut (INT), nRetries (INT)
r1, _, err := procSnmpMgrOpen.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpAgentAddress))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpAgentCommunity))),
	uintptr(nTimeOut),
	uintptr(nRetries),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void*
function SnmpMgrOpen(
  lpAgentAddress: PAnsiChar;   // LPSTR optional
  lpAgentCommunity: PAnsiChar;   // LPSTR optional
  nTimeOut: Integer;   // INT
  nRetries: Integer   // INT
): Pointer; stdcall;
  external 'mgmtapi.dll' name 'SnmpMgrOpen';
result := DllCall("mgmtapi\SnmpMgrOpen"
    , "AStr", lpAgentAddress   ; LPSTR optional
    , "AStr", lpAgentCommunity   ; LPSTR optional
    , "Int", nTimeOut   ; INT
    , "Int", nRetries   ; INT
    , "Ptr")   ; return: void*
●SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries) = DLL("mgmtapi.dll", "void* SnmpMgrOpen(char*, char*, int, int)")
# 呼び出し: SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries)
# lpAgentAddress : LPSTR optional -> "char*"
# lpAgentCommunity : LPSTR optional -> "char*"
# nTimeOut : INT -> "int"
# nRetries : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "mgmtapi" fn SnmpMgrOpen(
    lpAgentAddress: [*c]const u8, // LPSTR optional
    lpAgentCommunity: [*c]const u8, // LPSTR optional
    nTimeOut: i32, // INT
    nRetries: i32 // INT
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc SnmpMgrOpen(
    lpAgentAddress: cstring,  # LPSTR optional
    lpAgentCommunity: cstring,  # LPSTR optional
    nTimeOut: int32,  # INT
    nRetries: int32  # INT
): pointer {.importc: "SnmpMgrOpen", stdcall, dynlib: "mgmtapi.dll".}
pragma(lib, "mgmtapi");
extern(Windows)
void* SnmpMgrOpen(
    const(char)* lpAgentAddress,   // LPSTR optional
    const(char)* lpAgentCommunity,   // LPSTR optional
    int nTimeOut,   // INT
    int nRetries   // INT
);
ccall((:SnmpMgrOpen, "mgmtapi.dll"), stdcall, Ptr{Cvoid},
      (Cstring, Cstring, Int32, Int32),
      lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries)
# lpAgentAddress : LPSTR optional -> Cstring
# lpAgentCommunity : LPSTR optional -> Cstring
# nTimeOut : INT -> Int32
# nRetries : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* SnmpMgrOpen(
    const char* lpAgentAddress,
    const char* lpAgentCommunity,
    int32_t nTimeOut,
    int32_t nRetries);
]]
local mgmtapi = ffi.load("mgmtapi")
-- mgmtapi.SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries)
-- lpAgentAddress : LPSTR optional
-- lpAgentCommunity : LPSTR optional
-- nTimeOut : INT
-- nRetries : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('mgmtapi.dll');
const SnmpMgrOpen = lib.func('__stdcall', 'SnmpMgrOpen', 'void *', ['str', 'str', 'int32_t', 'int32_t']);
// SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries)
// lpAgentAddress : LPSTR optional -> 'str'
// lpAgentCommunity : LPSTR optional -> 'str'
// nTimeOut : INT -> 'int32_t'
// nRetries : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("mgmtapi.dll", {
  SnmpMgrOpen: { parameters: ["buffer", "buffer", "i32", "i32"], result: "pointer" },
});
// lib.symbols.SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries)
// lpAgentAddress : LPSTR optional -> "buffer"
// lpAgentCommunity : LPSTR optional -> "buffer"
// nTimeOut : INT -> "i32"
// nRetries : INT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* SnmpMgrOpen(
    const char* lpAgentAddress,
    const char* lpAgentCommunity,
    int32_t nTimeOut,
    int32_t nRetries);
C, "mgmtapi.dll");
// $ffi->SnmpMgrOpen(lpAgentAddress, lpAgentCommunity, nTimeOut, nRetries);
// lpAgentAddress : LPSTR optional
// lpAgentCommunity : LPSTR optional
// nTimeOut : INT
// nRetries : INT
// 構造体/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 Mgmtapi extends StdCallLibrary {
    Mgmtapi INSTANCE = Native.load("mgmtapi", Mgmtapi.class);
    Pointer SnmpMgrOpen(
        String lpAgentAddress,   // LPSTR optional
        String lpAgentCommunity,   // LPSTR optional
        int nTimeOut,   // INT
        int nRetries   // INT
    );
}
@[Link("mgmtapi")]
lib Libmgmtapi
  fun SnmpMgrOpen = SnmpMgrOpen(
    lpAgentAddress : UInt8*,   # LPSTR optional
    lpAgentCommunity : UInt8*,   # LPSTR optional
    nTimeOut : Int32,   # INT
    nRetries : Int32   # INT
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SnmpMgrOpenNative = Pointer<Void> Function(Pointer<Utf8>, Pointer<Utf8>, Int32, Int32);
typedef SnmpMgrOpenDart = Pointer<Void> Function(Pointer<Utf8>, Pointer<Utf8>, int, int);
final SnmpMgrOpen = DynamicLibrary.open('mgmtapi.dll')
    .lookupFunction<SnmpMgrOpenNative, SnmpMgrOpenDart>('SnmpMgrOpen');
// lpAgentAddress : LPSTR optional -> Pointer<Utf8>
// lpAgentCommunity : LPSTR optional -> Pointer<Utf8>
// nTimeOut : INT -> Int32
// nRetries : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SnmpMgrOpen(
  lpAgentAddress: PAnsiChar;   // LPSTR optional
  lpAgentCommunity: PAnsiChar;   // LPSTR optional
  nTimeOut: Integer;   // INT
  nRetries: Integer   // INT
): Pointer; stdcall;
  external 'mgmtapi.dll' name 'SnmpMgrOpen';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SnmpMgrOpen"
  c_SnmpMgrOpen :: CString -> CString -> Int32 -> Int32 -> IO (Ptr ())
-- lpAgentAddress : LPSTR optional -> CString
-- lpAgentCommunity : LPSTR optional -> CString
-- nTimeOut : INT -> Int32
-- nRetries : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let snmpmgropen =
  foreign "SnmpMgrOpen"
    (string @-> string @-> int32_t @-> int32_t @-> returning (ptr void))
(* lpAgentAddress : LPSTR optional -> string *)
(* lpAgentCommunity : LPSTR optional -> string *)
(* nTimeOut : INT -> int32_t *)
(* nRetries : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library mgmtapi (t "mgmtapi.dll"))
(cffi:use-foreign-library mgmtapi)

(cffi:defcfun ("SnmpMgrOpen" snmp-mgr-open :convention :stdcall) :pointer
  (lp-agent-address :string)   ; LPSTR optional
  (lp-agent-community :string)   ; LPSTR optional
  (n-time-out :int32)   ; INT
  (n-retries :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SnmpMgrOpen = Win32::API::More->new('mgmtapi',
    'LPVOID SnmpMgrOpen(LPCSTR lpAgentAddress, LPCSTR lpAgentCommunity, int nTimeOut, int nRetries)');
# my $ret = $SnmpMgrOpen->Call($lpAgentAddress, $lpAgentCommunity, $nTimeOut, $nRetries);
# lpAgentAddress : LPSTR optional -> LPCSTR
# lpAgentCommunity : LPSTR optional -> LPCSTR
# nTimeOut : INT -> int
# nRetries : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。