2

I have a struct in C++ which has a char[10] field.

struct Package
{
    char str[10];
};

I convert the struct into char array and send it to and c# application over a TCP socket and there I convert it back to a c# struct.

[StructLayout(LayoutKind.Sequential)]
public struct Package
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
    public string str;
};

The convertion is done properly and I get the message but the problem is when the length of the message is less than the size of the array and I think that it's due to the null terminator in c++ char array.

For instance if the I send "Hello\0" from C++ the char array is something like:

H e l l o \0 \0 \0 \0 \0

And when I get it in c# application it is something like:

H e l l o Ì Ì Ì Ì Ì

And I really don't know what to do with this (personally like to call) junk character 'Ì'.

please help me on this. Any help is appreciated.

Update:

I simply cast the struct to char* and send it over the socket;

Package pkg;
strcpy_s(pkg.str, 'Hello\0');

char* bytes = (char*)&pkg;

send(socket, bytes, sizeof(pkg), NULL);
13
  • 1
    Your array size is 10. Try type 10 characters. You don't have this character 'Ì'.So you can split string. This is one solution. Commented Dec 20, 2014 at 9:03
  • Your solution is good for 10 character but what if I need to have more and more characters. Howerver your answer reminded me of that I can have an end of the string character when I send the char array from c++. Still I'm waiting for more helps. Commented Dec 20, 2014 at 9:06
  • It seems that in c# code you doesn't receive '\0' char. Or is never sent to you in C++, but in c++ the array is set "memsetted" to 0x00 for all chars by default Commented Dec 20, 2014 at 9:08
  • Actually even if I put '\0' at the end of the char array in C++ it never receives in c#. Commented Dec 20, 2014 at 9:09
  • 1
    I thnk we need more code. Paste the way you use to pass from C++ array to C# string Commented Dec 20, 2014 at 9:11

3 Answers 3

4

I don't know any C#, but may be you can solve the problem this way

Package pkg;
strcpy_s(pkg.str, 10, "Hello");

char* bytes = (char*)&pkg;

send(socket, bytes, sizeof(pkg), NULL);

The strcpy_s function takes the size of the dst buffer as an argument to avoid an overflow (see here). I asked in the comments about the way you inovke it, because it doesn't even look like it's valid C++.

The terminating null byte is added to pkg.str automatically and also "Hello" has a terminating null byte after the o character, and you don't have to add it manually.

I assumed that C# knows how to handle the recieved string, so if you send the right string, it should be recived correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

It amazingly worked. I don't know why you received a negative vote. Anyway what is the explanation?
BTW: sizeof(pkg) is perfectly ok as long as pkg.str is an array and not a pointer, if it is a pointer, then it would be sizeof(pkg) + strlen(pkg.str) + 1, the + 1 for the terminating null byte. And you wold have to allocate space for the string using malloc.
2

You are sending sizeof array instead of strlen of str.

Take notes that if you send with strlen you need to add 1 to send the null termination of string

Comments

1

This is what I usually do in my wrappers dll with serial line

        public byte[] RXArray = new byte[length];

        byte[] appo = new byte[MsgLength];
        Array.ConstrainedCopy(RXArray, NcharsToCopy, appo, 0, MsgLength);
        string ConvertedToString = System.Text.Encoding.UTF8.GetString(appo);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.