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

GetAtomNameA

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

シグネチャ

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

DWORD GetAtomNameA(
    WORD nAtom,
    LPSTR lpBuffer,
    INT nSize
);

パラメーター

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

戻り値の型: DWORD

公式ドキュメント

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

戻り値

型: UINT

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

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

解説(Remarks)

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

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

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

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

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

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

GetAtomNameA = ctypes.windll.kernel32.GetAtomNameA
GetAtomNameA.restype = wintypes.DWORD
GetAtomNameA.argtypes = [
    ctypes.c_ushort,  # nAtom : WORD
    wintypes.LPSTR,  # lpBuffer : LPSTR 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')
GetAtomNameA = Fiddle::Function.new(
  lib['GetAtomNameA'],
  [
    -Fiddle::TYPE_SHORT,  # nAtom : WORD
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPSTR out
    Fiddle::TYPE_INT,  # nSize : INT
  ],
  -Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn GetAtomNameA(
        nAtom: u16,  // WORD
        lpBuffer: *mut u8,  // LPSTR out
        nSize: i32  // INT
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern uint GetAtomNameA(ushort nAtom, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer, int nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetAtomNameA' -Namespace Win32 -PassThru
# $api::GetAtomNameA(nAtom, lpBuffer, nSize)
#uselib "KERNEL32.dll"
#func global GetAtomNameA "GetAtomNameA" sptr, sptr, sptr
; GetAtomNameA nAtom, varptr(lpBuffer), nSize   ; 戻り値は stat
; nAtom : WORD -> "sptr"
; lpBuffer : LPSTR out -> "sptr"
; nSize : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global GetAtomNameA "GetAtomNameA" int, var, int
; res = GetAtomNameA(nAtom, lpBuffer, nSize)
; nAtom : WORD -> "int"
; lpBuffer : LPSTR out -> "var"
; nSize : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD GetAtomNameA(WORD nAtom, LPSTR lpBuffer, INT nSize)
#uselib "KERNEL32.dll"
#cfunc global GetAtomNameA "GetAtomNameA" int, var, int
; res = GetAtomNameA(nAtom, lpBuffer, nSize)
; nAtom : WORD -> "int"
; lpBuffer : LPSTR out -> "var"
; nSize : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetAtomNameA = kernel32.NewProc("GetAtomNameA")
)

// nAtom (WORD), lpBuffer (LPSTR out), nSize (INT)
r1, _, err := procGetAtomNameA.Call(
	uintptr(nAtom),
	uintptr(lpBuffer),
	uintptr(nSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetAtomNameA(
  nAtom: Word;   // WORD
  lpBuffer: PAnsiChar;   // LPSTR out
  nSize: Integer   // INT
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetAtomNameA';
result := DllCall("KERNEL32\GetAtomNameA"
    , "UShort", nAtom   ; WORD
    , "Ptr", lpBuffer   ; LPSTR out
    , "Int", nSize   ; INT
    , "UInt")   ; return: DWORD
●GetAtomNameA(nAtom, lpBuffer, nSize) = DLL("KERNEL32.dll", "dword GetAtomNameA(int, char*, int)")
# 呼び出し: GetAtomNameA(nAtom, lpBuffer, nSize)
# nAtom : WORD -> "int"
# lpBuffer : LPSTR out -> "char*"
# nSize : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn GetAtomNameA(
    nAtom: u16, // WORD
    lpBuffer: [*c]u8, // LPSTR out
    nSize: i32 // INT
) callconv(std.os.windows.WINAPI) u32;
proc GetAtomNameA(
    nAtom: uint16,  # WORD
    lpBuffer: ptr char,  # LPSTR out
    nSize: int32  # INT
): uint32 {.importc: "GetAtomNameA", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
uint GetAtomNameA(
    ushort nAtom,   // WORD
    char* lpBuffer,   // LPSTR out
    int nSize   // INT
);
ccall((:GetAtomNameA, "KERNEL32.dll"), stdcall, UInt32,
      (UInt16, Ptr{UInt8}, Int32),
      nAtom, lpBuffer, nSize)
# nAtom : WORD -> UInt16
# lpBuffer : LPSTR out -> Ptr{UInt8}
# nSize : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetAtomNameA(
    uint16_t nAtom,
    char* lpBuffer,
    int32_t nSize);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetAtomNameA(nAtom, lpBuffer, nSize)
-- nAtom : WORD
-- lpBuffer : LPSTR out
-- nSize : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetAtomNameA = lib.func('__stdcall', 'GetAtomNameA', 'uint32_t', ['uint16_t', 'char *', 'int32_t']);
// GetAtomNameA(nAtom, lpBuffer, nSize)
// nAtom : WORD -> 'uint16_t'
// lpBuffer : LPSTR out -> 'char *'
// nSize : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetAtomNameA: { parameters: ["u16", "buffer", "i32"], result: "u32" },
});
// lib.symbols.GetAtomNameA(nAtom, lpBuffer, nSize)
// nAtom : WORD -> "u16"
// lpBuffer : LPSTR out -> "buffer"
// nSize : INT -> "i32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetAtomNameA(
    uint16_t nAtom,
    char* lpBuffer,
    int32_t nSize);
C, "KERNEL32.dll");
// $ffi->GetAtomNameA(nAtom, lpBuffer, nSize);
// nAtom : WORD
// lpBuffer : LPSTR 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.ASCII_OPTIONS);
    int GetAtomNameA(
        short nAtom,   // WORD
        byte[] lpBuffer,   // LPSTR out
        int nSize   // INT
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun GetAtomNameA = GetAtomNameA(
    nAtom : UInt16,   # WORD
    lpBuffer : UInt8*,   # LPSTR 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 GetAtomNameANative = Uint32 Function(Uint16, Pointer<Utf8>, Int32);
typedef GetAtomNameADart = int Function(int, Pointer<Utf8>, int);
final GetAtomNameA = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetAtomNameANative, GetAtomNameADart>('GetAtomNameA');
// nAtom : WORD -> Uint16
// lpBuffer : LPSTR out -> Pointer<Utf8>
// nSize : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetAtomNameA(
  nAtom: Word;   // WORD
  lpBuffer: PAnsiChar;   // LPSTR out
  nSize: Integer   // INT
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetAtomNameA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let getatomnamea =
  foreign "GetAtomNameA"
    (uint16_t @-> string @-> int32_t @-> returning uint32_t)
(* nAtom : WORD -> uint16_t *)
(* lpBuffer : LPSTR out -> string *)
(* 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 ("GetAtomNameA" get-atom-name-a :convention :stdcall) :uint32
  (n-atom :uint16)   ; WORD
  (lp-buffer :pointer)   ; LPSTR out
  (n-size :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetAtomNameA = Win32::API::More->new('KERNEL32',
    'DWORD GetAtomNameA(WORD nAtom, LPSTR lpBuffer, int nSize)');
# my $ret = $GetAtomNameA->Call($nAtom, $lpBuffer, $nSize);
# nAtom : WORD -> WORD
# lpBuffer : LPSTR out -> LPSTR
# nSize : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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