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

GetVersionFromProcess

関数
指定したプロセスに読み込まれているCLRバージョンを取得する。
DLLMSCorEE.dll呼出規約winapi

シグネチャ

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

HRESULT GetVersionFromProcess(
    HANDLE hProcess,
    LPWSTR pVersion,
    DWORD cchBuffer,
    DWORD* dwLength
);

パラメーター

名前方向説明
hProcessHANDLEinCLR バージョンを取得する対象プロセスのハンドル。
pVersionLPWSTRout取得したバージョン文字列を受け取る出力バッファ。Unicode 文字列で返される。
cchBufferDWORDinpVersion のサイズ。文字数で指定する。
dwLengthDWORD*out実際に書き込まれた文字数を受け取る出力ポインタ。

戻り値の型: HRESULT

各言語での呼び出し定義

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

HRESULT GetVersionFromProcess(
    HANDLE hProcess,
    LPWSTR pVersion,
    DWORD cchBuffer,
    DWORD* dwLength
);
[DllImport("MSCorEE.dll", ExactSpelling = true)]
static extern int GetVersionFromProcess(
    IntPtr hProcess,   // HANDLE
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pVersion,   // LPWSTR out
    uint cchBuffer,   // DWORD
    out uint dwLength   // DWORD* out
);
<DllImport("MSCorEE.dll", ExactSpelling:=True)>
Public Shared Function GetVersionFromProcess(
    hProcess As IntPtr,   ' HANDLE
    <MarshalAs(UnmanagedType.LPWStr)> pVersion As System.Text.StringBuilder,   ' LPWSTR out
    cchBuffer As UInteger,   ' DWORD
    <Out> ByRef dwLength As UInteger   ' DWORD* out
) As Integer
End Function
' hProcess : HANDLE
' pVersion : LPWSTR out
' cchBuffer : DWORD
' dwLength : DWORD* out
Declare PtrSafe Function GetVersionFromProcess Lib "mscoree" ( _
    ByVal hProcess As LongPtr, _
    ByVal pVersion As LongPtr, _
    ByVal cchBuffer As Long, _
    ByRef dwLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetVersionFromProcess = ctypes.windll.mscoree.GetVersionFromProcess
GetVersionFromProcess.restype = ctypes.c_int
GetVersionFromProcess.argtypes = [
    wintypes.HANDLE,  # hProcess : HANDLE
    wintypes.LPWSTR,  # pVersion : LPWSTR out
    wintypes.DWORD,  # cchBuffer : DWORD
    ctypes.POINTER(wintypes.DWORD),  # dwLength : DWORD* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('MSCorEE.dll')
GetVersionFromProcess = Fiddle::Function.new(
  lib['GetVersionFromProcess'],
  [
    Fiddle::TYPE_VOIDP,  # hProcess : HANDLE
    Fiddle::TYPE_VOIDP,  # pVersion : LPWSTR out
    -Fiddle::TYPE_INT,  # cchBuffer : DWORD
    Fiddle::TYPE_VOIDP,  # dwLength : DWORD* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "mscoree")]
extern "system" {
    fn GetVersionFromProcess(
        hProcess: *mut core::ffi::c_void,  // HANDLE
        pVersion: *mut u16,  // LPWSTR out
        cchBuffer: u32,  // DWORD
        dwLength: *mut u32  // DWORD* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("MSCorEE.dll")]
public static extern int GetVersionFromProcess(IntPtr hProcess, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pVersion, uint cchBuffer, out uint dwLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MSCorEE_GetVersionFromProcess' -Namespace Win32 -PassThru
# $api::GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength)
#uselib "MSCorEE.dll"
#func global GetVersionFromProcess "GetVersionFromProcess" sptr, sptr, sptr, sptr
; GetVersionFromProcess hProcess, varptr(pVersion), cchBuffer, varptr(dwLength)   ; 戻り値は stat
; hProcess : HANDLE -> "sptr"
; pVersion : LPWSTR out -> "sptr"
; cchBuffer : DWORD -> "sptr"
; dwLength : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MSCorEE.dll"
#cfunc global GetVersionFromProcess "GetVersionFromProcess" sptr, var, int, var
; res = GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength)
; hProcess : HANDLE -> "sptr"
; pVersion : LPWSTR out -> "var"
; cchBuffer : DWORD -> "int"
; dwLength : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD* dwLength)
#uselib "MSCorEE.dll"
#cfunc global GetVersionFromProcess "GetVersionFromProcess" intptr, var, int, var
; res = GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength)
; hProcess : HANDLE -> "intptr"
; pVersion : LPWSTR out -> "var"
; cchBuffer : DWORD -> "int"
; dwLength : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mscoree = windows.NewLazySystemDLL("MSCorEE.dll")
	procGetVersionFromProcess = mscoree.NewProc("GetVersionFromProcess")
)

// hProcess (HANDLE), pVersion (LPWSTR out), cchBuffer (DWORD), dwLength (DWORD* out)
r1, _, err := procGetVersionFromProcess.Call(
	uintptr(hProcess),
	uintptr(pVersion),
	uintptr(cchBuffer),
	uintptr(dwLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function GetVersionFromProcess(
  hProcess: THandle;   // HANDLE
  pVersion: PWideChar;   // LPWSTR out
  cchBuffer: DWORD;   // DWORD
  dwLength: Pointer   // DWORD* out
): Integer; stdcall;
  external 'MSCorEE.dll' name 'GetVersionFromProcess';
result := DllCall("MSCorEE\GetVersionFromProcess"
    , "Ptr", hProcess   ; HANDLE
    , "Ptr", pVersion   ; LPWSTR out
    , "UInt", cchBuffer   ; DWORD
    , "Ptr", dwLength   ; DWORD* out
    , "Int")   ; return: HRESULT
●GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength) = DLL("MSCorEE.dll", "int GetVersionFromProcess(void*, char*, dword, void*)")
# 呼び出し: GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength)
# hProcess : HANDLE -> "void*"
# pVersion : LPWSTR out -> "char*"
# cchBuffer : DWORD -> "dword"
# dwLength : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "mscoree" fn GetVersionFromProcess(
    hProcess: ?*anyopaque, // HANDLE
    pVersion: [*c]u16, // LPWSTR out
    cchBuffer: u32, // DWORD
    dwLength: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
proc GetVersionFromProcess(
    hProcess: pointer,  # HANDLE
    pVersion: ptr uint16,  # LPWSTR out
    cchBuffer: uint32,  # DWORD
    dwLength: ptr uint32  # DWORD* out
): int32 {.importc: "GetVersionFromProcess", stdcall, dynlib: "MSCorEE.dll".}
pragma(lib, "mscoree");
extern(Windows)
int GetVersionFromProcess(
    void* hProcess,   // HANDLE
    wchar* pVersion,   // LPWSTR out
    uint cchBuffer,   // DWORD
    uint* dwLength   // DWORD* out
);
ccall((:GetVersionFromProcess, "MSCorEE.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{UInt16}, UInt32, Ptr{UInt32}),
      hProcess, pVersion, cchBuffer, dwLength)
# hProcess : HANDLE -> Ptr{Cvoid}
# pVersion : LPWSTR out -> Ptr{UInt16}
# cchBuffer : DWORD -> UInt32
# dwLength : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetVersionFromProcess(
    void* hProcess,
    uint16_t* pVersion,
    uint32_t cchBuffer,
    uint32_t* dwLength);
]]
local mscoree = ffi.load("mscoree")
-- mscoree.GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength)
-- hProcess : HANDLE
-- pVersion : LPWSTR out
-- cchBuffer : DWORD
-- dwLength : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('MSCorEE.dll');
const GetVersionFromProcess = lib.func('__stdcall', 'GetVersionFromProcess', 'int32_t', ['void *', 'uint16_t *', 'uint32_t', 'uint32_t *']);
// GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength)
// hProcess : HANDLE -> 'void *'
// pVersion : LPWSTR out -> 'uint16_t *'
// cchBuffer : DWORD -> 'uint32_t'
// dwLength : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("MSCorEE.dll", {
  GetVersionFromProcess: { parameters: ["pointer", "buffer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength)
// hProcess : HANDLE -> "pointer"
// pVersion : LPWSTR out -> "buffer"
// cchBuffer : DWORD -> "u32"
// dwLength : DWORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetVersionFromProcess(
    void* hProcess,
    uint16_t* pVersion,
    uint32_t cchBuffer,
    uint32_t* dwLength);
C, "MSCorEE.dll");
// $ffi->GetVersionFromProcess(hProcess, pVersion, cchBuffer, dwLength);
// hProcess : HANDLE
// pVersion : LPWSTR out
// cchBuffer : DWORD
// dwLength : 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 Mscoree extends StdCallLibrary {
    Mscoree INSTANCE = Native.load("mscoree", Mscoree.class);
    int GetVersionFromProcess(
        Pointer hProcess,   // HANDLE
        char[] pVersion,   // LPWSTR out
        int cchBuffer,   // DWORD
        IntByReference dwLength   // DWORD* out
    );
}
@[Link("mscoree")]
lib LibMSCorEE
  fun GetVersionFromProcess = GetVersionFromProcess(
    hProcess : Void*,   # HANDLE
    pVersion : UInt16*,   # LPWSTR out
    cchBuffer : UInt32,   # DWORD
    dwLength : 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 GetVersionFromProcessNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Uint32, Pointer<Uint32>);
