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

GetAtomNameW

関数
ローカルアトムに関連付けられた文字列を取得する(Unicode)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

DWORD GetAtomNameW(
    WORD nAtom,
    LPWSTR lpBuffer,
    INT nSize
);

パラメーター

名前方向説明
nAtomWORDin取得する文字列を識別するローカルアトム。
lpBufferLPWSTRout文字列。
nSizeINTinバッファのサイズ(文字数単位)。

戻り値の型: DWORD

公式ドキュメント

指定したローカルアトムに関連付けられた文字列のコピーを取得します。(Unicode)

戻り値

型: UINT

関数が成功した場合、戻り値はバッファにコピーされた文字列の長さ(文字数単位、終端の null 文字は含みません)です。

関数が失敗した場合、戻り値は 0 です。詳細なエラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

整数アトム(値が 0x0001 から 0xBFFF の範囲にあるアトム)に対して返される文字列は、最初の文字がポンド記号(#)で、残りの文字が符号なし整数のアトム値を表す null 終端文字列です。

セキュリティに関する考慮事項

この関数を誤って使用すると、プログラムのセキュリティが損なわれる可能性があります。誤った使用には、lpBuffer パラメーターのサイズを正しく指定しないことが含まれます。
メモ

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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>

DWORD GetAtomNameW(
    WORD nAtom,
    LPWSTR lpBuffer,
    INT nSize
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint GetAtomNameW(
    ushort nAtom,   // WORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer,   // LPWSTR out
    int nSize   // INT
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetAtomNameW(
    nAtom As UShort,   ' WORD
    <MarshalAs(UnmanagedType.LPWStr)> lpBuffer As System.Text.StringBuilder,   ' LPWSTR out
    nSize As Integer   ' INT
) As UInteger
End Function
' nAtom : WORD
' lpBuffer : LPWSTR out
' nSize : INT
Declare PtrSafe Function GetAtomNameW Lib "kernel32" ( _
    ByVal nAtom As Integer, _
    ByVal lpBuffer As LongPtr, _
    ByVal nSize 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

GetAtomNameW = ctypes.windll.kernel32.GetAtomNameW
GetAtomNameW.restype = wintypes.DWORD
GetAtomNameW.argtypes = [
    ctypes.c_ushort,  # nAtom : WORD
    wintypes.LPWSTR,  # lpBuffer : LPWSTR out
    ctypes.c_int,  # nSize : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetAtomNameW = Fiddle::Function.new(
  lib['GetAtomNameW'],
  [
    -Fiddle::TYPE_SHORT,  # nAtom : WORD
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPWSTR out
    Fiddle::TYPE_INT,  # nSize : INT
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn GetAtomNameW(
        nAtom: u16,  // WORD
        lpBuffer: *mut u16,  // LPWSTR out
        nSize: i32  // INT
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetAtomNameW(ushort nAtom, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer, int nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetAtomNameW' -Namespace Win32 -PassThru
# $api::GetAtomNameW(nAtom, lpBuffer, nSize)
#uselib "KERNEL32.dll"
#func global GetAtomNameW "GetAtomNameW" wptr, wptr, wptr
; GetAtomNameW nAtom, varptr(lpBuffer), nSize   ; 戻り値は stat
; nAtom : WORD -> "wptr"
; lpBuffer : LPWSTR out -> "wptr"
; nSize : INT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global GetAtomNameW "GetAtomNameW" int, var, int
; res = GetAtomNameW(nAtom, lpBuffer, nSize)
; nAtom : WORD -> "int"
; lpBuffer : LPWSTR out -> "var"
; nSize : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD GetAtomNameW(WORD nAtom, LPWSTR lpBuffer, INT nSize)
#uselib "KERNEL32.dll"
#cfunc global GetAtomNameW "GetAtomNameW" int, var, int
; res = GetAtomNameW(nAtom, lpBuffer, nSize)
; nAtom : WORD -> "int"
; lpBuffer : LPWSTR out -> "var"
; nSize : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetAtomNameW = kernel32.NewProc("GetAtomNameW")
)

// nAtom (WORD), lpBuffer (LPWSTR out), nSize (INT)
r1, _, err := procGetAtomNameW.Call(
	uintptr(nAtom),
	uintptr(lpBuffer),
	uintptr(nSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetAtomNameW(
  nAtom: Word;   // WORD
  lpBuffer: PWideChar;   // LPWSTR out
  nSize: Integer   // INT
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetAtomNameW';
result := DllCall("KERNEL32\GetAtomNameW"
    , "UShort", nAtom   ; WORD
    , "Ptr", lpBuffer   ; LPWSTR out
    , "Int", nSize   ; INT
    , "UInt")   ; return: DWORD
●GetAtomNameW(nAtom, lpBuffer, nSize) = DLL("KERNEL32.dll", "dword GetAtomNameW(int, char*, int)")
# 呼び出し: GetAtomNameW(nAtom, lpBuffer, nSize)
# nAtom : WORD -> "int"
# lpBuffer : LPWSTR out -> "char*"
# nSize : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef GetAtomNameWNative = Uint32 Function(Uint16, Pointer<Utf16>, Int32);
typedef GetAtomNameWDart = int Function(int, Pointer<Utf16>, int);
final GetAtomNameW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetAtomNameWNative, GetAtomNameWDart>('GetAtomNameW');
// nAtom : WORD -> Uint16
// lpBuffer : LPWSTR out -> Pointer<Utf16>
// nSize : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetAtomNameW(
  nAtom: Word;   // WORD
  lpBuffer: PWideChar;   // LPWSTR out
  nSize: Integer   // INT
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetAtomNameW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetAtomNameW"
  c_GetAtomNameW :: Word16 -> CWString -> Int32 -> IO Word32
-- nAtom : WORD -> Word16
-- lpBuffer : LPWSTR out -> CWString
-- nSize : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getatomnamew =
  foreign "GetAtomNameW"
    (uint16_t @-> (ptr uint16_t) @-> int32_t @-> returning uint32_t)
(* nAtom : WORD -> uint16_t *)
(* lpBuffer : LPWSTR out -> (ptr uint16_t) *)
(* nSize : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetAtomNameW" get-atom-name-w :convention :stdcall) :uint32
  (n-atom :uint16)   ; WORD
  (lp-buffer :pointer)   ; LPWSTR out
  (n-size :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetAtomNameW = Win32::API::More->new('KERNEL32',
    'DWORD GetAtomNameW(WORD nAtom, LPWSTR lpBuffer, int nSize)');
# my $ret = $GetAtomNameW->Call($nAtom, $lpBuffer, $nSize);
# nAtom : WORD -> WORD
# lpBuffer : LPWSTR out -> LPWSTR
# nSize : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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