Solved

converting unicode LPTSTR to WCHAR[]

Posted on 2004-08-27
2
1,723 Views
Last Modified: 2008-03-17
When I step through the code below, I put a watch on szFile and it shows it as containing "c:\files\李克勤 - 飛花.mpg" (chinese characters in unicode).
I need to convert that to WCHAR[]. so I do a:

wcsncpy(wFile, T2W(szFile,CP_UTF), NUMELMS(wFile)-1);
However, wFile now contains: "c:\files\§õ§J¶Ô - ­¸ªá.mpg"

The next bit of code tries to open the file specified by wFile but doesn't succeed because  "c:\files\§õ§J¶Ô - ­¸ªá.mpg" does not exist. The file that should be opened is "c:\files\李克勤 - 飛花.mpg". I would like the filename to stay as unicode instead of being converted according to the codepage. What am I doing wrong? Is there something I don't understand?

----------
HRESULT PlayMovieInWindow(LPTSTR szFile)
{
    USES_CONVERSION;
    WCHAR wFile[MAX_PATH];
    HRESULT hr;

    if (!szFile)
        return E_POINTER;

    // Clear open dialog remnants before calling RenderFile()
    UpdateWindow(ghApp);

    // Convert filename to wide character string
    wcsncpy(wFile, T2W(szFile), NUMELMS(wFile)-1);

-------

Edit:: Sorry the unicode I typed did not come out properly...
szFile should contain the chinese unicode...
wFile  ends up being converted to multibyte.. so looks like gibberish... but I would like it to be unicode.. because the filename of the file on the drive is in unicode.
0
Comment
Question by:theblip
2 Comments
 
LVL 16

Accepted Solution

by:
nonubik earned 125 total points
ID: 11919743
You'll just need to build your project using Unicode char set, or define UNICODE

Then the LPTSTR will be a WCHAR FAR *. So you won't need to convert the file name.
0
 

Author Comment

by:theblip
ID: 11924508
Thanks for the tip.

It works now without conversion.

I had to do both a
#define UNICODE
and
#define _UNICODE

hmm.. do both need to be defined or just _UNICODE? it doesn't seem to work with just one.
I need to have both #define UNICODE and #define _UNICODE or I get type errors.

But yea.. it works now if I have both defined.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now