CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    Converting char to LPCWSTR

    I recently switched from Dev-C++ to VS 2005 but when i try compiling this program:
    Code:
    #include "stdafx.h"
    #include <windows.h>
    
    int main()
    {
         MessageBox(NULL,"Text.","Caption.",MB_OK);
         return 0;
    }
    it says that it can't convert char[4] to LPCWSTR.
    This compiled perfectly fine in Dev-C++. Is there any setting that i need to change to get it to work?

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Converting char to LPCWSTR

    This -
    Quote Originally Posted by Bliutte
    This compiled perfectly fine in Dev-C++.
    ...even if it compiled fine is not correct.

    Your parameters are actually wrong.
    MessageBox API accepts a TCHAR string.

    The correct way to call it is -
    Code:
    	 MessageBox(NULL,_T ("Text."),_T("Caption."),MB_OK);
    You faced the compile error on MSVC because _UNICODE was defined, and not on Dev C++.

  3. #3
    Join Date
    Dec 2005
    Posts
    4

    Re: Converting char to LPCWSTR

    Ok, thanks for the quick reply!

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Converting char to LPCWSTR

    You are welcome...

  5. #5
    Join Date
    Dec 2005
    Posts
    4

    Re: Converting char to LPCWSTR

    Does this work with strings? I tried this:
    Code:
    char text = (char)"Text.";
    MessageBox(NULL,_T (text), _T ("Caption."),MB_OK);
    That gave this error:
    Code:
    error C2065: 'Ltext' : undeclared identifier
    How do i get it to work with strings?

    And it also said:
    Code:
    error C2440: 'initializing' : cannot convert from 'const char [6]' to 'char'
    before i put (char) before "Text.". Is there any way to make VC convert it automatically?

  6. #6
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Converting char to LPCWSTR

    Quote Originally Posted by Bliutte
    Does this work with strings? I tried this:
    Code:
    char text = (char)"Text.";
    I hope you realize that the variable called text is not a string - it is a character.

    The correct code would be -
    Code:
    const char* text = "Text.";
    And...
    Quote Originally Posted by Bliutte
    it also said:
    Code:
    error C2440: 'initializing' : cannot convert from 'const char [6]' to 'char'
    ...Look, it actually told you just what I did.

    It said that you cannot assign a string (i.e. a character array) to a single character.

    Also, like I said before, Win APIs like MessageBox typically accept TCHAR strings... So, your text variable should correct be -
    Code:
    const TCHAR* text = _T ("Text.");
    And the call to MessageBox would be -
    Code:
    MessageBox(NULL, text, _T ("Caption."),MB_OK);

  7. #7
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Converting char to LPCWSTR

    Quote Originally Posted by Bliutte
    How do i get it to work with strings?
    You can always use C++ std::string or std::wstring class by including <string>

    If your MSVC project is one with MFC support, you can also use CString (wraps TCHAR string).

  8. #8
    Join Date
    Dec 2005
    Posts
    4

    Re: Converting char to LPCWSTR

    Oh, sorry about that stupid post. I guess i am too tired right now.
    Thanks for the reply though.

  9. #9
    Join Date
    Feb 2006
    Posts
    1

    Question Re: Converting char to LPCWSTR

    I had a similar problem. I made the following chnages

    MessageBox(NULL, _T(errorMsg), _T("socketIndication"), MB_OK);

    Now it says: _T identifier not found.
    Can you please help me here.

    Thanks,
    deve06

  10. #10
    Join Date
    May 2013
    Posts
    2

    Re: Converting char to LPCWSTR

    Quote Originally Posted by deve06 View Post
    I had a similar problem. I made the following chnages

    MessageBox(NULL, _T(errorMsg), _T("socketIndication"), MB_OK);

    Now it says: _T identifier not found.
    Can you please help me here.

    Thanks,
    deve06
    you need to include tchar.h
    ----
    also if you dont want to include tchar or use that you do this .

    LPCWSTR Hello = L"salam";
    MessageBox(NULL, L"Text.", Hello, MB_OK);

    _T() is a funtion of Tchar that do the magic , but you need to include tchar.h , you can use L"somestring" . done.

  11. #11
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Converting char to LPCWSTR

    Quote Originally Posted by Punsher2011 View Post
    [...]
    _T() is a funtion of Tchar that do the magic [...]
    _T() is not a function but a macro.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  12. #12
    Join Date
    May 2013
    Posts
    2

    Re: Converting char to LPCWSTR

    Quote Originally Posted by ovidiucucu View Post
    _T() is not a function but a macro.
    sorry , my mistake !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured