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

GetModuleHandleW

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

シグネチャ

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

HMODULE GetModuleHandleW(
    LPCWSTR lpModuleName   // optional
);

パラメーター

名前方向
lpModuleNameLPCWSTRinoptional

戻り値の型: HMODULE

各言語での呼び出し定義

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

HMODULE GetModuleHandleW(
    LPCWSTR lpModuleName   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern IntPtr GetModuleHandleW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpModuleName   // LPCWSTR optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetModuleHandleW(
    <MarshalAs(UnmanagedType.LPWStr)> lpModuleName As String   ' LPCWSTR optional
) As IntPtr
End Function
' lpModuleName : LPCWSTR optional
Declare PtrSafe Function GetModuleHandleW Lib "kernel32" ( _
    ByVal lpModuleName As LongPtr) As LongPtr
' 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

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

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetModuleHandleW = kernel32.NewProc("GetModuleHandleW")
)

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