简体   繁体   中英

Convert C# String to MFC CString in C# project?

I am developing a client in C#. The server was developed by other who used C++ MFC, so I can´t change it. The server can only accept string data as CString (UTF8). Note: before I ask this question I have searched and read many threads, eg. thread1 , thread2 , thread3 , etc. But they talk about conversion in C++ project or C++ context (from CString to other or from C++ String to CString, etc.), also they are not in C#.

Please help by giving me a sample code or point me the link. Thank you in advance.

Below is my sample code. It is a C# console application:

class Program
{
    static void Main(string[] args)
    {
        String strHello = "Hello in C# String"; // immutable

        // How to convert ito MFC CString equivalent? CString is mutable.

    }
}

Added information (1):

  1. The thread "Convert String^ in c# to CString in c++" is from C++ point of view (with c++ code) and not from C# pov like mine. I have only access to my client, which is written in C#. I don't have access to server code which is written in C++, so I can't change anything on server code / C++ code.

  2. I send the data through TCP. The connection is established successfully. On server side the receiver (OnReceiveData) uses CString as parameter. Following is my send data code (after using the answer from Agent Ron). Still it doesn't work, means: server still ignores the data.

      tcpClient = new TcpClient(hostname, port); Byte[] data = Encoding.UTF8.GetBytes(message); NetworkStream stream = _client.GetStream(); StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); writer.AutoFlush = false; writer.Write(data.Length); writer.Write(message); writer.Flush(); 

Added information (2):

Finally I can contact the developer of the server. He won't tell me the code of server, but only gives me the code fragment of his C++ client and he said it works with his server.

int XmlClient::SendLine( const CString& strSend )
{
    char* source = CConversion::TcharToChar( strSend ); // UTF8 conversion
    int length = strlen( source );
    char* tmp = new char[length+3];
    memcpy_s( tmp, length+3, source, length );
    tmp[ length ] = '\0';
    strcat_s( tmp, length+3, "\r\n" );
    Send( tmp, length +2 );
    delete[] tmp;
    delete[] source;
    return 0;
}

It is kind of hard to answer without knowing how you are interfacing with the C++ MFC code, but if you just need a UTF-8 byte array you can do this:

byte[] utf8data = Encoding.UTF8.GetBytes(strHello);

Response to Update

I suspect you may want to be using BinaryWriter instead of StreamWriter. You may also need to do endianness conversion on the length integer see this thread , and this blog post for examples).

Finally the server accepts the data sent by my client. Here is the solution: - encode the original string to UTF8 and append it with "\\r\\n". - open network stream. - and simply send it without using any writer.

Nevertheless I thank to you all trying to help me by showing the possible direction to find the solution.

            // Translate the passed message into UTF8 and store it as a Byte array.
            Byte[] utf8source = UTF8Encoding.UTF8.GetBytes(strHello);
            Byte[] suffix = UTF8Encoding.UTF8.GetBytes("\r\n");
            Byte[] utf8Result = new byte[utf8source.Length + suffix.Length];
            Buffer.BlockCopy(utf8source, 0, utf8Result, 0, utf8source.Length);
            Buffer.BlockCopy(suffix, 0, utf8Result, utf8source.Length, suffix.Length);

            // Send the message to the connected TcpServer. 
            NetworkStream stream = _client.GetStream();
            stream.Write(utf8Result, 0, utf8Result.Length);

Case closed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM