1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【win32metadata / CsWin32】ANSI版 Win32APIの定義が生成されないことの解決策

Last updated at Posted at 2021-10-22

予備知識

「win32metadata」とは

Win32API を手軽に呼び出すためのメタデータ(?)らしい。

「CsWin32」とは

前述の「win32metadata」を使い、C# 用に、Win32API 関係の定義を自動で生成してくれるソースジェネレーター。

(プロジェクト直下に「NativeMethods.txt」というファイルを作成し、その中に改行区切りで Win32API の名前を書くだけで、定義が自動で生成される)

本題

問題

NativeMethods.txt に Win32API の名前を書いていたところ、なぜか ANSI 版の Win32API(末尾に"A"が付くやつ)だけ、定義が生成されないことに気づいた。

NativeMethods.txt
// 生成される
SendMessage
SendMessageW

// 生成されない...
SendMessageA

解決策

プロジェクト直下に「NativeMethods.json」(CsWin32 が生成するコードを設定するためのファイル)を作成し、下のように記述する。

NativeMethods.json
{
  "$schema": "https://aka.ms/CsWin32.schema.json",
  "wideCharOnly": false
}

どうやら、デフォルトで wideCharOnliy(UTF-16 版の Win32API のみ生成)が true らしく、ANSI 版の Win32API が生成されなかったみたい。

副作用

NativeMethods.txt に末尾 A / W を省略して書いた場合、省略した名前ではなく、UTF-16 版(末尾にWが付く)の名前で定義されるようになる。

例えば、下のように書いた場合、従来ではそのまま「SendMessage」と定義されるが、「SendMessageW」と定義されるようになる。

NativeMethods.txt
SendMessage

※ 当たり前だが、そもそも ANSI 版・ UTF-16 版がない Win32API に影響はない

1
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?