473,320 Members | 1,829 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,320 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 2736
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...

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.