mayaknife
mayaknife

Reputation: 302

How to retrieve array variable from DLL? (Visual C++)

A 3rd party C library contains the following global variable and function:

typedef RtBasis float[4][4];

RI_EXPORT RtBasis RiCatmullRomBasis;
RI_EXPORT void    RiBasis(RtBasis u);

I want to retrieve the variable and function from the DLL and use them in my C++ code. When I do the following:

// Get the function.
void (*RiBasis)(RtBasis);
RiBasis = (void (*)(RtBasis))GetProcAddress(h, "RiBasis");

// Get the variable.
RtBasis *RiCatmullRomBasis;
RiCatmullRomBasis = (RtBasis*)GetProcAddress(h, "RiCatmullRomBasis");

// Call the function, passing it the variable.
RiBasis(RiCatmullRomBasis);

Visual C++ gives this compile error on the call to RiBasis:

error C2664: 'void (float [][4])': cannot convert argument 1
from 'RtBasis (*)' to 'float [][4]'

I tried removing one level of indirection from the RiCatmullRomBasis variable:

// Get the variable.
RtBasis RiCatmullRomBasis;
RiCatmullRomBasis = (RtBasis)GetProcAddress(h, "RiCatmullRomBasis");

// Call the function, passing it the variable.
RiBasis(RiCatmullRomBasis);

but that gave me the following on the GetProcAddress call:

error C2440: 'type cast': cannot convert from 'FARPROC' to 'RtBasis'
note: There are no conversions to array types, although there are
conversions to references or pointers to arrays

What's the correct way of declaring the types in my C++ code to make this work?

Upvotes: 1

Views: 263

Answers (1)

SoronelHaetir
SoronelHaetir

Reputation: 15162

In the first version make the call as:

RiBasis(*RiCatmullRomBasis);

You need to get the address of the variable (that is what GetProcAddress can return), but the function takes an instance rather than a pointer so you have to dereference the returned pointer.

Upvotes: 2

Related Questions