C
CapCity
We're rewritting an app using C# 2005 and it needs to read files in netCDF
format. A dll is available for this and we've had success in calling its
functions, unless it updates strings. We have tried several of the
suggestions we've found on-line: Strings, StringBuilders, IntPtr, etc., but
haven't been able to exactly pull it off.
What seem to be closest is the following. It's a function that will return a
variable name for a given file ID and variable ID. We first have this:
[DllImport("netcdf.dll", CharSet = CharSet.Unicode)] //Also have
tried all other options than Unicode - same result
public static extern int nc_inq_varname(int ncid, int varid,
[Out,MarshalAs(UnmanagedType.LPTStr)] StringBuilder varName);
Then the call:
StringBuilder varName = new StringBuilder(100);
int status = nc_inq_varname(NCID, varID, varName);
The status comes back as 0, which is a success. varName.ToString() gives us
"????m????????9" but we know the name to be "TEMP_1p5m". The file has a
another variable, TFLAG, but using that varID gives back a variable name of
"??G??????????9".
Both returned values are the same length. And oddly enough, the "m" in the
first one, and the "G" in the second one, are recognizable in the known
values. And if each of the leading "?" were to represent 2 characters of the
known value then the "m" and "G" are in the right place.
Any thoughts on how to get the string value correctly?
format. A dll is available for this and we've had success in calling its
functions, unless it updates strings. We have tried several of the
suggestions we've found on-line: Strings, StringBuilders, IntPtr, etc., but
haven't been able to exactly pull it off.
What seem to be closest is the following. It's a function that will return a
variable name for a given file ID and variable ID. We first have this:
[DllImport("netcdf.dll", CharSet = CharSet.Unicode)] //Also have
tried all other options than Unicode - same result
public static extern int nc_inq_varname(int ncid, int varid,
[Out,MarshalAs(UnmanagedType.LPTStr)] StringBuilder varName);
Then the call:
StringBuilder varName = new StringBuilder(100);
int status = nc_inq_varname(NCID, varID, varName);
The status comes back as 0, which is a success. varName.ToString() gives us
"????m????????9" but we know the name to be "TEMP_1p5m". The file has a
another variable, TFLAG, but using that varID gives back a variable name of
"??G??????????9".
Both returned values are the same length. And oddly enough, the "m" in the
first one, and the "G" in the second one, are recognizable in the known
values. And if each of the leading "?" were to represent 2 characters of the
known value then the "m" and "G" are in the right place.
Any thoughts on how to get the string value correctly?