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

passing string without '\0' termination

GB
Hi,

I have a structure which I want to pass as a pointer to a C dll. The
problem is that C# adds '\0' character at the end of each string
(which is contained in the struct).

I understand that C# does it so that no null terminated strings are
passed but I want to pass the strings without the padding.

Example,

[StructLayout(LayoutKind.Sequential)]
public struct Name
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= 6)]
public string LastName
};

Name nm = new Name();
nm.Lastname = "GEORGE";

Cfunction(ref nm); //here at the 'C' end the value of LastName becomes
// LastName[0] = 'G'
// LastName[1] = 'E'
// LastName[2] = 'O'
// LastName[3] = 'R'
// LastName[4] = 'G'
// LastName[5] = 0
Instead of '0' I want 'E' (basically no '\0' termination)

Is there any other way except increasing the size of LastName(which
would increase the size of struct)?

Thanks a lot,
Gaurav
**************************
Nov 16 '05 #1
2 2741
Hi,
inline

"GB" <ga************@yahoo.co.uk> wrote in message
news:d5**************************@posting.google.c om...
Hi,

I have a structure which I want to pass as a pointer to a C dll. The
problem is that C# adds '\0' character at the end of each string
(which is contained in the struct).

I understand that C# does it so that no null terminated strings are
passed but I want to pass the strings without the padding.

Example,

[StructLayout(LayoutKind.Sequential)]
public struct Name
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= 6)]
public string LastName
};
One way is to declare LastName as a character array:
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
public char[] LastName;

Name nm = new Name();
nm.Lastname = "GEORGE";
would become:
nm.Lastname = "GEORGE".ToCharArray(0,6);

hth,
greetings


Cfunction(ref nm); //here at the 'C' end the value of LastName becomes
// LastName[0] = 'G'
// LastName[1] = 'E'
// LastName[2] = 'O'
// LastName[3] = 'R'
// LastName[4] = 'G'
// LastName[5] = 0
Instead of '0' I want 'E' (basically no '\0' termination)

Is there any other way except increasing the size of LastName(which
would increase the size of struct)?

Thanks a lot,
Gaurav
**************************

Nov 16 '05 #2
Hi,
inline

"GB" <ga************@yahoo.co.uk> wrote in message
news:d5**************************@posting.google.c om...
Hi,

I have a structure which I want to pass as a pointer to a C dll. The
problem is that C# adds '\0' character at the end of each string
(which is contained in the struct).

I understand that C# does it so that no null terminated strings are
passed but I want to pass the strings without the padding.

Example,

[StructLayout(LayoutKind.Sequential)]
public struct Name
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= 6)]
public string LastName
};
One way is to declare LastName as a character array:
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
public char[] LastName;

Name nm = new Name();
nm.Lastname = "GEORGE";
would become:
nm.Lastname = "GEORGE".ToCharArray(0,6);

hth,
greetings


Cfunction(ref nm); //here at the 'C' end the value of LastName becomes
// LastName[0] = 'G'
// LastName[1] = 'E'
// LastName[2] = 'O'
// LastName[3] = 'R'
// LastName[4] = 'G'
// LastName[5] = 0
Instead of '0' I want 'E' (basically no '\0' termination)

Is there any other way except increasing the size of LastName(which
would increase the size of struct)?

Thanks a lot,
Gaurav
**************************

Nov 16 '05 #3

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
17
by: subnet | last post by:
I'm not a C expert, but I've seen this topic discussed in the newsgroup several times. What's the deal with writing such programs? Why are they considered so interesting? Thanks
26
by: junky_fellow | last post by:
Consider the following piece of code: char *str = "Hello"; if (str = "Hello") printf("\nstring matches\n"); str is pointer to char and "Hello" is a string literal whose type is "array of...
1
by: GB | last post by:
Hi, I have a structure which I want to pass as a pointer to a C dll. The problem is that C# adds '\0' character at the end of each string (which is contained in the struct). I understand that...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
13
by: Logan Lee | last post by:
Hi. I've written a small program to learn to write in C. But unfortunately the output is all jumbled up and not nice. /* read_file.c The whole point of this code is to read the entire content...
2
by: Shashank | last post by:
Hi, I am passing a tcpdump filter to a function which compiles the filter using pcap_compile and then sets it. Here is the filter, ip>=0x0000 and ip <=0xd9295a3 and ip >=0x0000 and ip...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
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: 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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.