none
Convert from char to TCHAR

    Question

  • I cannot for the life of me find a function to convert from char to TCHAR or w_char. Can someone point me in the right direction?
    Lamblion
    Friday, July 24, 2009 7:58 PM

Answers

  • PC just means pointer to const. You can typecast that easily once your string has been converted using MultiByteToWideChar(). I'll see if I can find a .NET equivalent that's easier to use, that function is unnecessarily intense.

    • Marked as answer by Lamblion Friday, July 24, 2009 9:22 PM
    Friday, July 24, 2009 8:20 PM
  • Also, I'm trying to really convert from char to PCWSTR for SHParseDisplayName().
    Lamblion

    const wchar_t* target = CA2W ("An example");
    • Marked as answer by Lamblion Friday, July 24, 2009 9:22 PM
    Friday, July 24, 2009 8:29 PM

All replies

  • char b = 'b'; 
    
     wchar_t wb = (wchar_t)b;
    
    

     

     

     

    Friday, July 24, 2009 8:07 PM
  • Well, I should have been more exact. I want to convert a string, not a single character.
    Lamblion
    Friday, July 24, 2009 8:12 PM
  • Try MultiByteToWideChar()

    Friday, July 24, 2009 8:15 PM
  • Also, I'm trying to really convert from char to PCWSTR for SHParseDisplayName().
    Lamblion
    Friday, July 24, 2009 8:16 PM
  • PC just means pointer to const. You can typecast that easily once your string has been converted using MultiByteToWideChar(). I'll see if I can find a .NET equivalent that's easier to use, that function is unnecessarily intense.

    • Marked as answer by Lamblion Friday, July 24, 2009 9:22 PM
    Friday, July 24, 2009 8:20 PM
  • MultiByteToWideChar() would be fine if I could get it to work...

    char MYSTRING[MAX_PATH];
    WCHAR szDir[MAX_PATH];

    strcpy(MYSTRING, "This is my string");

    MultiByteToWide"Char(CP_ACP, MB_PRECOMPOSED, MYSTRING, -1, szDir, MAX_PATH);

    doesn't convert.
    Lamblion
    Friday, July 24, 2009 8:28 PM
  • Also, I'm trying to really convert from char to PCWSTR for SHParseDisplayName().
    Lamblion

    const wchar_t* target = CA2W ("An example");
    • Marked as answer by Lamblion Friday, July 24, 2009 9:22 PM
    Friday, July 24, 2009 8:29 PM
  • char MYDIR[MAX_PATH];

    MYDIR retrieved from an INI file, then...

    const wchar_t* szDir=MYDIR gives the following error...

    cannot convert from 'char [260]' to 'const wchar_t *' and when I typecast it...


    const wchar_t* szDir=(wchar_t*)MYDIR, the SHParseDisplayName() doesn't translate it.

    Lamblion
    Friday, July 24, 2009 8:33 PM
  • char MYDIR[MAX_PATH];

    MYDIR retrieved from an INI file, then...

    const wchar_t* szDir=MYDIR gives the following error...

    cannot convert from 'char [260]' to 'const wchar_t *' and when I typecast it...


    const wchar_t* szDir=(wchar_t*)MYDIR, the SHParseDisplayName() doesn't translate it.

    Lamblion

    This kind of coding is "wishful thinking". Do you think applying a cast is going to magically change ASCII strings to UNICODE strings?

    Read my post above.

    Friday, July 24, 2009 8:39 PM
  • I don't understand. You mean I have to take an already existing string and type it out in quotes? The idea is to take a string I got from an INI file and make it work in  SHParseDisplayName().

    Which I finally got to work in MultiByteToChar(), but I'd still like to understand your example.
    Lamblion
    Friday, July 24, 2009 8:41 PM
  • I'm looking at the code you posted:

    char MYDIR[MAX_PATH];
    const wchar_t* szDir=(wchar_t*)MYDIR;

    You seem to be under the impression that applying the (wchar_t*) cast performs some magic on the contents of MYDIR, translating the ANSI characters to UNICODE. If so, you'd better go back to your C++ reference book and study up on what casts are, and how they are used. It is critically important if you intend to advertise yourself as a C++ programmer.

    I thought given my coding example:

    const wchar_t* target = CA2W ("An example");

    that you might have the inclination to try:

    const wchar_t* szDir = CA2W(MYDIR);

    and see if that worked.
    Friday, July 24, 2009 8:52 PM
  • And I did try that, and the compiler flagged it as an error that CA2W was unrecognized. I don't really intend to advertise myself as a C++ programmer, at leasat not in the foreseeable future. I've got WAAAYYY too much to learn before I do that.

    I'm sure I could play around with #includes and get the CA2W to work (I think), but there's no need now. I got the string the way I want it through MultiByteToChar(), but alas, now I find that if I specify a root directory for SHBrowseForFolder(), I can't go up from there, i.e., it confines me to that root or lower. Which I guess is why it's called the pidlRoot, but my goal was to have SHBFF() begin in a certain folder, but also with allowances to go higher, not only lower.
    Lamblion
    Friday, July 24, 2009 9:02 PM
  • Not to flog a dead horse, but if you looked up CA2W in the MSDN library, you'd see that it uses the atlconv.h header.
    Friday, July 24, 2009 9:12 PM
  • Fair enough, and no doubt I would have looked it up if I hadn't already solved the string issue. And like I said, it was all to no avail in the sense that I'm confined to the root dir for SHBFF(), but not in the sense that I learned another few tricks. Of course, if I have missed something on the SHBFF() that allows me to specify a root dir but also allows me to go higher, feel free to chime in.

    In the meantime, I'm going to look up CA2W so I can see where you were leading.
    Lamblion
    Friday, July 24, 2009 9:19 PM
  • Ok. Although I don't think understanding CA2W() is very important. Understanding casting is very important.
    Friday, July 24, 2009 9:52 PM
  • I do have a basic understanding of casting, but it's just whenever I fail to try something that I think won't work, then it works, so even thoughtt I thought the type cast would produce nothing, I tried it anyway just to be sure. And it's no so much understanding typecasting in this case but the compiler, which I doubt I will ever really understand. Although I am thankful for it, as it catches a lot of stuff that keeps me out of trouble.
    Lamblion
    Friday, July 24, 2009 9:55 PM
  • I do have a basic understanding of casting, but it's just whenever I fail to try something that I think won't work, then it works, so even thoughtt I thought the type cast would produce nothing, I tried it anyway just to be sure
    This approach has got a name. It is called Shotgun Debugging.
    Friday, July 24, 2009 10:25 PM
  • This approach has got a name. It is called Shotgun Debugging.

    LOL. That's why I come here and ask so many questions... I want to make sure what I'm doing is legit. I really did learn a good bit having to work out this problem today, even though ultimately I'm back where I started on the actual implementation, but I learned several new functions and how to employ them, and sooner or later, I'm sure they'll come in handy.
    Lamblion
    Friday, July 24, 2009 10:34 PM