I have a huge XLL (more than a hundred of functions) coded (in C) using the Excel C API.
I need to use several functions of this XLL in a C# code.
The code of these functions is quite an intricate numerical code that I wrote during several months, several years ago, fairly tested, backtested and validated, so that I am not at all into recoding into C# these C functions.
Each function of the XLL has all its input parameters of one single type XLOPER * and returns a LPXLOPER, where for the sake of completion, we have :
typedef struct xloper
{
union
{
double num; /* xltypeNum */
LPSTR str; /* xltypeStr */
#ifdef __cplusplus
WORD xbool; /* xltypeBool */
#else
WORD bool; /* xltypeBool */
#endif
WORD err; /* xltypeErr */
short int w; /* xltypeInt */
struct
{
WORD count; /* always = 1 */
XLREF ref;
} sref; /* xltypeSRef */
struct
{
XLMREF *lpmref;
IDSHEET idSheet;
} mref; /* xltypeRef */
struct
{
struct xloper *lparray;
WORD rows;
WORD columns;
} array; /* xltypeMulti */
struct
{
union
{
short int level; /* xlflowRestart */
short int tbctrl; /* xlflowPause */
IDSHEET idSheet; /* xlflowGoto */
} valflow;
WORD rw; /* xlflowGoto */
BYTE col; /* xlflowGoto */
BYTE xlflow;
} flow; /* xltypeFlow */
struct
{
union
{
BYTE *lpbData; /* data passed to XL */
HANDLE hdata; /* data returned from XL */
} h;
long cbData;
} bigdata; /* xltypeBigData */
} val;
WORD xltype;
} XLOPER, *LPXLOPER;
(definition in the XLCALL.h file of the Excal C API.)
Each function F of the XLL takes parameters of the type XLOPER* and, according to what these paramters should represent, performs various checks on the parameters etc etc, to finally construct objects of various custom structs types, that are finally passed to an underlying C function f whose parameters are native C types or complicated stackings of structs and native types etc.
Marshalling theses structs (and structs of structs, structs of structs of ... of structs) would be an endless task. (And yes, it was and still is bad design to have so tightly coupled the "pure" C code to the Excel API C code ...)
This is why I want to marshall the XLOPER *, LPXLOPER and any types using to define them. An XLL being after all a DLL, It should be possible. (I have still a doubt about the XLL needing or not to be aware of the context of Excel using it, which could stop its use from a c# code ...)
I found code doing parts of this marshalling here :
https://www.demo2s.com/csharp/csharp-icustommarshaler-marshalnativetomanaged-intptr-pnativedata.html
and the namespace it lives in suggests it is a part of Excel DNA, yet I have not succeed in finding that code in the Excel-Dna GitHub repository, where I found traces of marshalling code, but only in internal classes, so that I am stuck.
(Naturally Excel-DNA hides all of this as it target is especially to alleviate the user from meddling with the Excel C Api but, as I am not an Excel-Dna expert, maybe Excel-Dna allows for such a marshalling.)