//---------------------------------------------------------------------------
#include "PCAN_USB.H"
//---------------------------------------------------------------------------
HINSTANCE g_i_DLL;
char g_LibFileName[] = "PCAN_USB.DLL";
typedef DWORD (__stdcall *PCAN_Init)(WORD wBTR0BTR1, int CANMsgType);
typedef DWORD (__stdcall *PCAN_Close)();
typedef DWORD (__stdcall *PCAN_Status)();
typedef DWORD (__stdcall *PCAN_Write)(TPCANMsg* pMsgBuff);
typedef DWORD (__stdcall *PCAN_Read)(TPCANMsg* pMsgBuff);
typedef DWORD (__stdcall *PCAN_VersionInfo)(LPSTR lpversioninfo);
PCAN_Init g_CAN_Init;
PCAN_Close g_CAN_Close;
PCAN_Status g_CAN_Status;
PCAN_Write g_CAN_Write;
PCAN_Read g_CAN_Read;
PCAN_VersionInfo g_CAN_VersionInfo;
//---------------------------------------------------------------------------
bool __fastcall InitCan(void)
{
if(Load_dll() == false)
{
Application->MessageBox
("The required file 'PCAN_USB.DLL' can not be loaded?", "error", MB_ICONSTOP);
return false;
}
return true;
}
//---------------------------------------------------------------------------
bool Load_dll(void)
{
if(g_i_DLL == NULL)
{
g_i_DLL = LoadLibrary(g_LibFileName);
if(g_i_DLL == NULL)
return false;
if(GetFunctionAdress(g_i_DLL) == false)
return false;
}
return true;
}
//---------------------------------------------------------------------------
bool GetFunctionAdress(HINSTANCE h_module)
{
if(h_module == NULL)
return false;
g_CAN_Init = (PCAN_Init) GetProcAddress(h_module, "CAN_Init");
if(g_CAN_Init == NULL)
return false;
g_CAN_Close = (PCAN_Close) GetProcAddress(h_module, "CAN_Close");
if(g_CAN_Close == NULL)
return false;
g_CAN_Status = (PCAN_Status) GetProcAddress(h_module, "CAN_Status");
if(g_CAN_Status == NULL)
return false;
g_CAN_Write = (PCAN_Write) GetProcAddress(h_module, "CAN_Write");
if(g_CAN_Write == NULL)
return false;
g_CAN_Read = (PCAN_Read) GetProcAddress(h_module, "CAN_Read");
if(g_CAN_Read == NULL)
return false;
g_CAN_VersionInfo = (PCAN_VersionInfo) GetProcAddress(h_module, "CAN_VersionInfo");
if(g_CAN_VersionInfo == NULL)
return false;
return true;
}
//---------------------------------------------------------------------------
void __fastcall Close_can(TObject *Sender)
{
if(g_i_DLL)
{
FreeLibrary(g_i_DLL);
g_i_DLL = NULL;
g_CAN_Init = NULL;
g_CAN_Close = NULL;
g_CAN_Status = NULL;
g_CAN_Write = NULL;
g_CAN_Read = NULL;
g_CAN_VersionInfo = NULL;
}
}
//---------------------------------------------------------------------------