Объявить заявление в Win64 VBA Office

Я использовал код VBA в Windows 32 бит. Теперь, когда я перешел на 64-битную Windows 10, я получил сообщение "Код в этом проекте должен быть обновлен для использования в 64-битных системах. Пожалуйста, просмотрите и обновите операторы Declare, а затем пометьте их атрибутом PtrSafe". У меня есть следующие команды объявлять:

Public Declare Function GetUserNameEx Lib "Secur32.dll" Alias "GetUserNameExA" ( _
  ByVal NameFormat As EXTENDED_NAME_FORMAT, _
  ByVal lpNameBuffer As String, _
  ByRef lpnSize As Long) As Long

Public Enum EXTENDED_NAME_FORMAT
  NameUnknown = 0
  NameFullyQualifiedDN = 1
  NameSamCompatible = 2
  NameDisplay = 3
  NameUniqueId = 6
  NameCanonical = 7
  NameUserPrincipal = 8
  NameCanonicalEx = 9
  NameServicePrincipal = 10
  NameDnsDomain = 12
End Enum

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Public 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

Я посмотрел на некоторую документацию, но я не могу понять, когда используются операторы LongLong и LongPtr. Или, если это только объявить PtrSafe в заявлении Declare.

Ссылка на документацию MicroSoft https://msdn.microsoft.com/en-us/library/office/ee691831(v=office.14).aspx

Кто-нибудь может мне помочь?

2 ответа

Вы используете LongPtr, когда он касается адреса памяти, и Long, когда это просто число. Включайте PtrSafe всякий раз, когда вы используете LongPtr в своем объявлении. Таким образом, ваша декларация ShellExecute будет (проверена):

#If VBA7 Then
Public 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
Public 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

Попробуйте заменить их на эти (непроверенные)


GetUserNameEx 64 бит / 64 бит

Public Enum EXTENDED_NAME_FORMAT
  NameUnknown = 0
  NameFullyQualifiedDN = 1
  NameSamCompatible = 2
  NameDisplay = 3
  NameUniqueId = 6
  NameCanonical = 7
  NameUserPrincipal = 8
  NameCanonicalEx = 9
  NameServicePrincipal = 10
  NameDnsDomain = 12
End Enum

#If VBA7 Then
    Public Declare PtrSafe Function GetUserNameEx Lib "Secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef lpnSize As Long) As Long
#Else
    Public Declare Function GetUserNameEx Lib "Secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef lpnSize As Long) As Long
#End If

GetOpenFileName 64 бит / 64 бит

Type OPENFILENAME
        lStructSize As Long
        hwndOwner As LongPtr
        hInstance As LongPtr
        lpstrFilter As String
        lpstrCustomFilter As String
        nMaxCustFilter As Long
        nFilterIndex As Long
        lpstrFile As String
        nMaxFile As Long
        lpstrFileTitle As String
        nMaxFileTitle As Long
        lpstrInitialDir As String
        lpstrTitle As String
        flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        lpstrDefExt As String
        lCustData As LongPtr
        lpfnHook As LongPtr
        lpTemplateName As String
'#if (_WIN32_WINNT >= 0x0500)
        pvReserved As LongPtr
        dwReserved As Long
        FlagsEx As Long
'#endif // (_WIN32_WINNT >= 0x0500)
End Type

#If VBA7 Then
    Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
#Else
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
#End If

ShellExecute 64 бит / 64 бит

#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
Другие вопросы по тегам