473,811 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("netc df.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(U nmanagedType.LP TStr)] StringBuilder varName);

Then the call:
StringBuilder varName = new StringBuilder(1 00);
int status = nc_inq_varname( NCID, varID, varName);

The status comes back as 0, which is a success. varName.ToStrin g() 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 3522
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("netc df.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(U nmanagedType.LP TStr)] StringBuilder varName);

Then the call:
StringBuilder varName = new StringBuilder(1 00);
int status = nc_inq_varname( NCID, varID, varName);

The status comes back as 0, which is a success. varName.ToStrin g() 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("netc df.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(U nmanagedType.LP TStr)] StringBuilder varName);

Then the call:
StringBuilde r varName = new StringBuilder(1 00);
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.ToStrin g() 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******** ******@TK2MSFTN GP02.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("netc df.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(U nmanagedType.LP TStr)] StringBuilder varName);

Then the call:
StringBuilde r varName = new StringBuilder(1 00);
int status = nc_inq_varname( NCID, varID, varName);

The status comes back as 0, which is a success. varName.ToStrin g() 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******@coldm ail.comwrote in message
news:p4******** *************** *********@4ax.c om...
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("netc df.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(U nmanagedType.LP TStr)] StringBuilder varName);

Then the call:
StringBuild er varName = new StringBuilder(1 00);
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.ToStrin g() 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******** ******@TK2MSFTN GP06.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("netc df.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(U nmanagedType.LP TStr)] StringBuilder varName);

Then the call:
StringBuilder varName = new StringBuilder(1 00);
int status = nc_inq_varname( NCID, varID, varName);

The status comes back as 0, which is a success. varName.ToStrin g() 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_floa t (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_floa t (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.o rgwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Now we're fighting with another function in the same DLL. The signature
is:

int nc_get_var_floa t (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_floa t (int ncid, int varid, float *fp).

Our declaration is:

[DllImport("netc df.dll")]
public static extern int nc_get_var_floa t(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_floa t(NCID, varID, ref fValues);

And the message we get on the call is:

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

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

>
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("netc df.dll")]
public static extern int nc_get_var_floa t(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.o rgwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Our declaration is:

[DllImport("netc df.dll")]
public static extern int nc_get_var_floa t(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
1632
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 appreciated.
5
10044
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
1722
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 get the error: Cannot convert a managed type to an unmanaged type
7
9546
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 gives no problems whatsoever. However, upon shutdown an exception pops up: "The string binding is invalid" If I call the singleton method from inside a purely unmanaged class, I
3
3235
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 other. i have two functions in the same .cpp file //exception occurs at this function void call(const char* var2 ) {
15
4643
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
7475
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 methods in CLI return System::String^. I originally just gcnew'd System::String^ passing in the c_str(). But I can't really have as many gcnew's as I'm using for overhead and for fear of leaks. So my question is this, how can I get the char*...
10
1691
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 'input string was not in a correct format.' Code: foreach (GridViewRow row in grid.Rows) { CheckBox chk = (CheckBox)gr.FindControl("checkbox"); if (chk.Checked) {
13
2883
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 class /method is imported from a C++ DLL generated by vc 6.0 { std::string* inputString = new string();
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.