Visual C++ CLI need to display the String in an Object

Hi all,

Have coded in a few differant languages but still have heaps to learn, however I have recently been delving into Winsock programming and I want to do a simple thing, that is display an objects contents that has been passed from one server form to a client form

On one form I did "Winsock1->SendData("Hello") " and on the next I did in a data arrival event

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private: System::Void Winsock1_DataArrival(System::Object^  sender, AxMSWinsockLib::DMSWinsockControlEvents_DataArrivalEvent^  e) {
			 //Object ^myObjArray;
			 Object ^t;
			 Winsock1->GetData(t);
			// o = (String^)t;
			 //textBox1->Text = "test";
			// array ^D1 = static_cast<array^>(t);
			 
	 
//String ^ar =<String^> t;
//array<Object^>^myArr = t;
			label3->Text = "t is:"+t->ToString();  
			  
		 }
};
}


I have used the debugger and inside Object ^t the data is there however I just cant get a textbox to display the data, sure I can get it to say data arrived but not show3 the string in the object variable, well actually debug shows the object contents as a System::array with all data there.

I come from a background of standard C and the more I devlved into why I was having problems the more I ended up in this quagmire of complexity pertaining to new style casting and system objects vs standard objects as well as managed vs native code, could someone aid me to do this simple thing? I realise I should probably have some kind of incomming buffer but I am planning to just see this working an am debating whether I should look into other ways of handling sockets (Very new to socket programming)

I even did what I thought was casting Object ^t to a String ^ but when I do this, even though it builds and runs ok , well during debug the newly created String ^ shows the text but the form label1->Text = "value is"+t doesnt even run in this one instance? No error, but also no label text adjustment to what I want to print, just a blank label

This has been driving me absolutely bonkers lol any help you guys can provide is appreciated, I have been checking the msdn for about 1 week solid and there is no code piece that works with exactly what I am needing to do

Im quite surprised that printing the contents of an object to a label in net is so tough

Completely stumped!
I got the message displayed but with there being so little C++ code on the net which is aimed at begginer level for what I am trying to achieve in this specific enviroment well the solution is remedial at best and It merely gets the result but in a rather silly way .

Essentially Object ^t to my understanding encapsulates a system array , if I toString the array it says System Byte therefore I used a function of the web which creates a memmory stream and then converts the said system Object ^t into a byte Array , Next I was yielding ascii numbers when I was printing the byte array and also the first 20 odd array elements were garbage, at least in the human readable sense so I used the following code and while it works for any message sent across I would need to do a debug to see where in the array the ascii values started and ended and then hard code the values in. (Not hopeful of a reply for this as documentation is sparse for how to do this simple thing, but still maybe someone can improve drastically on this below code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
System::IO::MemoryStream ^_MemoryStream = gcnew System::IO::MemoryStream();
//Convert::ToChar(t->ToString()->ToCharArray());
//String ^givemeadambreak = BitConverter::ToString(myArraycr,0,10);

array<System::Byte> ^_ByteArray = ObjectToByteArray(t); 

__wchar_t* arr = new __wchar_t[_ByteArray->Length];
for(int i=0; i < _ByteArray->Length; i++)
{
arr[i] = _ByteArray[i]; 
}


textBox3->Text =""+ arr[27]; //i
textBox4->Text =""+ arr[28]; //g
textBox5->Text =""+arr[29];//n
textBox6->Text =""+ arr[30];//i
textBox7->Text =""+ arr[31];//t
textBox8->Text =""+ arr[32];//i
textBox9->Text =""+ arr[33];//o
textBox10->Text =""+arr[34]; //n

		 }






 array<System::Byte> ^ObjectToByteArray(System::Object ^_Object) 
 {  

 try 

{  
        // create new memory stream . 

        System::IO::MemoryStream ^_MemoryStream = gcnew System::IO::MemoryStream(); 
         // create new BinaryFormatter . 

        System::Runtime::Serialization::Formatters::Binary::BinaryFormatter ^_BinaryFormatter = gcnew System::Runtime::Serialization::Formatters::Binary::BinaryFormatter(); 

         // Serializes an object, or graph of connected objects, to the given stream. . 
         _BinaryFormatter->Serialize(_MemoryStream, _Object); 
        // convert stream to byte array and return . 

        return _MemoryStream->ToArray(); 

    }  

    catch (Exception ^_Exception)  
    { 

       // Error . 

       Console::WriteLine("Exception caught in process: {0}", _Exception->ToString());  

   } 

    // Error occured, return null . 
   return nullptr; 

} 
Topic archived. No new replies allowed.