CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old August 20th, 2002, 10:33 PM
    mce mce is offline
    Member +
     
    Join Date: Jul 2002
    Posts: 718
    mce is an unknown quantity at this point (<10)
    TCHAR [] and LPCTSTR

    could someone help me on the following?

    TCHAR name[50];
    CString str="test";

    name=(LPCTSTR)str;

    The above produce compile error
    error C2440: '=' : cannot convert from 'const unsigned short *' to 'unsigned short []' Conversion loses qualifiers
    I don't want to use LPCTSTR for name bcos i don't want to allocate memory and delete memory ...
    Any good fix for the above?
    Reply With Quote
      #2    
    Old August 20th, 2002, 11:00 PM
    poccil poccil is offline
    Member
     
    Join Date: Aug 2002
    Posts: 467
    poccil is an unknown quantity at this point (<10)
    Declaring a string literal automatically allocates the memory necessary to store. Therefore, you need not create an array of characters tostore the string.

    'LPTSTR' and 'LPCTSTR' are just types for arbitrary pointers. No memory is allocated in their use.

    Rewrite the code like this.

    Code:
    LPTSTR name;
    CString str="test";
    name=(LPTSTR)str;
    I use the LPCTSTR data type only in function prototypes.
    __________________
    Peter O.
    My Pokemon site / Win32 tips / Window Viewer 1.0 alpha
    Reply With Quote
      #3    
    Old August 20th, 2002, 11:51 PM
    mce mce is offline
    Member +
     
    Join Date: Jul 2002
    Posts: 718
    mce is an unknown quantity at this point (<10)
    yes.. i forgot about one thing just now,

    i should be able to do the following
    Code:
    LPTSTR name;
    CString str="test";
    name=(LPTSTR)str;
    but what if i need to do something like this:
    where memberStruct has a name data type char[50].
    something like this:
    MEMBERSTRUCT
    {
    ....
    char name[50];
    .....

    }
    Code:
    GetInfo(LPTSTR name)
    {
    MemberStruct member;
    SomebodyFucntion(&member);
    //now i need to return char name[50] back through name, how??
    
    
    }
    Reply With Quote
      #4    
    Old August 21st, 2002, 12:00 AM
    Yves M's Avatar
    Yves M Yves M is offline
    Moderator
    Power Poster
     
    Join Date: Aug 2002
    Location: Madrid
    Posts: 4,560
    Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)
    Looks like you are compiling this program in Unicode, so your LPSTR will actually be an array of unsigned shorts (2 bytes) while your char name[50] is just an array of single bytes.

    There are two solutions :
    1) Convert the Unicode string (the LPSTR) to ANSI
    2) use an array of unsigned shorts in your struct MEMBERSTRUCT

    I guess what you would like to do is option 2)
    __________________
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.
    Reply With Quote
      #5    
    Old August 21st, 2002, 12:12 AM
    mce mce is offline
    Member +
     
    Join Date: Jul 2002
    Posts: 718
    mce is an unknown quantity at this point (<10)
    I am compiling for unicode, yes..
    yes i would go for option 2, but what if memberStruct is 3rd party struct.(i couldn;t change anything on that)
    Reply With Quote
      #6    
    Old August 21st, 2002, 12:24 AM
    mce mce is offline
    Member +
     
    Join Date: Jul 2002
    Posts: 718
    mce is an unknown quantity at this point (<10)
    I just want to be clearer as to what i wanted... in GetInfo(LPCTSTR name) fucntion.

    Inside this fucntion, on one hand, i have to use CString to facilitate some string manipulation, like CString::Find, CString::ReverseFind etc.. and return the result back through name easily like this:

    name=(LPCTSTR)FoundResult;

    On the other hand, i need to call a third party fucntion inside GetInfo, which return a char[50] type to me through MemberStruct as described above.
    so i need to return this char[50] result back through LPCTSTR name as well... if you can suggest me using types other that LPTSTR as parameter in GetInfo, then it is fine, just that it must be unicode!
    Reply With Quote
      #7    
    Old September 19th, 2002, 03:48 PM
    Sam Hobbs Sam Hobbs is offline
    Elite Member
    Power Poster
     
    Join Date: May 1999
    Location: Southern California
    Posts: 12,261
    Sam Hobbs has a spectacular aura about (125+)Sam Hobbs has a spectacular aura about (125+)
    Quote:
    Originally posted by poccil
    Rewrite the code like this.

    Code:
    LPTSTR name;
    CString str="test";
    name=(LPTSTR)str;
    That is likely to cause problems if "name" is expected to be 50 characters. Doing it that way, "name" will have a pointer to the string in the CString and that string is likely to be less than 50 characters. Also, that pointer will become invalid if the CString reallocates it's buffer. A correct way (not necessarily the only correct way) to rewrite the code I think would be:
    Code:
    TCHAR name[50];
    CString str="test";
    wcscpy(name, str);
    __________________
    "Signature":
    My web site is Simple Samples.
    Reply With Quote
      #8    
    Old September 19th, 2002, 04:02 PM
    Sam Hobbs Sam Hobbs is offline
    Elite Member
    Power Poster
     
    Join Date: May 1999
    Location: Southern California
    Posts: 12,261
    Sam Hobbs has a spectacular aura about (125+)Sam Hobbs has a spectacular aura about (125+)
    So to answer mce's question, I think the following general idea will work:
    Code:
    void GetInfo(LPTSTR name) {
    CString str=name;
    str=T("Info");    // Replace with your string manipulation and such
    wcscpy(name, str);
    }
    This code assumes that name is 50 characters. However the Unicode part is confusing; I am not sure where it is that you need to convert to/from Unicode. If you still need help then you can ask about how to convert to/from Unicode.
    __________________
    "Signature":
    My web site is Simple Samples.
    Reply With Quote
      #9    
    Old September 19th, 2002, 04:28 PM
    Yves M's Avatar
    Yves M Yves M is offline
    Moderator
    Power Poster
     
    Join Date: Aug 2002
    Location: Madrid
    Posts: 4,560
    Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)
    To convert an ANSI string to Unicode, use
    Code:
    BSTR unicodestr;
    unicodestr = SysAllocStringLen(NULL, lstrlenA(ansistr) * 2);
    MultiByteToWideChar(CODE_PAGE, 0, ansistr, -1, unicodestr, lstrlenA(ansistr));
    To convert Unicode to ANSI, use
    Code:
    char *szString;
    
    szString = (char *) malloc(SysStringLen(unicodestr) + 1);
    WideCharToMultiByte(CODE_PAGE, 0, unicodestr, -1, ansistr, SysStringLen(unicodestr), NULL, NULL);
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 08:39 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009