Top Level (^) objects

Hi,
can someone give a brief explanation of so called top level objects (with ^ near type) that appears in windows form applications? I'd appreciate that.
Also, how do I convert String^ to *char? Typecast don't work, nor the c_str(), or i'm using them wrong.
Windows objects are accessed by handles, which is, in Win32, a better choice than a pointer.

Something like String^ s; declares a handle to a 'String' object.

The problems with the MS approach are the same as the advantages: the code is not portable, but it capitalizes on the Windows architecture.

The String class is an older MS C++ creation than the std::string class. To convert it to a char*, you need to use one of the supplied methods.
http://support.microsoft.com/kb/311259

The simplest method might just be to use the CString class, but the StringToHGlobalAnsi() method is also useful.

Good luck!
thank you, Duoas.
closed account (z05DSL3A)
String^ and form applications = C++/CLI (.net)

Off the top of my head I can't think of a way of getting a char* to a system::string...

What are you trying to achieve?

Edit:
Mixed mode C++ and C++/CLI is a royal pain in the butt.
Last edited on
I am trying to send data via socket from the input text field.
Last edited on
closed account (z05DSL3A)
I have done a little research and getting a char * to a system::string would go somthing like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//compiler option: cl /clr 

#include <atlstr.h>
#include <stdio.h>
#using <mscorlib.dll>
#include <msclr/marshal.h>

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace msclr::interop;

int _tmain(void)
{
    System::String ^ str = "Hello world\n";

    marshal_context ^ context = gcnew marshal_context();
    const char* str4 = context->marshal_as<const char*>(str);
    puts(str4);
    delete context;

    return 0;
}

NB: Untested code and assuming Visual C++ 2008
The marshal context is a newer way of doing it... as was noted in the link I posted...
If you are working with anything older, you might want to try one of the two methods I suggested.
closed account (z05DSL3A)
Duoas,

Sorry, I was put off by the "The String class is an older MS C++ creation than the std::string class" and did not think you were talking about system::string from .net and so did not look at your link.
Last edited on
Hey, don't be sorry. Sometimes I word things thoughtlessly.

The system::string class is really just a .NET version of String, IIUC.
Ok, this is how it is - in my client app i have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public: void Issiusti(const char *msg) {
		if (send(hSocket, (char*)&msg, sizeof(msg)-1, 0)==SOCKET_ERROR) {
			Isvesti(" issiusti nepavyko.\n");
		}
	}
...
private: System::Void siustibtn_Click(System::Object^  sender, System::EventArgs^  e) {
				 System::String ^ str = this->ivedimas->Text;
				 marshal_context ^ context = gcnew marshal_context();
				 const char* str4 = context->marshal_as<const char*>(str);
				 puts(str4);
				 delete context;
				 Issiusti(str4);
				 this->ivedimas->Text = "";
			 }
	};


But in the server output i only get unrecognizable characters, the same as i would not do any conversation but pass String^ type data to Issiusti() and then just typecast inside the send(). Why isn't this conversion working? I also tried StringToHGlobalAnsi() as Duoas suggested but the result was the same.
Topic archived. No new replies allowed.