How to Use GetCOnfigString Function in New Library

How can I use the _GetConfigString function of the new Toradex library in C#? I don’t see any example in the library help file. Only set functions are described

For example “Sys_GetConfigString” is defined in TdxAllLibraries.cs as

public static extern bool Sys_GetConfigString(IntPtr hSys, string paramName, string pValue,  UInt32 maxBytes);

So how do I get, for example, the BuildDate, a 19-character long string?

string tmp = "";
sysinfo.Sys_GetConfigString(hSys, "BuildDate", tmp, 19); ??

pValue should be a ref or out parameter, but either works.

Did you already tried to pre allocate the string? I think this is necessary as it shown here for VB .

This kind of fixed length strings are removed from .NET C#. I can “allocate” the string with Nulls but it doesn’t work.
I think there is something else wrong. If I look at UInt32 (which works) a ref is used:

[DllImport("TdxAllLibrariesDll.dll")]
public static extern UInt32 Sys_GetConfigInt(IntPtr hSys, string paramName, ref UInt32 Value);

with GetConfigString(…) there is no ref or out, which should be used in my opinion.

[DllImport("TdxAllLibrariesDll.dll")]
public static extern bool Sys_GetConfigString(IntPtr hSys, string paramName, string pValue,  UInt32 maxBytes);

ref is used to pass an argument as a reference (pointer), so the C/C++ function can change the value in place. This is true for an integer. But, since C/C++ has no concept of “string” type, strings are just pointers to a null-terminated char array. So they don’t need the reference.
You may pre-allocate memory for your string using a StringBuilder and pass it to the API.

Hi Valter.
Yes I could use StringBuilder but I can’t pass a StringBuilder object to the function. It expects a string. Converting the StringBuilder object to a string doesn’t work. Did I oversee something?

You can change the prototype to accept a StringBuilder instead, the .NET runtime should do the marshaling automatically, passing the buffer associated with the StringBuilder to the function. The only thing you have to care about is to pre-allocate this buffer since C APIs usually expect that memory is allocated and released by the caller.
Also allocating a String with:

String parm=new String("",size);

should work.