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 2005

Tip: Looking for answers? Try searching our database.

How do I use String class  in CRT Fuctions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John E Katich - 08 Jun 2005 03:00 GMT
How can I cast a String to a const wchar_t*?

Ths following code genreates a compile error:

error C2664: '_wsplitpath' : cannot convert parameter 1 from 'System::String
__gc *' to 'const wchar_t *'

OpenFileDialog* ofd = new OpenFileDialog();
ofd->Filter = "OW Files|*.db;aepctl.dat;aepord.dat;aep|All Files|*.*";
if(ofd->ShowDialog() == DialogResult::OK)
{
     wchar_t drive[_MAX_DRIVE];
     wchar_t dir[_MAX_DIR];
     wchar_t fname[_MAX_FNAME];
   _wsplitpath( ofd->FileName ,drive,dir,fname,NULL);
}

William DePalo [MVP VC++] - 08 Jun 2005 03:51 GMT
> How can I cast a String to a const wchar_t*?

It's not actually a cast but this will do the job on an instance s of
System::String

#include <vcclr.h>

wchar_t __pin *pStr;

pStr = PtrToStringChars(s);

Regards,
Will
Frisky - 09 Jun 2005 03:59 GMT
You can also use

Char * ppchar = PtrToStringChars( mystring ); // __pin not needed
for( ; ppchar != 0; ++ppchar )
{
  // do stuff with each Char
}

PtrToStringChars returns a System::Char*, which is an interior pointer. As
such, it is subject to garbage collection. You don't have to pin this
pointer unless you're going to pass it to a native function.

Pinning is not needed because ppchar is an interior pointer, and if the
garbage collector moves the string it points to, it will also update ppchar.
Without __pin the code will work and not have the potential performance hit
caused by pinning.

> How can I cast a String to a const wchar_t*?
>
[quoted text clipped - 12 lines]
>    _wsplitpath( ofd->FileName ,drive,dir,fname,NULL);
> }

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.