ホーム › System.SystemInformation › GetComputerNameExA
GetComputerNameExA
関数指定した形式でコンピューター名を取得する(ANSI版)。
シグネチャ
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
BOOL GetComputerNameExA(
COMPUTER_NAME_FORMAT NameType,
LPSTR lpBuffer, // optional
DWORD* nSize
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| NameType | COMPUTER_NAME_FORMAT | in |
| lpBuffer | LPSTR | outoptional |
| nSize | DWORD* | inout |
戻り値の型: BOOL
各言語での呼び出し定義
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
BOOL GetComputerNameExA(
COMPUTER_NAME_FORMAT NameType,
LPSTR lpBuffer, // optional
DWORD* nSize
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool GetComputerNameExA(
int NameType, // COMPUTER_NAME_FORMAT
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer, // LPSTR optional, out
ref uint nSize // DWORD* in/out
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetComputerNameExA(
NameType As Integer, ' COMPUTER_NAME_FORMAT
<MarshalAs(UnmanagedType.LPStr)> lpBuffer As System.Text.StringBuilder, ' LPSTR optional, out
ByRef nSize As UInteger ' DWORD* in/out
) As Boolean
End Function' NameType : COMPUTER_NAME_FORMAT
' lpBuffer : LPSTR optional, out
' nSize : DWORD* in/out
Declare PtrSafe Function GetComputerNameExA Lib "kernel32" ( _
ByVal NameType As Long, _
ByVal lpBuffer As String, _
ByRef nSize As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GetComputerNameExA = ctypes.windll.kernel32.GetComputerNameExA
GetComputerNameExA.restype = wintypes.BOOL
GetComputerNameExA.argtypes = [
ctypes.c_int, # NameType : COMPUTER_NAME_FORMAT
wintypes.LPSTR, # lpBuffer : LPSTR optional, out
ctypes.POINTER(wintypes.DWORD), # nSize : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
GetComputerNameExA = Fiddle::Function.new(
lib['GetComputerNameExA'],
[
Fiddle::TYPE_INT, # NameType : COMPUTER_NAME_FORMAT
Fiddle::TYPE_VOIDP, # lpBuffer : LPSTR optional, out
Fiddle::TYPE_VOIDP, # nSize : DWORD* in/out
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn GetComputerNameExA(
NameType: i32, // COMPUTER_NAME_FORMAT
lpBuffer: *mut u8, // LPSTR optional, out
nSize: *mut u32 // DWORD* in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool GetComputerNameExA(int NameType, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer, ref uint nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetComputerNameExA' -Namespace Win32 -PassThru
# $api::GetComputerNameExA(NameType, lpBuffer, nSize)#uselib "KERNEL32.dll"
#func global GetComputerNameExA "GetComputerNameExA" sptr, sptr, sptr
; GetComputerNameExA NameType, varptr(lpBuffer), varptr(nSize) ; 戻り値は stat
; NameType : COMPUTER_NAME_FORMAT -> "sptr"
; lpBuffer : LPSTR optional, out -> "sptr"
; nSize : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global GetComputerNameExA "GetComputerNameExA" int, var, var ; res = GetComputerNameExA(NameType, lpBuffer, nSize) ; NameType : COMPUTER_NAME_FORMAT -> "int" ; lpBuffer : LPSTR optional, out -> "var" ; nSize : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global GetComputerNameExA "GetComputerNameExA" int, sptr, sptr ; res = GetComputerNameExA(NameType, varptr(lpBuffer), varptr(nSize)) ; NameType : COMPUTER_NAME_FORMAT -> "int" ; lpBuffer : LPSTR optional, out -> "sptr" ; nSize : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL GetComputerNameExA(COMPUTER_NAME_FORMAT NameType, LPSTR lpBuffer, DWORD* nSize) #uselib "KERNEL32.dll" #cfunc global GetComputerNameExA "GetComputerNameExA" int, var, var ; res = GetComputerNameExA(NameType, lpBuffer, nSize) ; NameType : COMPUTER_NAME_FORMAT -> "int" ; lpBuffer : LPSTR optional, out -> "var" ; nSize : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL GetComputerNameExA(COMPUTER_NAME_FORMAT NameType, LPSTR lpBuffer, DWORD* nSize) #uselib "KERNEL32.dll" #cfunc global GetComputerNameExA "GetComputerNameExA" int, intptr, intptr ; res = GetComputerNameExA(NameType, varptr(lpBuffer), varptr(nSize)) ; NameType : COMPUTER_NAME_FORMAT -> "int" ; lpBuffer : LPSTR optional, out -> "intptr" ; nSize : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procGetComputerNameExA = kernel32.NewProc("GetComputerNameExA")
)
// NameType (COMPUTER_NAME_FORMAT), lpBuffer (LPSTR optional, out), nSize (DWORD* in/out)
r1, _, err := procGetComputerNameExA.Call(
uintptr(NameType),
uintptr(lpBuffer),
uintptr(nSize),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction GetComputerNameExA(
NameType: Integer; // COMPUTER_NAME_FORMAT
lpBuffer: PAnsiChar; // LPSTR optional, out
nSize: Pointer // DWORD* in/out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'GetComputerNameExA';result := DllCall("KERNEL32\GetComputerNameExA"
, "Int", NameType ; COMPUTER_NAME_FORMAT
, "Ptr", lpBuffer ; LPSTR optional, out
, "Ptr", nSize ; DWORD* in/out
, "Int") ; return: BOOL●GetComputerNameExA(NameType, lpBuffer, nSize) = DLL("KERNEL32.dll", "bool GetComputerNameExA(int, char*, void*)")
# 呼び出し: GetComputerNameExA(NameType, lpBuffer, nSize)
# NameType : COMPUTER_NAME_FORMAT -> "int"
# lpBuffer : LPSTR optional, out -> "char*"
# nSize : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。