Re: std::string <--> System::String*

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Jochen Kalmbach [MVP] <nospam-Jochen.Kalmbach@xxxxxxxxx> wrote:
Hi Marcus!

Why not use:

struct Conv
{
char *szAnsi;
Conv(System::String* s)
:
szAnsi(static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
{}
~Conv()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};


Then you can simply write:

std::string ss = Conv(s);

Nice, now this eliminates the need for a second wrapper function :)


class MarshalNetToStdString {
public:
MarshalNetToStdString(System::String* s)
: cp(static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
{ }

~MarshalNetToStdString()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(static_cast<System::IntPtr>(cp));
}

operator const char*() const
{
return cp;
}

private:
char* cp;

MarshalNetToStdString(const MarshalNetToStdString&);
MarshalNetToStdString& operator=(const MarshalNetToStdString&);
};


However, thinking about it, I was reminded of the "rule of 3" (in
essence, it says that if you provide any of {destructor, copy
constructor, assignment operator} then you probably need to provide all
three of them). I felt that it wouldn't really make much sense to be
copying these objects around, so I made the copy constructor and
assignment operator private to prevent the compiler from generating
default ones that will only perform shallow copies. The default
constructor does not get auto-generated since I have a user-defined
constructor, so I don't have to worry about that one.


Please see if the following train of thought sounds logical:
I only intend to use this class in the context of

System::String* s = new System::String("...");
std::string ss = MarshalNetToStdString(s);

In other words, create a temporary object (which gets destroyed at the
end of the statement) to do the conversion. In order to avoid the
overhead of creating/destroying an object every time, I thought about
making static member functions to do the conversion, but then I realized
that this would make the memory management much more complex, so I
decided against it. Plus, this class seems pretty lightweight so I
don't think the overhead is too great compared to the safety it
provides.

--
Marcus Kwok
.



Relevant Pages

  • Re: std::string <--> System::String*
    ... char *szAnsi; ... operator LPCSTR() const ... return szAnsi; ...
    (microsoft.public.dotnet.languages.vc)
  • Re: std::string <--> System::String*
    ... char *szAnsi; ... operator LPCSTR() const ... return szAnsi; ...
    (microsoft.public.dotnet.languages.vc)
  • Re: dh, the daemon helper
    ... some type is a special case, while const ordinarily binds to the ... ie char const * is a pointer to a constant ... no point in returning memory to the malloc heap. ... 2004 is the UNIX standard. ...
    (comp.unix.programmer)
  • review of the "container library", part 1/?
    ... A few warnings were issued with gcc 4.4.5 and the options in the Makefile. ... or cast to ... definition of struct tagBinarySearchTreeNode has "char factor;" which is used ... where GetElement returns const void *. ...
    (comp.lang.c)
  • Re: HPGCC: exit() and return
    ... The switch statements ... static char const *string2basen{ ... Do not declare char const * string; as a const value (use just ... pointer since it is 'const', ...
    (comp.sys.hp48)