Convert Char To LPCTSTR

This is a discussion on Convert Char To LPCTSTR within the C Programming forums, part of the General Programming Boards category; Hi all, Ive coded quite a complicated C programme and stuck on a really simple problem which i cant seem ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    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.

    Code:
    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:

    Code:
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    char makeAshortString[2];
    makeAshortString[0] = ch;
    makeAshortString[1] = '\0';
    SetDlgItemText(hwnd, IDC_S, makeAshortString );

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.

    Code:
    SetDlgItemText(hwnd, IDC_S,_TEXT("a"));
    
    or
    char a[] = "a";
    SetDlgItemText(hwnd, IDC_S,a);
    
    or
    wchar_t a[] = L"a";
    SetDlgItemText(hwnd, IDC_S,a);
    
    or
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21