I'm getting an error where it says I'm trying to use a null pointer. Then when I click ignore another error saying I tried to read or write protected memory. Any idea how to fix?

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {


 using namespace System::Runtime::InteropServices;
using namespace System;
using namespace System::Text;

System::String ^ zs = textBox1->Text;
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(zs);
bob.SendData(str2);

Sleep(2000);

char er[50];
memcpy(er,"-",50);
bob.RecvData(er,50);

string string1(er, 50);

System::String ^swq;
MarshalString(swq, string1);
label2->Text = "Hi";


			 }

Recommended Answers

All 3 Replies

Member Avatar for jencas

Ever checked str2 after the call to Marshal:: StringToHGlobalAnsi(zs)???

More code:

Recive Data:

bool Socket::RecvData( char *buffer, int size )
{
	int i = recv( mySocket, buffer, size, 0 );
	buffer[i] = '\0';
	return true;
}

Marshall Function:

void MarshalString ( String ^ s, string& os ) {
   using namespace Runtime::InteropServices;
   const char* chars = 
	  (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
   os = chars;
   Marshal::FreeHGlobal(IntPtr((void*)chars));
}
Member Avatar for jencas

You still are'nt checking chars for NULL!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.