473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting updated string from unmanaged DLL

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?
Jan 4 '07 #1
9 3498
CapCity wrote:
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?

More like an encoding issue to me.

tried all other options than Unicode - same result

What options you have tried, if you can list all the options you tired,
people here may be able to help you.

And what's the byte value of those ?, are they same ??

John
Jan 4 '07 #2
On Thu, 4 Jan 2007 07:21:10 -0500, "CapCity" <sgomori at yahoo dot
comwrote:
>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
Unicode covers a multitude of sins. Have you tried being more
specific: ASCII, UTF7, UTF8? EBCDIC would be a long shot I suspect.
>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);
Have you tried char[]? For disgnostic purposes you might also want to
try passing in a byte[] and have a look at what comes back as hex -
that might give you a better idea of what the dll is actually
returning.
>
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".
Try converting the return directly to a byte array to see what is
actually in there before you start trying to convert it. After
conversion you are seeing a mix of the return and the conversion
function. You need to start by looking just at the return from the
dll. Once you know exactly what is going into the conversion function
should you have a look at what is coming out of it.
>
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.
This may mean that there is UTF16 in there somewhere. UTF16 uses two
bytes per character rather than one byte for UTF7, UTF8 or ASCII. The
same length may just be due to overflow protection preventing writing
off the end of an array or string.

rossum
>
Any thoughts on how to get the string value correctly?
Jan 4 '07 #3

"Jianwei Sun" <js***********@gmail.comwrote in message
news:Oi**************@TK2MSFTNGP02.phx.gbl...
CapCity wrote:
>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?

More like an encoding issue to me.

tried all other options than Unicode - same result

What options you have tried, if you can list all the options you tired,
people here may be able to help you.
The options are Ansi, Auto, None and Unicode
>
And what's the byte value of those ?, are they same ??
The byte values of each of those is 63, the ascii code for "?"
>
John

Jan 4 '07 #4

"rossum" <ro******@coldmail.comwrote in message
news:p4********************************@4ax.com...
On Thu, 4 Jan 2007 07:21:10 -0500, "CapCity" <sgomori at yahoo dot
comwrote:
>>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
Unicode covers a multitude of sins. Have you tried being more
specific: ASCII, UTF7, UTF8? EBCDIC would be a long shot I suspect.
>>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);
Have you tried char[]? For disgnostic purposes you might also want to
try passing in a byte[] and have a look at what comes back as hex -
that might give you a better idea of what the dll is actually
returning.
>>
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".
Try converting the return directly to a byte array to see what is
actually in there before you start trying to convert it. After
conversion you are seeing a mix of the return and the conversion
function. You need to start by looking just at the return from the
dll. Once you know exactly what is going into the conversion function
should you have a look at what is coming out of it.
The byte values for those ? are 63, so they're actual ? and not just
something to represent something unprintable
>>
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.
This may mean that there is UTF16 in there somewhere. UTF16 uses two
bytes per character rather than one byte for UTF7, UTF8 or ASCII. The
same length may just be due to overflow protection preventing writing
off the end of an array or string.

rossum
>>
Any thoughts on how to get the string value correctly?

Jan 4 '07 #5

"CapCity" <sgomori at yahoo dot comwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
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?
Thanks to all who put thought into this. The solution to the above was to
have LPStr as the MarshalAs type, not LPTStr.
Now we're fighting with another function in the same DLL. The signature is:

int nc_get_var_float (int ncid, int varid, float *fp). We're looking to see
what we can use in C# to hold the array of floats. Just using an array of
floats gives some error code that MS says can be fixed by installing SP2 for
Win 2000 (we're running XP).

Jan 4 '07 #6
>Now we're fighting with another function in the same DLL. The signature is:
>
int nc_get_var_float (int ncid, int varid, float *fp). We're looking to see
what we can use in C# to hold the array of floats. Just using an array of
floats gives some error code that MS says can be fixed by installing SP2 for
Win 2000 (we're running XP).
A float[] should work fine. Can you post your declaration, calling
code and details on the error you get?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 4 '07 #7

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Now we're fighting with another function in the same DLL. The signature
is:

int nc_get_var_float (int ncid, int varid, float *fp). We're looking to
see
what we can use in C# to hold the array of floats. Just using an array of
floats gives some error code that MS says can be fixed by installing SP2
for
Win 2000 (we're running XP).

A float[] should work fine. Can you post your declaration, calling
code and details on the error you get?
Absolutely.
The documentation that came with the DLL has the signature as this:

int nc_get_var_float (int ncid, int varid, float *fp).

Our declaration is:

[DllImport("netcdf.dll")]
public static extern int nc_get_var_float(int ncid, int varid, ref float[]
fValues);
The call is this:

float[] fValues = { 0.0F }; //Build error if this array is not
initialized
status = nc_get_var_float(NCID, varID, ref fValues);

And the message we get on the call is:

The program '[496] EPAWA41_Dev2.vshost.exe: Managed' has exited with
code -1073741819 (0xc0000005).

We tried to add some marshalling to the declaration, but got a pop-up
'FatalExecutionEngineError'.

>
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 5 '07 #8
>Our declaration is:
>
[DllImport("netcdf.dll")]
public static extern int nc_get_var_float(int ncid, int varid, ref float[]
fValues);
Try it without the ref modifier.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 5 '07 #9

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Our declaration is:

[DllImport("netcdf.dll")]
public static extern int nc_get_var_float(int ncid, int varid, ref float[]
fValues);

Try it without the ref modifier.
That worked! I also had to declare the array with a fixed size, but it is
known ahead of time, so that's not too big of a deal. I think the last is a
"feature" with the DLL and not really a rule of thumb. The "ref" thing makes
sense now that you pointed it out.

Anyway - thank you!
>
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: rasa | last post by:
I am able to instantiate and make calls to a com module that use only primitive data types, but I simply don't know what the c# equivalent is for a function expecting short*. Any help would be...
5
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
4
by: quat | last post by:
Hello all, I have some unmanaged code that requires an HWND to a managed control (e.g., a picture box). I try: mRenderWnd->Handle; Of course, this returns an IntPtr. If I try to case, I...
7
by: Søren Dreijer | last post by:
Hi, I have a mixed C#, managed C++ and unmanaged C++ project. The managed class calls a method which exists in an unmanaged singleton class. During the entire lifetime of the application, this...
3
by: doubts | last post by:
Hi all, I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net. when using std::string type variable, the application causes exception at one instance and does not cause an exception at...
15
by: Mark C | last post by:
I know a string is immutable, but is there any trick or any other way to destroy a string Thanks www.quiznetonline.com
6
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my...
10
by: Mike | last post by:
I have code that is doing some updating to a record. Its getting the ID to update from the Grid. I'm passing an INT to my method to update the record. My code is working though I'm still getting an...
13
by: Creativ | last post by:
I've looked through this thread and still have quetions. Suppose In visual studio 2005, I write the following #pragam managed class ManagedWrapper { void CallUnmanagedMethod() // The unmanaged...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.