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

AttachConsole

関数
指定したプロセスのコンソールに呼び出し元プロセスを接続する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり

シグネチャ

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

BOOL AttachConsole(
    DWORD dwProcessId
);

パラメーター

名前方向
dwProcessIdDWORDin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL AttachConsole(
    DWORD dwProcessId
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool AttachConsole(
    uint dwProcessId   // DWORD
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function AttachConsole(
    dwProcessId As UInteger   ' DWORD
) As Boolean
End Function
' dwProcessId : DWORD
Declare PtrSafe Function AttachConsole Lib "kernel32" ( _
    ByVal dwProcessId As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

AttachConsole = ctypes.windll.kernel32.AttachConsole
AttachConsole.restype = wintypes.BOOL
AttachConsole.argtypes = [
    wintypes.DWORD,  # dwProcessId : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
AttachConsole = Fiddle::Function.new(
  lib['AttachConsole'],
  [
    -Fiddle::TYPE_INT,  # dwProcessId : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn AttachConsole(
        dwProcessId: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern bool AttachConsole(uint dwProcessId);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_AttachConsole' -Namespace Win32 -PassThru
# $api::AttachConsole(dwProcessId)
#uselib "KERNEL32.dll"
#func global AttachConsole "AttachConsole" sptr
; AttachConsole dwProcessId   ; 戻り値は stat
; dwProcessId : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global AttachConsole "AttachConsole" int
; res = AttachConsole(dwProcessId)
; dwProcessId : DWORD -> "int"
; BOOL AttachConsole(DWORD dwProcessId)
#uselib "KERNEL32.dll"
#cfunc global AttachConsole "AttachConsole" int
; res = AttachConsole(dwProcessId)
; dwProcessId : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procAttachConsole = kernel32.NewProc("AttachConsole")
)

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