Win32 API 日本語リファレンス
ホームStorage.FileSystem › GetBinaryTypeW

GetBinaryTypeW

関数
実行可能ファイルのバイナリ種別を判定する(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL GetBinaryTypeW(
    LPCWSTR lpApplicationName,
    DWORD* lpBinaryType
);

パラメーター

名前方向説明
lpApplicationNameLPCWSTRin

実行可能タイプを判定する対象ファイルのフルパスです。

既定では、名前は MAX_PATH 文字に制限されます。この制限を 32,767 ワイド文字に拡張するには、パスの先頭に "\\?\" を付加します。詳細については、「ファイル、パス、および名前空間の名前付け」を参照してください。

ヒント

Windows 10 バージョン 1607 以降では、"\\?\" を付加することなく MAX_PATH 制限を解除するようにオプトインできます。詳細については、「ファイル、パス、および名前空間の名前付け」の「Maximum Path Length Limitation」セクションを参照してください。

lpBinaryTypeDWORD*out

lpApplicationName で指定したファイルの実行可能タイプに関する情報を受け取る変数へのポインターです。次の定数が定義されています。

意味
SCS_32BIT_BINARY
0
32 ビットの Windows ベースアプリケーション
SCS_64BIT_BINARY
6
64 ビットの Windows ベースアプリケーション。
SCS_DOS_BINARY
1
MS-DOS ベースアプリケーション
SCS_OS216_BINARY
5
16 ビットの OS/2 ベースアプリケーション
SCS_PIF_BINARY
3
MS-DOS ベースアプリケーションを実行する PIF ファイル
SCS_POSIX_BINARY
4
POSIX ベースアプリケーション
SCS_WOW_BINARY
2
16 ビットの Windows ベースアプリケーション

戻り値の型: BOOL

公式ドキュメント

ファイルが実行可能(.exe)ファイルかどうかを判定し、実行可能ファイルである場合は、どのサブシステムがそのファイルを実行するかを判定します。(Unicode)

戻り値

ファイルが実行可能である場合、戻り値は 0 以外の値になります。この関数は、ファイルの実行可能タイプを示すために lpBinaryType が指す変数を設定します。

ファイルが実行可能でない場合、または関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、GetLastError を呼び出します。ファイルが DLL である場合、最後のエラーコードは ERROR_BAD_EXE_FORMAT になります。

解説(Remarks)

代替手段として、SHGetFileInfo 関数を呼び出し、uFlags パラメーターに SHGFI_EXETYPE フラグを渡すことで、同じ情報を取得できます。

シンボリックリンクの動作—パスがシンボリックリンクを指している場合は、リンク先のファイルが使用されます。

Windows 8 および Windows Server 2012 では、この関数は次のテクノロジでサポートされています。

テクノロジ サポート
Server Message Block (SMB) 3.0 プロトコル はい
SMB 3.0 Transparent Failover (TFO) はい
SMB 3.0 with Scale-out File Shares (SO) はい
Cluster Shared Volume File System (CsvFS) はい
Resilient File System (ReFS) はい
メモ

winbase.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetBinaryType を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じ、コンパイルエラーまたは実行時エラーを引き起こす可能性があります。詳細については、「関数プロトタイプの規則」を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL GetBinaryTypeW(
    LPCWSTR lpApplicationName,
    DWORD* lpBinaryType
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool GetBinaryTypeW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpApplicationName,   // LPCWSTR
    out uint lpBinaryType   // DWORD* out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetBinaryTypeW(
    <MarshalAs(UnmanagedType.LPWStr)> lpApplicationName As String,   ' LPCWSTR
    <Out> ByRef lpBinaryType As UInteger   ' DWORD* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpApplicationName : LPCWSTR
' lpBinaryType : DWORD* out
Declare PtrSafe Function GetBinaryTypeW Lib "kernel32" ( _
    ByVal lpApplicationName As LongPtr, _
    ByRef lpBinaryType As Long) 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

GetBinaryTypeW = ctypes.windll.kernel32.GetBinaryTypeW
GetBinaryTypeW.restype = wintypes.BOOL
GetBinaryTypeW.argtypes = [
    wintypes.LPCWSTR,  # lpApplicationName : LPCWSTR
    ctypes.POINTER(wintypes.DWORD),  # lpBinaryType : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetBinaryTypeW = Fiddle::Function.new(
  lib['GetBinaryTypeW'],
  [
    Fiddle::TYPE_VOIDP,  # lpApplicationName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpBinaryType : DWORD* out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn GetBinaryTypeW(
        lpApplicationName: *const u16,  // LPCWSTR
        lpBinaryType: *mut u32  // DWORD* out
    ) -> 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 GetBinaryTypeW([MarshalAs(UnmanagedType.LPWStr)] string lpApplicationName, out uint lpBinaryType);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetBinaryTypeW' -Namespace Win32 -PassThru
# $api::GetBinaryTypeW(lpApplicationName, lpBinaryType)
#uselib "KERNEL32.dll"
#func global GetBinaryTypeW "GetBinaryTypeW" wptr, wptr
; GetBinaryTypeW lpApplicationName, varptr(lpBinaryType)   ; 戻り値は stat
; lpApplicationName : LPCWSTR -> "wptr"
; lpBinaryType : DWORD* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global GetBinaryTypeW "GetBinaryTypeW" wstr, var
; res = GetBinaryTypeW(lpApplicationName, lpBinaryType)
; lpApplicationName : LPCWSTR -> "wstr"
; lpBinaryType : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetBinaryTypeW(LPCWSTR lpApplicationName, DWORD* lpBinaryType)
#uselib "KERNEL32.dll"
#cfunc global GetBinaryTypeW "GetBinaryTypeW" wstr, var
; res = GetBinaryTypeW(lpApplicationName, lpBinaryType)
; lpApplicationName : LPCWSTR -> "wstr"
; lpBinaryType : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetBinaryTypeW = kernel32.NewProc("GetBinaryTypeW")
)

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

extern "kernel32" fn GetBinaryTypeW(
    lpApplicationName: [*c]const u16, // LPCWSTR
    lpBinaryType: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetBinaryTypeW(
    lpApplicationName: WideCString,  # LPCWSTR
    lpBinaryType: ptr uint32  # DWORD* out
): int32 {.importc: "GetBinaryTypeW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "kernel32");
extern(Windows)
int GetBinaryTypeW(
    const(wchar)* lpApplicationName,   // LPCWSTR
    uint* lpBinaryType   // DWORD* out
);
ccall((:GetBinaryTypeW, "KERNEL32.dll"), stdcall, Int32,
      (Cwstring, Ptr{UInt32}),
      lpApplicationName, lpBinaryType)
# lpApplicationName : LPCWSTR -> Cwstring
# lpBinaryType : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetBinaryTypeW(
    const uint16_t* lpApplicationName,
    uint32_t* lpBinaryType);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetBinaryTypeW(lpApplicationName, lpBinaryType)
-- lpApplicationName : LPCWSTR
-- lpBinaryType : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetBinaryTypeW = lib.func('__stdcall', 'GetBinaryTypeW', 'int32_t', ['str16', 'uint32_t *']);
// GetBinaryTypeW(lpApplicationName, lpBinaryType)
// lpApplicationName : LPCWSTR -> 'str16'
// lpBinaryType : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetBinaryTypeW: { parameters: ["buffer", "pointer"], result: "i32" },
});
// lib.symbols.GetBinaryTypeW(lpApplicationName, lpBinaryType)
// lpApplicationName : LPCWSTR -> "buffer"
// lpBinaryType : DWORD* out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetBinaryTypeW(
    const uint16_t* lpApplicationName,
    uint32_t* lpBinaryType);
