Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


C Wchar_t, Tchar, Unicode

  • Please log in to reply
No replies to this topic

#1 jpshortstuff


    WhatTheTech Teacher

  • Members
  • 660 posts
  • Gender:Male
  • Location:UK
  • Local time:05:20 AM

Posted 21 August 2008 - 04:25 AM

Hi all.

I have decided to begin working with Unicode compatible programs and have started the process of easing my mind into the correct way of thinking for this task. Quite quickly I encountered a couple of issues that I couldn't seem to understand the cause of, so I am hoping that someone here can provide an insight. Thanks in advance for any help with these problems.

The first problem seems to relate to a simple concatenation of wchar_ts. Firstly, I have come to understand that (correct me if I'm wrong) TCHAR is infact a wchar_t if UNICODE is defined and so I have been using TCHAR extensively. All I am trying to do in this snippet is obtain the value of the %systemdrive% variable and pre-pend it to a file path.

Here is my first attempt:
TCHAR* sysdrive = new TCHAR[2];
ExpandEnvironmentStrings(L"%systemdrive%", sysdrive, 2);
log = _tcscat(sysdrive, L"\\Folder\\log.txt"); //this line causes program to crash, could be result of above line though?
Pretty much directly converted from the working char* version I have been using.

Having no idea what could be wrong on where the problem lies, I decided that the sysdrive is probably not going to be a Unicode character so I would see if it worked with a char* casted to a TCHAR*:
char* sysdrive = new char[2];
ExpandEnvironmentStringsA("%systemdrive%", sysdrive, 2);
log = _tcscat((TCHAR*)sysdrive, L"\\Folder\\log.txt"); //this line causes program to crash, could be result of above line though?

Still no luck. Since I am still in the first stages of non char characters and general Unicode API use there is probably something fundamental that I have missed, or it could be the (badly)self-taught C catching up to me.

Plugging on to the second identifiable issue with my program, I can't seem to work out how to compare my TCHAR*s correctly. Here is a comparison that just wont go:
if(_tcscmp(argv[1], L"/?")==0) //argv being the command parameters from main()
Simple stuff, just checking to see if someone used the /? command-line switch with my program. No joy with this one either (no crashes, just no joy).

If anyone can offer any help or advice with either of these issues, or point me in the direction of a good Unicode handling tutorial (or even both!) then I would greatly appreciative.

Thanks :thumbsup:
EDIT: Ok, I have spotted the huge buffer overflow magnet, and that is now sorted. The /? comparison still doesn't work though...

Edited by jpshortstuff, 21 August 2008 - 08:48 AM.

Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image

BC AdBot (Login to Remove)


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users