473,406 Members | 2,707 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.

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 2742
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
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,...
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.