Converting Managed String to char Array


New to the asking online for help so be gentle.

I am required to make a simple chat program using a windows form application. I have both the Client and Server forms made and I can connect them no problem. The problem I am having is geting my textbox->Text to be passed through a message.

the send() functions second parameter is a const char* buffer. I can't get my Textbox->Text to fit in that parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
String^ myString = Input->Text;
char message = myString->ToCharArray();

		
if ( ( iResult = send( s, message , 5, 0)) == SOCKET_ERROR)
{
    Output->Text = "send failed with error " + WSAGetLastError();
    closesocket( s );
    WSACleanup();
    return;
}
    Output->Text = "We successfully sent %d byte(s) " + iResult;
    Output->Text += Input->Text;



I have tried multiple ways of converting the Textbox->Text but just can't figure it out. Any help would be great thanks.
System::IntPtr ptr = System::Runtime::InteropServices::Marshal:: StringToHGlobalAnsi (myString);

char* str = static_cast<char*>(ptr.ToPointer());

if you use StringToHGlobalAnsi don't forget to free the memory.
I have been using CString as it has a built-in conversions, not sure if that will work in your case

Topic archived. No new replies allowed.