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

GetModuleHandleA

関数
読み込み済みモジュールのハンドルをANSI名で取得する。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

HMODULE GetModuleHandleA(
    LPCSTR lpModuleName   // optional
);

パラメーター

名前方向
lpModuleNameLPCSTRinoptional

戻り値の型: HMODULE

各言語での呼び出し定義

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

HMODULE GetModuleHandleA(
    LPCSTR lpModuleName   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern IntPtr GetModuleHandleA(
    [MarshalAs(UnmanagedType.LPStr)] string lpModuleName   // LPCSTR optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetModuleHandleA(
    <MarshalAs(UnmanagedType.LPStr)> lpModuleName As String   ' LPCSTR optional
) As IntPtr
End Function
' lpModuleName : LPCSTR optional
Declare PtrSafe Function GetModuleHandleA Lib "kernel32" ( _
    ByVal lpModuleName As String) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetModuleHandleA = ctypes.windll.kernel32.GetModuleHandleA
GetModuleHandleA.restype = ctypes.c_void_p
GetModuleHandleA.argtypes = [
    wintypes.LPCSTR,  # lpModuleName : LPCSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetModuleHandleA = Fiddle::Function.new(
  lib['GetModuleHandleA'],
  [
    Fiddle::TYPE_VOIDP,  # lpModuleName : LPCSTR optional
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "kernel32")]
extern "system" {
    fn GetModuleHandleA(
        lpModuleName: *const u8  // LPCSTR optional
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr GetModuleHandleA([MarshalAs(UnmanagedType.LPStr)] string lpModuleName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetModuleHandleA' -Namespace Win32 -PassThru
# $api::GetModuleHandleA(lpModuleName)
#uselib "KERNEL32.dll"
#func global GetModuleHandleA "GetModuleHandleA" sptr
; GetModuleHandleA lpModuleName   ; 戻り値は stat
; lpModuleName : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global GetModuleHandleA "GetModuleHandleA" str
; res = GetModuleHandleA(lpModuleName)
; lpModuleName : LPCSTR optional -> "str"
; HMODULE GetModuleHandleA(LPCSTR lpModuleName)
#uselib "KERNEL32.dll"
#cfunc global GetModuleHandleA "GetModuleHandleA" str
; res = GetModuleHandleA(lpModuleName)
; lpModuleName : LPCSTR optional -> "str"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetModuleHandleA = kernel32.NewProc("GetModuleHandleA")
)

// lpModuleName (LPCSTR optional)
r1, _, err := procGetModuleHandleA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpModuleName))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HMODULE
function GetModuleHandleA(
  lpModuleName: PAnsiChar   // LPCSTR optional
): THandle; stdcall;
  external 'KERNEL32.dll' name 'GetModuleHandleA';
result := DllCall("KERNEL32\GetModuleHandleA"
    , "AStr", lpModuleName   ; LPCSTR optional
    , "Ptr")   ; return: HMODULE
●GetModuleHandleA(lpModuleName) = DLL("KERNEL32.dll", "void* GetModuleHandleA(char*)")
# 呼び出し: GetModuleHandleA(lpModuleName)
# lpModuleName : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。