MEHNAZ HUSSAIN
MEHNAZ HUSSAIN

Reputation: 81

How to load a dll into VS c++, which acts like a wrapper to another CAPL code?

I am trying to reference functions in a 3rd party dll file through CAPL Script. Since, I cannot directly call them, I am trying to create a wrapper which exports the functions in the dll.

int MA_Init(char *TbName, int Option); is the function in the dll file.

The wrapper code for this is

int CAPLEXPORT far CAPLPASCAL CMA_Init(char *TbName, int Option)
{
return MA_Init(*TbName, Option);
}

I am trying to use

HINSTANCE DllHandel = loadlibrary("C:\\Turbo.dll"); to load the library and

typedef int(*TESTFnptr)(char, int); TESTFnptr fn= (TESTFnptr)getprocaddress(DllHandle, "MA_Init"); to resolve the function address.

However the compiler says the function "MA_Init()" is not defined. I am not sure if I am using the correct procedure to load the dll into my visual C++ project. Has anyone tried doing this or knows how it's done? Thank you very much.

Upvotes: 2

Views: 671

Answers (1)

Dmitry Grigoryev
Dmitry Grigoryev

Reputation: 3203

The standard procedure would be to include the corresponding .lib file to VS project. Go to "Project - Properties - Configuration Properties - Linker - Additional Dependencies" and add turbo.lib on a new line. Then you'll need to include the corresponding turbo.h header file which contains the definition for MA_Init function.

In this case, you'll be able to call MA_Init directly, as you do now. The compiler will happily find the definition of MA_Init in the header file, and the linker will find the reference to MA_Init in the .lib file.

If you don't have turbo.h file, you can create one yourself provided you know the prototypes of all functions you want to use. Just put definitions like

int MA_Init(char *TbName, int Option);

there and include it.

If you don't have turbo.lib file, you'll have to proceed with LoadLibrary and GetProcAddress. Obviously, you cannot call MA_Init by name in this case, since it is undefined. You'll have to call the pointer returned by GetProcAddress instead:

TESTFnptr fn = (TESTFnptr)GetProcAddress(DllHandle, "MA_Init");
int CAPLEXPORT far CAPLPASCAL CMA_Init(char *TbName, int Option)
{
    return fn(TbName, Option);
}

PS. Notice I removed the start in front of TbName?

PPS. Don't forget to include your wrapper function, CMA_Init, to CAPL_DLL_INFO_LIST, otherwise it will not be accessible in CANoe/CANalyzer.

Upvotes: 0

Related Questions