473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Conversion of String* to wchar_t in VS 2005

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
Nov 17 '05 #1
9 3566
"blair" <bl***@discussions.microsoft.com> wrote in message
news:67**********************************@microsof t.com...
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/de...TransGuide.asp

Regards,
Will

Nov 17 '05 #2
blair wrote:
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
Nov 17 '05 #3
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

"Tamas Demjen" wrote:
blair wrote:
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

Nov 17 '05 #4
blair wrote:
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
Nov 17 '05 #5

--
Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights
"blair" wrote:
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
Nov 17 '05 #6
"Kapil Khosla [MSFT]" <kk*****@online.microsoft.com> wrote in message
news:18**********************************@microsof t.com...

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

Nov 17 '05 #7
"Norman Diamond" wrote:
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.

Nov 17 '05 #8
"Marcus Heege" <Ma*********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.com...
"Norman Diamond" wrote:
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.


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?

Nov 17 '05 #9
"Norman Diamond" <nd******@community.nospam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Marcus Heege" <Ma*********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.com...
"Norman Diamond" wrote:
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.


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.

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Karthik | last post by:
Hello I am trying to convert the following??? The Code std::string* ChkName;
4
by: djake | last post by:
How can I convert char in wchar_t? And how can I convert wchar_t in char? Thanks to anyone
8
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I...
3
by: Steve Richter | last post by:
here is a warning I am getting in a C++ .NET compile: c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while...
2
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it...
7
by: LuB | last post by:
I have to convert a wide (16bit) char to a standard (8bit) char. Please nevermind that I am using WideCharToMultiByte ... but my question is as follows: Does this approach make any sense? My...
14
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right?...
3
by: =?ISO-8859-1?Q?S=F8ren?= | last post by:
Hi Guys I'm still trying to learn C++, and it´s going im the right direction. The only thing that keeps make me banging my head againts the wall, is when I have a variable of some sort, and need...
2
by: Arcturus | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkh6waMACgkQq5h2IFR18WMFrwCgv/PNAC8FTZCErvc0KHnx0zpC...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.