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.

Conversion of String* to wchar_t in VS 2005

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
blair - 20 May 2005 21:18 GMT
Hi I am using VS 2005 to upgrade a demo project and I came across this error
in a bunch of code which compiles fine on VC++ 2003.

<code>
wchar_t __pin* pVal = PtrToStringChars( val );
</code>

<error>
Error    1    error C2440: 'initializing' : cannot convert from '__const_Char_ptr'
to 'wchar_t __pin *'   
</error>

Can anyone give me a hint on how to fix this. Also vcclr.h is included.

Thanks,

blair
William DePalo [MVP VC++] - 20 May 2005 21:38 GMT
> Hi I am using VS 2005 to upgrade a demo project and I came across this
> error
[quoted text clipped - 11 lines]
>
> Can anyone give me a hint on how to fix this. Also vcclr.h is included.

I _think_ that the new syntax requires that the target be const

   const_cast< interior_ptr<Char> >(  PtrToStringChars(val) );

I should tell you, though, that I haven't done any interop with VS2005 and
haven't compiled this.

A good introduction to the differences with the "old" MC++ syntax and the
"new" C++/CLI syntax is here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/Tra
nsGuide.asp


Regards,
Will
Tamas Demjen - 20 May 2005 22:31 GMT
> Hi I am using VS 2005 to upgrade a demo project and I came across this error
> in a bunch of code which compiles fine on VC++ 2003.
[quoted text clipped - 13 lines]
>
> blair

The C++/CLI syntax for pinning is
cli::pin_ptr<wchar_t> pVal(PtrToStringChars(val));

I haven't tried this example, though.

Or are you using the old MC++ syntax? I've never used that with VC++ 2005.

Tom
blair - 20 May 2005 23:02 GMT
I am trying to compile some code written in the old syntax so that I can
debug some unique errors only seen when running under .NET 2.0 (what my
actual program uses). I have the source and a Release dll only :)

Also when I tried this code:
const_cast< interior_ptr<Char> >(  PtrToStringChars(val) );
the complier threw a syntax error probably because I have /clr:oldSyntax
enabled. I really do not want to rewrite the whole code base into the new
syntax; but if I have to Oh well.

Thanks for the help,
blair

> > Hi I am using VS 2005 to upgrade a demo project and I came across this error
> > in a bunch of code which compiles fine on VC++ 2003.
[quoted text clipped - 22 lines]
>
> Tom
Tamas Demjen - 21 May 2005 01:39 GMT
> the complier threw a syntax error probably because I have /clr:oldSyntax
> enabled. I really do not want to rewrite the whole code base into the new
> syntax; but if I have to Oh well.

I didn't say you had to. I just didn't realize you were using
/clr:oldSyntax.

PtrToStringChars is defined in vcclr.h. The function seems to return a
const pointer, so you're supposed to assign it to a const __pin pointer:

const wchar_t __pin* const_pVal = PtrToStringChars( val );

This could be the problem. Then you should be able to use const_cast to
cast away the const-ness, if you understand its dangers:

wchar_t* pVal = cost_cast<wchar_t*>(const_pVal);

I haven't tried this, and my old MC++ syntax knowledge is limited, but
you should be looking into this direction. Let us know how it works out.

Tom
Kapil Khosla [MSFT] - 23 May 2005 20:21 GMT
Signature

Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights

> Hi I am using VS 2005 to upgrade a demo project and I came across this error
> in a bunch of code which compiles fine on VC++ 2003.
[quoted text clipped - 13 lines]
>
> blair

I tried  the following code with oldSyntax on Beta2 compiler and it compiled
fine.

#include <vcclr.h>

int main()
{
    String *val;
    const wchar_t __pin* pVal = PtrToStringChars( val );
}

Does this help,
Kapil
Norman Diamond - 25 May 2005 06:14 GMT
[...]
> I tried  the following code with oldSyntax on Beta2 compiler and it
> compiled fine.
[quoted text clipped - 4 lines]
> const wchar_t __pin* pVal = PtrToStringChars( val );
> }

Now my question is WHY does it work.
Header file <vcclr.h> contains the following line:
   const System::Byte *bp = reinterpret_cast<const System::Byte *>(s);
Notice that bp is not pinned.  The definition occurs in inline code.  Is
there any guarantee that this is always going to be managed code?  Is there
any guarantee that the garbage collector won't move the Chars (bytes) of the
string before the final value of bp gets copied back to the caller's pinned
pointer?
Marcus Heege - 02 Jun 2005 21:16 GMT
> Now my question is WHY does it work.
> Header file <vcclr.h> contains the following line:
[quoted text clipped - 4 lines]
> string before the final value of bp gets copied back to the caller's pinned
> pointer?

const System::Byte* is as __gc* not a normal pointer; so it is updates
during a GC.
Norman Diamond - 03 Jun 2005 02:04 GMT
>> Now my question is WHY does it work.
>> Header file <vcclr.h> contains the following line:
[quoted text clipped - 7 lines]
> const System::Byte* is as __gc* not a normal pointer; so it is updates
> during a GC.

That's exactly what I thought.  It looks like you agree.  PtrToStringChars()
sometimes returns a pointer that becomes invalid before the caller gets a
chance to pin it.  So how could PtrToStringChars() ever be reliable?
Norman Diamond - 03 Jun 2005 02:23 GMT
>>> Now my question is WHY does it work.
>>> Header file <vcclr.h> contains the following line:
[quoted text clipped - 7 lines]
>> const System::Byte* is as __gc* not a normal pointer; so it is updates
>> during a GC.

I messed up pretty badly in writing the following:

> That's exactly what I thought.  It looks like you agree.
> PtrToStringChars()
> sometimes returns a pointer that becomes invalid before the caller gets a
> chance to pin it.  So how could PtrToStringChars() ever be reliable?

Perhaps I need to review what happens with interior non-pinned pointers and
stuff like that, but it still makes me nervous.  The code does computations
on bp, while the target data might get moved in the middle of the
computations, and the target data might get moved before the computed result
gets copied into the caller's pinning pointer.

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.