声明语句的 ptrSafe 属性的目的是什么?



在 VBA 中,如果我在 64 位 Office 环境中使用它,我需要在具有ptrSafe属性的 DLL 中declare一个函数。

据我所知,ptrSafe在技术上没有改变任何东西,所以我不确定ptrSafe属性的实际用途是什么。

PtrSafe用于告诉 VBA API 声明是安全的:Ptr (pointer)Safe (safe, safe)

此属性指示我们以 64 位版本为目标。

例如

ShellExecuteAPI 函数为例,该函数在不知情的情况下打开其默认应用程序中的任何文件。例如,.xlsx文档将在 Excel 中打开,.pdf将在 Acrobat Reader 中打开,除非您安装了其他 PDF 阅读器(如 Sumatra PDF)。

此函数的 API 声明已更改为64-bit版本。要使32 and 64-bit版本共存于同一数据库中,请编写以下内容:

' --- DECLARATION API WINDOWS
#If VBA7 Then
Private Declare PtrSafe Function ShellExecute _
Lib "shell32.dll" _
Alias "ShellExecuteA" ( _
ByVal hwnd As LongPtr, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) _
As LongPtr
#Else
Private Declare Function ShellExecute _
Lib "shell32.dll" _
Alias "ShellExecuteA" ( _
ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) _
As Long
#End If

原创内容可以在那里找到

最新更新