Win32 API 日本語リファレンス
ホームNetworking.WinHttp › WinHttpCloseHandle

WinHttpCloseHandle

関数
WinHTTPハンドルを閉じてリソースを解放する。
DLLWINHTTP.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL WinHttpCloseHandle(
    void* hInternet
);

パラメーター

名前方向説明
hInternetvoid*inout閉じる対象となる有効な HINTERNET ハンドル(HINTERNET Handles in WinHTTP を参照)。

戻り値の型: BOOL

公式ドキュメント

WinHttpCloseHandle 関数は、単一の HINTERNET ハンドルを閉じます。

戻り値

ハンドルが正常に閉じられた場合は TRUE、それ以外の場合は FALSE を返します。拡張エラー情報を取得するには、 GetLastError を呼び出します。返されるエラーコードには次のものがあります。

エラーコード 説明
ERROR_WINHTTP_SHUTDOWN
WinHTTP 機能のサポートがシャットダウンまたはアンロードされています。
ERROR_WINHTTP_INTERNAL_ERROR
内部エラーが発生しました。
ERROR_NOT_ENOUGH_MEMORY
要求された操作を完了するのに十分なメモリがありませんでした。(Windows エラーコード)

解説(Remarks)

WinHTTP を非同期モードで使用している場合(つまり WinHttpOpenWINHTTP_FLAG_ASYNC が設定されている場合)でも、この関数は同期的に動作します。戻り値は成功または失敗を示します。拡張エラー情報を取得するには、 GetLastError を呼び出します。

閉じようとしているハンドルに対してステータスコールバックが登録されており、かつそのハンドルが非 NULL のコンテキスト値で作成されていた場合、WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING コールバックが行われます。これはそのハンドルから行われる最後のコールバックであり、ハンドルが破棄されることを示します。

アプリケーションは、WinHttpCloseHandle を使用して HINTERNET 要求ハンドルを閉じることで、実行中の非同期要求を終了できます。次の点に注意してください。

アプリケーションは、同期要求に対して WinHttpCloseHandle を呼び出してはなりません。これは競合状態を引き起こす可能性があります。詳細については HINTERNET Handles in WinHTTP を参照してください。
注意 Windows XP および Windows 2000 については、WinHttp スタートページの Run-Time Requirements セクションを参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL WinHttpCloseHandle(
    void* hInternet
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINHTTP.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WinHttpCloseHandle(
    IntPtr hInternet   // void* in/out
);
<DllImport("WINHTTP.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WinHttpCloseHandle(
    hInternet As IntPtr   ' void* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hInternet : void* in/out
Declare PtrSafe Function WinHttpCloseHandle Lib "winhttp" ( _
    ByVal hInternet As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WinHttpCloseHandle = ctypes.windll.winhttp.WinHttpCloseHandle
WinHttpCloseHandle.restype = wintypes.BOOL
WinHttpCloseHandle.argtypes = [
    ctypes.POINTER(None),  # hInternet : void* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	winhttp = windows.NewLazySystemDLL("WINHTTP.dll")
	procWinHttpCloseHandle = winhttp.NewProc("WinHttpCloseHandle")
)

// hInternet (void* in/out)
r1, _, err := procWinHttpCloseHandle.Call(
	uintptr(hInternet),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function WinHttpCloseHandle(
  hInternet: Pointer   // void* in/out
): BOOL; stdcall;
  external 'WINHTTP.dll' name 'WinHttpCloseHandle';
result := DllCall("WINHTTP\WinHttpCloseHandle"
    , "Ptr", hInternet   ; void* in/out
    , "Int")   ; return: BOOL
●WinHttpCloseHandle(hInternet) = DLL("WINHTTP.dll", "bool WinHttpCloseHandle(void*)")
# 呼び出し: WinHttpCloseHandle(hInternet)
# hInternet : void* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "winhttp" fn WinHttpCloseHandle(
    hInternet: ?*anyopaque // void* in/out
) callconv(std.os.windows.WINAPI) i32;
proc WinHttpCloseHandle(
    hInternet: pointer  # void* in/out
): int32 {.importc: "WinHttpCloseHandle", stdcall, dynlib: "WINHTTP.dll".}
pragma(lib, "winhttp");
extern(Windows)
int WinHttpCloseHandle(
    void* hInternet   // void* in/out
);
ccall((:WinHttpCloseHandle, "WINHTTP.dll"), stdcall, Int32,
      (Ptr{Cvoid},),
      hInternet)
# hInternet : void* in/out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WinHttpCloseHandle(
    void* hInternet);
]]
local winhttp = ffi.load("winhttp")
-- winhttp.WinHttpCloseHandle(hInternet)
-- hInternet : void* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WINHTTP.dll');
const WinHttpCloseHandle = lib.func('__stdcall', 'WinHttpCloseHandle', 'int32_t', ['void *']);
// WinHttpCloseHandle(hInternet)
// hInternet : void* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WINHTTP.dll", {
  WinHttpCloseHandle: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.WinHttpCloseHandle(hInternet)
// hInternet : void* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WinHttpCloseHandle(
    void* hInternet);
C, "WINHTTP.dll");
// $ffi->WinHttpCloseHandle(hInternet);
// hInternet : void* in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Winhttp extends StdCallLibrary {
    Winhttp INSTANCE = Native.load("winhttp", Winhttp.class);
    boolean WinHttpCloseHandle(
        Pointer hInternet   // void* in/out
    );
}
@[Link("winhttp")]
lib LibWINHTTP
  fun WinHttpCloseHandle = WinHttpCloseHandle(
    hInternet : Void*   # void* in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WinHttpCloseHandleNative = Int32 Function(Pointer<Void>);
typedef WinHttpCloseHandleDart = int Function(Pointer<Void>);
final WinHttpCloseHandle = DynamicLibrary.open('WINHTTP.dll')
    .lookupFunction<WinHttpCloseHandleNative, WinHttpCloseHandleDart>('WinHttpCloseHandle');
// hInternet : void* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WinHttpCloseHandle(
  hInternet: Pointer   // void* in/out
): BOOL; stdcall;
  external 'WINHTTP.dll' name 'WinHttpCloseHandle';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WinHttpCloseHandle"
  c_WinHttpCloseHandle :: Ptr () -> IO CInt
-- hInternet : void* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let winhttpclosehandle =
  foreign "WinHttpCloseHandle"
    ((ptr void) @-> returning int32_t)
(* hInternet : void* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library winhttp (t "WINHTTP.dll"))
(cffi:use-foreign-library winhttp)

(cffi:defcfun ("WinHttpCloseHandle" win-http-close-handle :convention :stdcall) :int32
  (h-internet :pointer))   ; void* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WinHttpCloseHandle = Win32::API::More->new('WINHTTP',
    'BOOL WinHttpCloseHandle(LPVOID hInternet)');
# my $ret = $WinHttpCloseHandle->Call($hInternet);
# hInternet : void* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目