0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VBAのDeclare文を64bit版Officeに対応させる:PtrSafeとLongPtrにする引数の見分け方

0
Posted at

Office を 64bit 版にしたら、Windows API を使っているマクロが次のエラーで動かなくなった——という場合の直し方をまとめます。

コンパイル エラー:
このプロジェクトのコードは、64 ビット システムで使用するように更新する必要があります。
Declare ステートメントを確認、更新し、PtrSafe 属性でマークしてください。

結論

  1. Declare に PtrSafe を付ける
  2. ハンドル・ポインタ・サイズを表す引数と戻り値を Long → LongPtr にする
  3. それを受け取る変数も LongPtr にする
  4. 32bit 版と両対応にするなら #If VBA7 Then で分岐する

PtrSafe を付けるだけでもコンパイルは通りますが、ハンドルを Long のままにすると 64bit 版では値が切り捨てられ、動いたり動かなかったりする厄介なバグになります。

どの引数を LongPtr にするか

Win32 API のドキュメント上の型で判断します。

API上の型 意味 VBA(64bit対応)
HWND, HDC, HINSTANCE, HMODULE, HMENU, HANDLE など H で始まる型 ハンドル LongPtr
LPVOID, LPCVOID, * が付くポインタ(数値で渡す場合) ポインタ LongPtr
WPARAM, LPARAM, LRESULT ポインタ幅の整数 LongPtr
SIZE_T, ULONG_PTR, INT_PTR ポインタ幅の整数 LongPtr
BOOL, INT, UINT, LONG, DWORD 32bit 整数 Long のまま
LPCSTR / LPSTR(文字列) 文字列 String のまま(ByVal ... As String)

書き換え例

修正前(32bit 版のみ)

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

修正後(32bit / 64bit 両対応)

#If VBA7 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (Destination As Any, Source As Any, ByVal Length As LongPtr)
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

ポイント:

  • FindWindow の戻り値は HWND なので LongPtr
  • SendMessage は hWnd・wParam・戻り値(LRESULT)が LongPtr。wMsg(UINT)は Long のまま
  • CopyMemory の Length は SIZE_T なので LongPtr
  • Sleep の引数は DWORD なので Long のまま(PtrSafe だけ付ける)

受け取る変数も LongPtr に

#If VBA7 Then
    Dim hWnd As LongPtr
#Else
    Dim hWnd As Long
#End If
hWnd = FindWindow(vbNullString, "電卓")

ここを Long のままにすると、64bit 版では値が切り捨てられたり、ByRef で渡す箇所が「ByRef 引数の型が一致しません」のコンパイルエラーになったりします。

VBA7 と Win64 の違い

定数 True になる環境 用途
VBA7 Office 2010 以降(32bit・64bit とも) PtrSafe・LongPtr が使えるかの判定
Win64 64bit 版 Office 64bit でだけ名前が違う API の切り替え

通常は #If VBA7 で十分です。Win64 が必要になるのは、64bit 版でだけ別の関数を呼ぶケースです。

#If Win64 Then
    Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongPtrA" _
        (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
#ElseIf VBA7 Then
    Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" _
        (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
#Else
    Private Declare Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" _
        (ByVal hWnd As Long, ByVal nIndex As Long) As Long
#End If

GetWindowLongPtrA / SetWindowLongPtrA は 64bit の user32.dll にしか存在しないため、32bit 版では GetWindowLongA / SetWindowLongA を呼びます。

Declare 以外で 64bit 版に移行するときの注意

  • ActiveX コントロール:古いコントロールの一部は 64bit 版がない、またはPCに登録されていないため、フォームが開けなくなることがあります(MSCOMCTL.OCX の ListView・TreeView などは、新しい Microsoft 365 では 64bit 版が提供されていますが、Office のバージョンによっては使えません)
  • 外部の 32bit DLL:自作・他社製の 32bit DLL は 64bit 版 Office から呼べません

まとめ

  • PtrSafe を付けるだけでなく、ハンドル・ポインタ・サイズは LongPtr にする
  • 受け取る変数も LongPtr にする
  • 両対応は #If VBA7、64bit 専用の API は #If Win64

ブックがたくさんあって、どこに Declare があるのか洗い出すだけで大変……という場合のために、マクロ入りのブックやフォルダをまとめて検査して、修正案(PtrSafe・LongPtr を入れた Declare 文)を出すツールも作っています。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?