Conversion of String* to wchar_t in VS 2005

G

Guest

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
 
W

William DePalo [MVP VC++]

blair said:
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.

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/TransGuide.asp

Regards,
Will
 
T

Tamas Demjen

blair said:
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

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
 
G

Guest

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:blush:ldSyntax
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
 
T

Tamas Demjen

blair said:
the complier threw a syntax error probably because I have /clr:blush:ldSyntax
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:blush:ldSyntax.

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
 
G

Guest

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


blair said:
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

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
 
N

Norman Diamond

Kapil Khosla said:
--
Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights [...]
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 );
}

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?
 
G

Guest

Norman Diamond said:
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?

const System::Byte* is as __gc* not a normal pointer; so it is updates
during a GC.
 
N

Norman Diamond

Marcus Heege said:
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?
 
N

Norman Diamond

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top