C, "KERNEL32.dll");
// $ffi->GetBinaryTypeW(lpApplicationName, lpBinaryType);
// lpApplicationName : LPCWSTR
// lpBinaryType : DWORD* 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 Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean GetBinaryTypeW(
        WString lpApplicationName,   // LPCWSTR
        IntByReference lpBinaryType   // DWORD* out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("kernel32")]
lib LibKERNEL32
  fun GetBinaryTypeW = GetBinaryTypeW(
    lpApplicationName : UInt16*,   # LPCWSTR
    lpBinaryType : UInt32*   # DWORD* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetBinaryTypeWNative = Int32 Function(Pointer<Utf16>, Pointer<Uint32>);
typedef GetBinaryTypeWDart = int Function(Pointer<Utf16>, Pointer<Uint32>);
final GetBinaryTypeW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetBinaryTypeWNative, GetBinaryTypeWDart>('GetBinaryTypeW');
// lpApplicationName : LPCWSTR -> Pointer<Utf16>
// lpBinaryType : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetBinaryTypeW(
  lpApplicationName: PWideChar;   // LPCWSTR
  lpBinaryType: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetBinaryTypeW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetBinaryTypeW"
  c_GetBinaryTypeW :: CWString -> Ptr Word32 -> IO CInt
-- lpApplicationName : LPCWSTR -> CWString
-- lpBinaryType : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getbinarytypew =
  foreign "GetBinaryTypeW"
    ((ptr uint16_t) @-> (ptr uint32_t) @-> returning int32_t)
(* lpApplicationName : LPCWSTR -> (ptr uint16_t) *)
(* lpBinaryType : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetBinaryTypeW" get-binary-type-w :convention :stdcall) :int32
  (lp-application-name (:string :encoding :utf-16le))   ; LPCWSTR
  (lp-binary-type :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetBinaryTypeW = Win32::API::More->new('KERNEL32',
    'BOOL GetBinaryTypeW(LPCWSTR lpApplicationName, LPVOID lpBinaryType)');
# my $ret = $GetBinaryTypeW->Call($lpApplicationName, $lpBinaryType);
# lpApplicationName : LPCWSTR -> LPCWSTR
# lpBinaryType : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目