I am trying to use a third-party DLL written in ANSI C (compiled as C++). One of the functions has the following C++ prototype:
tResultCode GetAttrib
(
tHandle handle,
tAttrib attrib,
void * value,
tAttribType valueType,
int valueMaxSize
);
Where the arguments of GetAttrib are as follows:
handleis a void pointer to an opaque structure within the DLLattribindicates which attribute to retrieve from the structurevaluea pointer to a variable of the type indicated byvalueTypevalueTypeindicates the C++ type of the buffer allocated to hold the attribute valuevalueMaxSizeis the number of bytes allocated to variable pointed to byvalue
The eAttribType enumeration is defined as follows:
typedef enum eAttribType
{
eHandle,
eBool,
eEnum,
eInt,
eLong,
eFloat,
eDouble,
eDate,
eTime,
eTimestamp,
eString, // char *
eChar, // char
eVoid,
eHandle,
eFunctionPointer
} tAttribType;
The eAttrib enumeration is defined as follows:
typedef enum eAttrib
{
eInvoiceNumber, // eString
eInvoiceDate, // eTimestamp
eUnits, // eLong
ePrice, // eFloat
eDiscount, // eDouble
ePreferredFlag, // eBool
...
} tAttrib;
I declare the unmanaged function pointer in C# as follows:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate eResultCode getAttrib(void** handle, eAttrib attrib, void* value, eAttribType attribType, int valueMaxSize);
internal unsafe getAttrib GetAttrib;
Where:
handleis avoid **for the pointer to the C++ library structureattribmimics the C++ enumeration that indicates the attribute to retrievevalueis avoid *to the C++ variableattribTypemimics the C++ enumeration that indicated the C++ type ofvaluevalueMaxSizeis an integer meant to represent the size of the variable pointed to byvalue
My problem is that I don't know how call GetAttrib() from C# with different data types for the void * (the value variable). Sometimes this variable will be a C++ char *, and other times it will be a C++ int, and still other times it will be a C++ enum, etc
If someone can show me how to properly allocate/populate a char * (eString) and a long (eLong) from C# I think that the rest will fall into place (hopefilly).