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++ / February 2007

Tip: Looking for answers? Try searching our database.

Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
renbin - 26 Feb 2007 08:52 GMT
Now I have a problem. I have some code like this:

void SetFieldName(LPCTSTR lpszNewValue)
{
   char szFieldName[11];
   strcpy(szFieldName,lpszNewValue);
}

When I debug it,the System tells me
"Error 1 error C2664: 'strcpy' : cannot convert parameter 2 from 'LPCTSTR'
to 'const char *' e:\MapControlTest\Map\MapTableDesc.cpp line 74"

Please tell me where my code wrong,thank you.
Bruno van Dooren [MVP VC++] - 26 Feb 2007 09:21 GMT
> void SetFieldName(LPCTSTR lpszNewValue)
> {
[quoted text clipped - 5 lines]
> "Error 1 error C2664: 'strcpy' : cannot convert parameter 2 from 'LPCTSTR'
> to 'const char *' e:\MapControlTest\Map\MapTableDesc.cpp line 74"

strcpy takes char arrays as parameters, your lpszNewValue is probably a
unicode string.
if you want to use TSTR, you should use
_tcscpy

to insure that you use the correct function.

additionally:
-Your code is dangerous because you don't check that szFieldName is large
enough to hold lpszNewValue.
-drop the lpsz prefixes for variable names. These days there is absolutely
no reason anymore to use it. The compiler will tell you if you mix different
pointer types.
Signature


Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

David Lowndes - 26 Feb 2007 09:29 GMT
>When I debug it,the System tells me
>"Error 1 error C2664: 'strcpy' : cannot convert parameter 2 from 'LPCTSTR'
>to 'const char *' e:\MapControlTest\Map\MapTableDesc.cpp line 74"

You must be compiling a Unicode build, so your code should presumably
be:

void SetFieldName(LPCTSTR lpszNewValue)
{
   TCHAR szFieldName[11];
   _tcscpy(szFieldName,lpszNewValue);
}

... unless you specifically need szFieldName as a MBCS - in which case
you somewhere need to convert from Unicode to MBCS - have a look at
the T2A macro documentation.

Dave
renbin - 27 Feb 2007 00:44 GMT
Hello Dave

I'm sorry that I don't want to change szFieldName type form char array to
TCHAR array.Do you have another method ?

I only want to copy lpszNewValue to char array,thank you!
David Lowndes - 27 Feb 2007 08:01 GMT
>I'm sorry that I don't want to change szFieldName type form char array to
>TCHAR array.Do you have another method ?
>
>I only want to copy lpszNewValue to char array,thank you!

Is lpszNewValue actually a pointer to a Unicode string, or is it an
incorrectly defined MBCS string?

If it's the former, do as I mentioned originally:

"... unless you specifically need szFieldName as a MBCS - in which
case you somewhere need to convert from Unicode to MBCS - have a look
at the T2A macro documentation.
"

If it's the latter - define it correctly as LPCSTR.

Dave

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.