Win32 API 日本語リファレンス
ホームFoundation › SetLastError

SetLastError

関数
呼び出しスレッドの最終エラーコードを設定する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

void SetLastError(
    WIN32_ERROR dwErrCode
);

パラメーター

名前方向
dwErrCodeWIN32_ERRORin

戻り値の型: void

各言語での呼び出し定義

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

void SetLastError(
    WIN32_ERROR dwErrCode
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern void SetLastError(
    uint dwErrCode   // WIN32_ERROR
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Sub SetLastError(
    dwErrCode As UInteger   ' WIN32_ERROR
)
End Sub
' dwErrCode : WIN32_ERROR
Declare PtrSafe Sub SetLastError Lib "kernel32" ( _
    ByVal dwErrCode As Long)
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetLastError = ctypes.windll.kernel32.SetLastError
SetLastError.restype = None
SetLastError.argtypes = [
    wintypes.DWORD,  # dwErrCode : WIN32_ERROR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SetLastError = Fiddle::Function.new(
  lib['SetLastError'],
  [
    -Fiddle::TYPE_INT,  # dwErrCode : WIN32_ERROR
  ],
  Fiddle::TYPE_VOID)
#[link(name = "kernel32")]
extern "system" {
    fn SetLastError(
        dwErrCode: u32  // WIN32_ERROR
    );
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern void SetLastError(uint dwErrCode);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetLastError' -Namespace Win32 -PassThru
# $api::SetLastError(dwErrCode)
#uselib "KERNEL32.dll"
#func global SetLastError "SetLastError" sptr
; SetLastError dwErrCode
; dwErrCode : WIN32_ERROR -> "sptr"
#uselib "KERNEL32.dll"
#func global SetLastError "SetLastError" int
; SetLastError dwErrCode
; dwErrCode : WIN32_ERROR -> "int"
; void SetLastError(WIN32_ERROR dwErrCode)
#uselib "KERNEL32.dll"
#func global SetLastError "SetLastError" int
; SetLastError dwErrCode
; dwErrCode : WIN32_ERROR -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetLastError = kernel32.NewProc("SetLastError")
)

// dwErrCode (WIN32_ERROR)
r1, _, err := procSetLastError.Call(
	uintptr(dwErrCode),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void
procedure SetLastError(
  dwErrCode: DWORD   // WIN32_ERROR
); stdcall;
  external 'KERNEL32.dll' name 'SetLastError';
result := DllCall("KERNEL32\SetLastError"
    , "UInt", dwErrCode   ; WIN32_ERROR
    , "Int")   ; return: void
●SetLastError(dwErrCode) = DLL("KERNEL32.dll", "int SetLastError(dword)")
# 呼び出し: SetLastError(dwErrCode)
# dwErrCode : WIN32_ERROR -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。