473,406 Members | 2,847 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,406 software developers and data experts.

Marshalling a char* inside a struct with null characters :(

Typical story, I want to call an unmanaged c DLL from a c# app. I have most
of the methods working, there is one that is stubborn.

<c code>

union DEVICE_T
{
/// this buffer holds the complete device information
/// and is overlayed by the following information structure
CHAR buffer[80];
struct
{
WORD endian;
WORD id;
BYTE string[32];
WORD mainStart;
WORD infoStart;
WORD ramEnd;
WORD nBreakpoints;
WORD emulation;
WORD clockControl;
WORD lcdStart;
WORD lcdEnd;
WORD vccMinOp;
WORD vccMaxOp;
WORD hasTestVpp;
WORD ramStart;
WORD ram2Start;
WORD ram2End;
WORD infoEnd;
ULONG mainEnd;
};
};
Here is the function signature that I'm trying to call:
MSP430_Identify(CHAR* buffer, LONG count, LONG setId);

</c code>
And here is my C# code:

<C# code>

[DllImport(m_dllName)]
public static extern int MSP430_Identify(
[MarshalAs(UnmanagedType.LPStr)]StringBuilder buffer, int count, int setId);
Here is the code where I call it:
StringBuilder identityBuffer = new StringBuilder(80, 80);
TIMSP430Driver.MSP430_Identify( identityBuffer, 80, 0);

</C# code>
The StringBuilder approach is the only one I have been able to get any sort
of result from. ost combinations of ref, out, char[] and string cause
exceptions, but the StringBuilder method gives me 3 characters and they are
valid. The catch is, since this isn't a valid string but rather a byte
buffer, there are some 0 character in the string and that is stopping the
StringBuilder from getting the entire results. I need to tell it to get all
bytes, not just until it hit a terminator.

An example of the correct result is:
[0] 85 'U' char
[1] -86 'ª' char
[2] 5 '?' char
[3] 0 char
[4] 77 'M' char
[5] 83 'S' char
[6] 80 'P' char
[7] 52 '4' char
[8] 51 '3' char
[9] 48 '0' char
[10] 70 'F' char
[11] 49 '1' char
[12] 52 '4' char
[13] 55 '7' char
[14] 0 char
... etc
What do I need to do to get the StringBuilder to cooperate with me? Any
suggestions welcome.
Thanks!
Steve
Feb 25 '06 #1
1 2663
I was able to get this to work by using a byte[].

"Steve" <ss*@sss.com> wrote in message
news:eI**************@TK2MSFTNGP10.phx.gbl...
Typical story, I want to call an unmanaged c DLL from a c# app. I have
most of the methods working, there is one that is stubborn.

<c code>

union DEVICE_T
{
/// this buffer holds the complete device information
/// and is overlayed by the following information structure
CHAR buffer[80];
struct
{
WORD endian;
WORD id;
BYTE string[32];
WORD mainStart;
WORD infoStart;
WORD ramEnd;
WORD nBreakpoints;
WORD emulation;
WORD clockControl;
WORD lcdStart;
WORD lcdEnd;
WORD vccMinOp;
WORD vccMaxOp;
WORD hasTestVpp;
WORD ramStart;
WORD ram2Start;
WORD ram2End;
WORD infoEnd;
ULONG mainEnd;
};
};
Here is the function signature that I'm trying to call:
MSP430_Identify(CHAR* buffer, LONG count, LONG setId);

</c code>
And here is my C# code:

<C# code>

[DllImport(m_dllName)]
public static extern int MSP430_Identify(
[MarshalAs(UnmanagedType.LPStr)]StringBuilder buffer, int count, int
setId);
Here is the code where I call it:
StringBuilder identityBuffer = new StringBuilder(80, 80);
TIMSP430Driver.MSP430_Identify( identityBuffer, 80, 0);

</C# code>
The StringBuilder approach is the only one I have been able to get any
sort of result from. ost combinations of ref, out, char[] and string
cause exceptions, but the StringBuilder method gives me 3 characters and
they are valid. The catch is, since this isn't a valid string but rather
a byte buffer, there are some 0 character in the string and that is
stopping the StringBuilder from getting the entire results. I need to
tell it to get all bytes, not just until it hit a terminator.

An example of the correct result is:
[0] 85 'U' char
[1] -86 'ª' char
[2] 5 '?' char
[3] 0 char
[4] 77 'M' char
[5] 83 'S' char
[6] 80 'P' char
[7] 52 '4' char
[8] 51 '3' char
[9] 48 '0' char
[10] 70 'F' char
[11] 49 '1' char
[12] 52 '4' char
[13] 55 '7' char
[14] 0 char
... etc
What do I need to do to get the StringBuilder to cooperate with me? Any
suggestions welcome.
Thanks!
Steve

Feb 25 '06 #2

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

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
7
by: JS | last post by:
I have this struct: int main() { struct jb { char actor; struct jb *next; }; struct jb *bond;
4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
1
by: Tajmiester | last post by:
Hi, And thanks for any help. I am having trouble declaring a struct containing strings that can be Serialized using the following functions. It works, but the strings wont store the right number...
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
2
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.