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

SetLastErrorEx

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

シグネチャ

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

void SetLastErrorEx(
    WIN32_ERROR dwErrCode,
    DWORD dwType
);

パラメーター

名前方向
dwErrCodeWIN32_ERRORin
dwTypeDWORDin

戻り値の型: void

各言語での呼び出し定義

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

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

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

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetLastErrorEx = user32.NewProc("SetLastErrorEx")
)

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