marshal craft
marshal craft

Reputation: 447

Windows char and Char types and strings with Console::WriteLine()

I am writing a Winsock application and at one point I need to form the tcp message which is an http get request for a webpage.

char clientmessage[] = "GET / HTTP/1.1\r\n\r\n";
Console::WriteLine(clientmessage);

When I try to print out the message useing Console::WriteLine() method it prints True. I've considered it may be an issue with the method and maybe I nee to use puts() as is done in this tutorial. http://www.binarytides.com/winsock-socket-programming-tutorial/

Any help is appreciated.

Upvotes: 1

Views: 340

Answers (1)

Eldar Dordzhiev
Eldar Dordzhiev

Reputation: 5135

It seems like it implicitly converts an unmanaged pointer to System.Boolean. In order to use .NET APIs, you need to marshal the parameters. In this case you need to marshal clientmessage into System::String^. You can do this with the System::Runtime::InteropServices::Marshal::PtrToStringAnsi() method.

char clientmessage[] = "GET / HTTP/1.1\r\n\r\n";
System::String^ str = System::Runtime::InteropServices::Marshal::PtrToStringAnsi(static_cast<System::IntPtr>(clientmessage));
Console::WriteLine(str);

Upvotes: 3

Related Questions