Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2004

Tip: Looking for answers? Try searching our database.

C++/CLI norms for calling functions that want String ^ when you have native char *

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bern McCarty - 22 Jun 2004 17:51 GMT
In MEC++ I could just call a function that was expecting a String __gc* even
though I had a native char *.  The char * automatically got marshaled to a
String __gc*.

In the May CTP of VS 2005 I just get a compile-time error:  cannot convert
parameter 1 from 'char *' to 'System::String ^'

What would be the normal C++/CLI way to handle this?  And can I do it that
way in the May CTP?

I thought it might be the marshal_as template but I couldn't really find it
or enough information about it.

Bern McCarty
Bentley Systems, Inc.
Steve McLellan - 22 Jun 2004 18:12 GMT
> In MEC++ I could just call a function that was expecting a String __gc* even
> though I had a native char *.  The char * automatically got marshaled to a
[quoted text clipped - 5 lines]
> What would be the normal C++/CLI way to handle this?  And can I do it that
> way in the May CTP?

I use:

System::String * DotNetStringFromSTLString( const std::string &string_ )
{
   return Marshal::PtrToStringAnsi((int*) string_.c_str());
}

which works, but for all I know is leaking like a swimming pool full of
porcupines.
Nishant S - 22 Jun 2004 19:30 GMT
Hi Steve

There is no leak, since PtrToStringAnsi creates a new String object and
copies the source string into it. The managed String object will get GC'd
eventually.

Signature

Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */

> > In MEC++ I could just call a function that was expecting a String __gc*
> even
[quoted text clipped - 16 lines]
> which works, but for all I know is leaking like a swimming pool full of
> porcupines.
Nishant S - 22 Jun 2004 19:27 GMT
Do this :-

ref class R
{
public:
  void Test(String^ s)
  {
  }
};

void _tmain()
{
  R^ r = gcnew R();
  char* s = "hello world";
  r->Test(gcnew String(s));
}

Signature

Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */

> In MEC++ I could just call a function that was expecting a String __gc* even
> though I had a native char *.  The char * automatically got marshaled to a
[quoted text clipped - 11 lines]
> Bern McCarty
> Bentley Systems, Inc.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.