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

GetModuleBaseNameA

関数
指定モジュールの基本名をANSI文字列で取得する。
DLLPSAPI.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// PSAPI.dll  (ANSI / -A)
#include <windows.h>

DWORD GetModuleBaseNameA(
    HANDLE hProcess,
    HMODULE hModule,   // optional
    LPSTR lpBaseName,
    DWORD nSize
);

パラメーター

名前方向
hProcessHANDLEin
hModuleHMODULEinoptional
lpBaseNameLPSTRout
nSizeDWORDin

戻り値の型: DWORD

各言語での呼び出し定義

// PSAPI.dll  (ANSI / -A)
#include <windows.h>

DWORD GetModuleBaseNameA(
    HANDLE hProcess,
    HMODULE hModule,   // optional
    LPSTR lpBaseName,
    DWORD nSize
);
[DllImport("PSAPI.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern uint GetModuleBaseNameA(
    IntPtr hProcess,   // HANDLE
    IntPtr hModule,   // HMODULE optional
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBaseName,   // LPSTR out
    uint nSize   // DWORD
);
<DllImport("PSAPI.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetModuleBaseNameA(
    hProcess As IntPtr,   ' HANDLE
    hModule As IntPtr,   ' HMODULE optional
    <MarshalAs(UnmanagedType.LPStr)> lpBaseName As System.Text.StringBuilder,   ' LPSTR out
    nSize As UInteger   ' DWORD
) As UInteger
End Function
' hProcess : HANDLE
' hModule : HMODULE optional
' lpBaseName : LPSTR out
' nSize : DWORD
Declare PtrSafe Function GetModuleBaseNameA Lib "psapi" ( _
    ByVal hProcess As LongPtr, _
    ByVal hModule As LongPtr, _
    ByVal lpBaseName As String, _
    ByVal nSize As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetModuleBaseNameA = ctypes.windll.psapi.GetModuleBaseNameA
GetModuleBaseNameA.restype = wintypes.DWORD
GetModuleBaseNameA.argtypes = [
    wintypes.HANDLE,  # hProcess : HANDLE
    wintypes.HANDLE,  # hModule : HMODULE optional
    wintypes.LPSTR,  # lpBaseName : LPSTR out
    wintypes.DWORD,  # nSize : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	psapi = windows.NewLazySystemDLL("PSAPI.dll")
	procGetModuleBaseNameA = psapi.NewProc("GetModuleBaseNameA")
)

// hProcess (HANDLE), hModule (HMODULE optional), lpBaseName (LPSTR out), nSize (DWORD)
r1, _, err := procGetModuleBaseNameA.Call(
	uintptr(hProcess),
	uintptr(hModule),
	uintptr(lpBaseName),
	uintptr(nSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetModuleBaseNameA(
  hProcess: THandle;   // HANDLE
  hModule: THandle;   // HMODULE optional
  lpBaseName: PAnsiChar;   // LPSTR out
  nSize: DWORD   // DWORD
): DWORD; stdcall;
  external 'PSAPI.dll' name 'GetModuleBaseNameA';
result := DllCall("PSAPI\GetModuleBaseNameA"
    , "Ptr", hProcess   ; HANDLE
    , "Ptr", hModule   ; HMODULE optional
    , "Ptr", lpBaseName   ; LPSTR out
    , "UInt", nSize   ; DWORD
    , "UInt")   ; return: DWORD
●GetModuleBaseNameA(hProcess, hModule, lpBaseName, nSize) = DLL("PSAPI.dll", "dword GetModuleBaseNameA(void*, void*, char*, dword)")
# 呼び出し: GetModuleBaseNameA(hProcess, hModule, lpBaseName, nSize)
# hProcess : HANDLE -> "void*"
# hModule : HMODULE optional -> "void*"
# lpBaseName : LPSTR out -> "char*"
# nSize : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。