Win32 API 日本語リファレンス
ホームSystem.SystemInformation › SetComputerNameExA

SetComputerNameExA

関数
指定した形式でコンピューター名を設定する(ANSI版)。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL SetComputerNameExA(
    COMPUTER_NAME_FORMAT NameType,
    LPCSTR lpBuffer
);

パラメーター

名前方向
NameTypeCOMPUTER_NAME_FORMATin
lpBufferLPCSTRin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetComputerNameExA(
    COMPUTER_NAME_FORMAT NameType,
    LPCSTR lpBuffer
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool SetComputerNameExA(
    int NameType,   // COMPUTER_NAME_FORMAT
    [MarshalAs(UnmanagedType.LPStr)] string lpBuffer   // LPCSTR
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetComputerNameExA(
    NameType As Integer,   ' COMPUTER_NAME_FORMAT
    <MarshalAs(UnmanagedType.LPStr)> lpBuffer As String   ' LPCSTR
) As Boolean
End Function
' NameType : COMPUTER_NAME_FORMAT
' lpBuffer : LPCSTR
Declare PtrSafe Function SetComputerNameExA Lib "kernel32" ( _
    ByVal NameType As Long, _
    ByVal lpBuffer As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetComputerNameExA = ctypes.windll.kernel32.SetComputerNameExA
SetComputerNameExA.restype = wintypes.BOOL
SetComputerNameExA.argtypes = [
    ctypes.c_int,  # NameType : COMPUTER_NAME_FORMAT
    wintypes.LPCSTR,  # lpBuffer : LPCSTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SetComputerNameExA = Fiddle::Function.new(
  lib['SetComputerNameExA'],
  [
    Fiddle::TYPE_INT,  # NameType : COMPUTER_NAME_FORMAT
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPCSTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn SetComputerNameExA(
        NameType: i32,  // COMPUTER_NAME_FORMAT
        lpBuffer: *const u8  // LPCSTR
    ) -> 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 SetComputerNameExA(int NameType, [MarshalAs(UnmanagedType.LPStr)] string lpBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetComputerNameExA' -Namespace Win32 -PassThru
# $api::SetComputerNameExA(NameType, lpBuffer)
#uselib "KERNEL32.dll"
#func global SetComputerNameExA "SetComputerNameExA" sptr, sptr
; SetComputerNameExA NameType, lpBuffer   ; 戻り値は stat
; NameType : COMPUTER_NAME_FORMAT -> "sptr"
; lpBuffer : LPCSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global SetComputerNameExA "SetComputerNameExA" int, str
; res = SetComputerNameExA(NameType, lpBuffer)
; NameType : COMPUTER_NAME_FORMAT -> "int"
; lpBuffer : LPCSTR -> "str"
; BOOL SetComputerNameExA(COMPUTER_NAME_FORMAT NameType, LPCSTR lpBuffer)
#uselib "KERNEL32.dll"
#cfunc global SetComputerNameExA "SetComputerNameExA" int, str
; res = SetComputerNameExA(NameType, lpBuffer)
; NameType : COMPUTER_NAME_FORMAT -> "int"
; lpBuffer : LPCSTR -> "str"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetComputerNameExA = kernel32.NewProc("SetComputerNameExA")
)

// NameType (COMPUTER_NAME_FORMAT), lpBuffer (LPCSTR)
r1, _, err := procSetComputerNameExA.Call(
	uintptr(NameType),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpBuffer))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetComputerNameExA(
  NameType: Integer;   // COMPUTER_NAME_FORMAT
  lpBuffer: PAnsiChar   // LPCSTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'SetComputerNameExA';
result := DllCall("KERNEL32\SetComputerNameExA"
    , "Int", NameType   ; COMPUTER_NAME_FORMAT
    , "AStr", lpBuffer   ; LPCSTR
    , "Int")   ; return: BOOL
●SetComputerNameExA(NameType, lpBuffer) = DLL("KERNEL32.dll", "bool SetComputerNameExA(int, char*)")
# 呼び出し: SetComputerNameExA(NameType, lpBuffer)
# NameType : COMPUTER_NAME_FORMAT -> "int"
# lpBuffer : LPCSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。