Categorias

Dll em ADVPL

Segue um descritivo de como usar .dll em advpl.

É possível executar qualquer DLL usando ADVPL, desde que a DLL respeite os itens abaixo.

1  – A DLL, deve possuir o seguinte método a ser exportada:

EXEMPLO C++:

int DLL_EXPORT ExecInClientDLL(int nParamHdl, CHAR* cParameters, CHAR* cBuffer, int nSizeBuffer)

2 – Caso a sua DLL seja em C, C++ o cabeçalho .h deve possuir :

Código

#ifndef __MAIN_H__ 
#define __MAIN_H__ 
#include  
#ifdef BUILD_DLL 
#define DLL_EXPORT __declspec(dllexport) 
#else 
#define DLL_EXPORT __declspec(dllimport) 
#endif
#ifdef __cplusplus
 
extern "C" 
{
 #endif 
int DLL_EXPORT  ExecInClientDLL(int nParamHdl, CHAR* cParameters, CHAR* cBuffer, int nSizeBuffer);
 #ifdef __cplusplus
 }
 
#endif
 #endif // __MAIN_H__

Source C++ :

Código

#include "main.h" 
#ifdef DLLDIR_EX 
#define DLLDIR  __declspec(dllexport)
#else
#define DLLDIR  __declspec(dllimport)
#endif
 
int DLL_EXPORT  ExecInClientDLL(int _FunctionId, CHAR* _Parameters, CHAR* _Buffer, int _SizeBuffer )
{
MessageBoxA(0, _Parameters, "DLL Message", MB_OK | MB_ICONINFORMATION);
return 1 ;
}
 
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
 
return TRUE; // succesful
 
}
3 – Caso seja em Delphi criar a interface :

function  ExecInClientDLL(nParamHdl : integer ; cParameters : Pchar ; cBuffer : Pchar; nSizeBuffer : integer ) : integer ; stdcall ; export ;

E os parâmetros  int nParamHdl, CHAR* cParameters, CHAR* cBuffer, int nSizeBuffer?

Int nParamHdl	Valor retornado pela chamada da DLL
cParameters	Qualquer valor string a ser executado, ou tratado pela DLL
cBuffer	Cadeia de caracters a ser usado na DLL
nSizeBuffer	Tamanho do Buffer – de acordo com cBuffer (Caracters)
Chamamando a DLL

Código

#include "protheus.ch"
 
User Function TLIBEX
 
local nHandle := 0
 
local cRetDLL   := 0
 
nHandle := ExecInDllOpen('tlib.dll')
 
//O nHandle – ver o parametro Int nParamHdl na DLL, se for -1: Erro na DLL, ou não 
 
//  a DLL não encontra-se na pasta: Remote ou SmartCLient ( P10)
 
cRetDLL := ExecInDLLRun( nHandle, 2, 'MINHA DLL !!!' )
 
MsgStop( 'RETORNO: ' + cRetDLL )
 
ExecInDLLClose(nHandle)
 
Return()