0

I am trying to call a DLL (created with LabView) from .NET. I've succeeded for most of the methods, but one requires a string array as an input.

Labview supposedly uses C calling conventions when creating DLLs but the function prototype does not use a char pointer as I would expect but rather an LStrHandleArray:

void SetOutputUnits(LStrHandleArray *OutputUnits)

Does anyone know what reference I need to include to be able to use LStrHandleArray from within .NET?

Googling LStrHandleArray yields only 19 results, most of which are unanswered questions on other forums. I've added every National Instruments reference I can think of but I don't see LStrHandleArray and I can't find any documentation on it online.

G. Lester
  • 1
  • 1

2 Answers2

0

The definition of the type is as follows:

/** @brief Long Pascal-style string types. */
typedef struct {
    int32   cnt;        /* number of bytes that follow */
    uChar   str[1];     /* cnt bytes */
} LStr, *LStrPtr, **LStrHandle;
typedef struct _LStrArray {
    int32 nElts;
    LStrHandle  str[1];
} LStrArray, **LStrArrayHandle;

Note that the use of 1 is the C technique to make an array type be allocated flat inline instead of in a separate pointer. The same declaration is used regardless of the length of the array or length of the string. You allocate enough memory accommodate the full size of the array/string... indexes deliberately go beyond the declared allocation size. It's something unheard of in managed languages, but this is the low-level structural declaration that the compiler uses.

To invoke from .NET code, you'll need to create a wrapper DLL that takes a flattened form of the data that you marshal across as a string or other marshallable data structure and then unflatten\construct in the C++ code. Both the array handle itself and the internal string handles have to be allocated with DSNewHandle() or DSNewHClr() from the extcode.h header file so that they're in the memory space that LabVIEW can manipulate.

This example shows invoking the DLL from another C DLL, but it has other helpful discussion.

PS: LabVIEW DOES use C calling conventions. That refers to the order of parameters on the stack and the fact that the data structures are all definable with PODs (plain old C data types). It does not imply anything about the data structures used in the interfaces.

srm
  • 3,062
  • 16
  • 30
0

This is an old post, but since there is no accepted answer... LStrHandleArray is represented as a flattened 2-D array of char, with known dimension sizes. You can use array of StringBuilder to send input to LStrHandleArray. This works because StringBuilder allocates memory for the string and keeps track of its capacity size, increasing as needed.

Just make sure charset is ANSI when importing your Labview DLL!

https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder?view=netcore-3.1