Vídeo recomendado
https://youtu.be/diWPPPhW-9E

Compatibilidade API 32 para 64 bits

Dúvidas gerais sobre Excel
Avatar do usuário
TARSA
Colaborador
Colaborador
Mensagens: 36
Registrado em: Dom Set 11, 2016 5:04 pm

Compatibilidade API 32 para 64 bits

Mensagem por TARSA »

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


Disable adblock

This site is supported by ads and donations.
If you see this text you are blocking our ads.
Please consider a Donation to support the site.


Avatar do usuário
Mikel Silveira Fraga
Jedi
Jedi
Mensagens: 1173
Registrado em: Sex Mai 27, 2011 3:27 pm
Localização: Governador Valadares - MG
Contato:

Re: Compatibilidade API 32 para 64 bits

Mensagem por Mikel Silveira Fraga »

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.


Avatar do usuário
TARSA
Colaborador
Colaborador
Mensagens: 36
Registrado em: Dom Set 11, 2016 5:04 pm

Re: Compatibilidade API 32 para 64 bits

Mensagem por TARSA »

Olá mestre Mikel,

Muito obrigado pela sua ajuda.


Avatar do usuário
TARSA
Colaborador
Colaborador
Mensagens: 36
Registrado em: Dom Set 11, 2016 5:04 pm

Re: Compatibilidade API 32 para 64 bits

Mensagem por TARSA »

Funcionou perfeitamente Mikel. Obg mais uma vez.


Disable adblock

This site is supported by ads and donations.
If you see this text you are blocking our ads.
Please consider a Donation to support the site.


Responder