typedef GetVersionFromProcessDart = int Function(Pointer<Void>, Pointer<Utf16>, int, Pointer<Uint32>);
final GetVersionFromProcess = DynamicLibrary.open('MSCorEE.dll')
    .lookupFunction<GetVersionFromProcessNative, GetVersionFromProcessDart>('GetVersionFromProcess');
// hProcess : HANDLE -> Pointer<Void>
// pVersion : LPWSTR out -> Pointer<Utf16>
// cchBuffer : DWORD -> Uint32
// dwLength : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetVersionFromProcess(
  hProcess: THandle;   // HANDLE
  pVersion: PWideChar;   // LPWSTR out
  cchBuffer: DWORD;   // DWORD
  dwLength: Pointer   // DWORD* out
): Integer; stdcall;
  external 'MSCorEE.dll' name 'GetVersionFromProcess';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetVersionFromProcess"
  c_GetVersionFromProcess :: Ptr () -> CWString -> Word32 -> Ptr Word32 -> IO Int32
-- hProcess : HANDLE -> Ptr ()
-- pVersion : LPWSTR out -> CWString
-- cchBuffer : DWORD -> Word32
-- dwLength : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getversionfromprocess =
  foreign "GetVersionFromProcess"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint32_t) @-> returning int32_t)
(* hProcess : HANDLE -> (ptr void) *)
(* pVersion : LPWSTR out -> (ptr uint16_t) *)
(* cchBuffer : DWORD -> uint32_t *)
(* dwLength : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library mscoree (t "MSCorEE.dll"))
(cffi:use-foreign-library mscoree)

(cffi:defcfun ("GetVersionFromProcess" get-version-from-process :convention :stdcall) :int32
  (h-process :pointer)   ; HANDLE
  (p-version :pointer)   ; LPWSTR out
  (cch-buffer :uint32)   ; DWORD
  (dw-length :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetVersionFromProcess = Win32::API::More->new('MSCorEE',
    'int GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, LPVOID dwLength)');
# my $ret = $GetVersionFromProcess->Call($hProcess, $pVersion, $cchBuffer, $dwLength);
# hProcess : HANDLE -> HANDLE
# pVersion : LPWSTR out -> LPWSTR
# cchBuffer : DWORD -> DWORD
# dwLength : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。