Thread: Convert Char To LPCTSTR

  1. #1
    Registered User
    Join Date
    Oct 2004

    Convert Char To LPCTSTR

    Hi all,

    Ive coded quite a complicated C programme and stuck on a really simple problem which i cant seem to solve.

    char ch = 'a';
    SetDlgItemText(hwnd, IDC_S, ch);
    That is what im trying to do but with no luck as SetDlgItem requires a LPCTSTR as its third argument and not a char. Any ideas how i can get this working?

    Note: Im not looking for the following solution:

    SetDlgItemText(hwnd, IDC_S, "a");
    As i am required to do it by converting a char to a LPCTSTR

    Thanks for any help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    char makeAshortString[2];
    makeAshortString[0] = ch;
    makeAshortString[1] = '\0';
    SetDlgItemText(hwnd, IDC_S, makeAshortString );

  3. #3
    Registered User
    Join Date
    Aug 2005
    LPCTSTR can be either char* or wchar_t*, depending on whether the program is compiled for UNICODE or not. You cannot pass a single char as the third argument. There are several solutions, depending on UNICODE settings -- the first and last solutions below will compile with or without UNICODE.

    SetDlgItemText(hwnd, IDC_S,_TEXT("a"));
    char a[] = "a";
    SetDlgItemText(hwnd, IDC_S,a);
    wchar_t a[] = L"a";
    SetDlgItemText(hwnd, IDC_S,a);
    TCHAR a[] = _TEXT("a");
    SetDlgItemText(hwnd, IDC_S,a);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM