Compatibilidade API 32 para 64 bits

TARSA

02-07-2017 15:11:02

Pessoal, necessito de ajuda para finalizar uma API que roda perfeitamente em 32 bits, porém em
Office 64 (Excel) está apresentando erro de compatibilidade.
Ela é:

Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Tentei adaptar para 64 bits com o código abaixo, mas continua dando erro.
Alguém poderia me dar uma ajuda?

#If VBA7 Then
Private Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr

#If Win64 Then
Private Declare PtrSafe Function GetWindowLongA Lib "user32" (ByVal hWnd As LongPtr, ByVal nIndex As LongPtr) As LongPtr

#Else
Private Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long


#End If
#Else

Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If

Mikel Silveira Fraga

03-07-2017 03:29:26

Tarsa, boa noite.

Essa questão de Compatibilidade de API já é uma velha história que aterroriza a comunidade de Desenvolvedores VBA. Por outro lado, não é algo muito complicado de se resolver.

Conforme mencionado, você usa a sequência de códigos no 32 bits, conforme abaixo:

Código: Selecionar todos

Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Para fazer com que esse código rode no 64 bits, é necessário apenas adicionar a palavra chave PtrSafe entre as palavras Declare e Function, deixando o código da seguinte forma:

Código: Selecionar todos

Private Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Além dessa situação, existe a questão de que a sua planilha pode ser aberta por usuários diferentes em sistemas com arquiteturas diferentes e, por esse motivo, temos que adequar o código, para que rode nas duas arquiteturas, sem ter que ficar a todo instante alterando o código da planilha. Para isso, vamos adicionar um bloco #If, que permite fazer esse tipo de situação. Neste bloco, será testada a versão do VBA que esta rodando no sistema, conforme abaixo:

Código: Selecionar todos

#IF VBA7 THEN
	Private Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
	Private Declare PtrSafe Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
	Private Declare PtrSafe Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#ELSEIF VBA6 THEN ' pode ser utilizado aqui apenas o #ELSE.
	Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
	Private Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
	Private Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#END IF
Desta maneira, seu código irá rodar em todas as versões 32 bits e 64 bits, sem erros.

Teste e nos retorne. Boa noite e excelente início de semana.

TARSA

09-07-2017 18:09:20

Olá mestre Mikel,

Muito obrigado pela sua ajuda.

TARSA

14-07-2017 16:46:49

Funcionou perfeitamente Mikel. Obg mais uma vez.