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

SetComputerNameExW

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

シグネチャ

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL SetComputerNameExW(
    COMPUTER_NAME_FORMAT NameType,
    LPCWSTR lpBuffer
);

パラメーター

名前方向
NameTypeCOMPUTER_NAME_FORMATin
lpBufferLPCWSTRin

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL SetComputerNameExW(
    COMPUTER_NAME_FORMAT NameType,
    LPCWSTR lpBuffer
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SetComputerNameExW(
    int NameType,   // COMPUTER_NAME_FORMAT
    [MarshalAs(UnmanagedType.LPWStr)] string lpBuffer   // LPCWSTR
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetComputerNameExW(
    NameType As Integer,   ' COMPUTER_NAME_FORMAT
    <MarshalAs(UnmanagedType.LPWStr)> lpBuffer As String   ' LPCWSTR
) As Boolean
End Function
' NameType : COMPUTER_NAME_FORMAT
' lpBuffer : LPCWSTR
Declare PtrSafe Function SetComputerNameExW Lib "kernel32" ( _
    ByVal NameType As Long, _
    ByVal lpBuffer As LongPtr) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

lib = Fiddle.dlopen('KERNEL32.dll')
SetComputerNameExW = Fiddle::Function.new(
  lib['SetComputerNameExW'],
  [
    Fiddle::TYPE_INT,  # NameType : COMPUTER_NAME_FORMAT
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPCWSTR
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn SetComputerNameExW(
        NameType: i32,  // COMPUTER_NAME_FORMAT
        lpBuffer: *const u16  // LPCWSTR
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetComputerNameExW(int NameType, [MarshalAs(UnmanagedType.LPWStr)] string lpBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetComputerNameExW' -Namespace Win32 -PassThru
# $api::SetComputerNameExW(NameType, lpBuffer)
#uselib "KERNEL32.dll"
#func global SetComputerNameExW "SetComputerNameExW" wptr, wptr
; SetComputerNameExW NameType, lpBuffer   ; 戻り値は stat
; NameType : COMPUTER_NAME_FORMAT -> "wptr"
; lpBuffer : LPCWSTR -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global SetComputerNameExW "SetComputerNameExW" int, wstr
; res = SetComputerNameExW(NameType, lpBuffer)
; NameType : COMPUTER_NAME_FORMAT -> "int"
; lpBuffer : LPCWSTR -> "wstr"
; BOOL SetComputerNameExW(COMPUTER_NAME_FORMAT NameType, LPCWSTR lpBuffer)
#uselib "KERNEL32.dll"
#cfunc global SetComputerNameExW "SetComputerNameExW" int, wstr
; res = SetComputerNameExW(NameType, lpBuffer)
; NameType : COMPUTER_NAME_FORMAT -> "int"
; lpBuffer : LPCWSTR -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetComputerNameExW = kernel32.NewProc("SetComputerNameExW")
)

// NameType (COMPUTER_NAME_FORMAT), lpBuffer (LPCWSTR)
r1, _, err := procSetComputerNameExW.Call(
	uintptr(NameType),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpBuffer))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetComputerNameExW(
  NameType: Integer;   // COMPUTER_NAME_FORMAT
  lpBuffer: PWideChar   // LPCWSTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'SetComputerNameExW';
result := DllCall("KERNEL32\SetComputerNameExW"
    , "Int", NameType   ; COMPUTER_NAME_FORMAT
    , "WStr", lpBuffer   ; LPCWSTR
    , "Int")   ; return: BOOL
●SetComputerNameExW(NameType, lpBuffer) = DLL("KERNEL32.dll", "bool SetComputerNameExW(int, char*)")
# 呼び出し: SetComputerNameExW(NameType, lpBuffer)
# NameType : COMPUTER_NAME_FORMAT -> "int"
# lpBuffer : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。