Groups | Blog | Home
all groups > visual c > february 2006 >

visual c : convert char * (argv[]) to wstring and to LPCWSTR


ka1cqd
2/28/2006 7:55:27 AM
I have been looking all over the place for a method to take command line
arguments and convert them to a string or wstring so i can process the data
and then covert the resulting strings to LPCWSTRs. I have tryed several
methods and none work. They include:
the convert utilities A2W but that does not compile because of unknow
variables declared in the convert header file.
MultiByteToWideChar which seems to work once but not the second time i go to
use it.
The problem with all the task scheduler examples is they only show how to
work with literal (hardcoded) values for CPCWSTRs. and i can not find any
examples of how to just convert command line arguments to LPCWSTRs.

Thanks in advance,
Fred
ka1cqd
2/28/2006 8:16:27 AM
one additional requirement is that what ever i do works in the non-.NET
environment, meaning the old NT environment.

[quoted text, click to view]
ka1cqd
2/28/2006 9:07:28 AM
I have read all the info about both methods that i could find on MSDN and
used the code examples for there and could not get them to work.

I have since found, mbstowcs() which appears to work. also it appears to
backward compatible.

Where do i find the doucmentation on how to use the MFC/ATL??

How do you tell what version of the complier is being used? When i start
it, it just says visual studio .net and if i look at the versions under help
about i see a number
69586-335-0000007-18820.

[quoted text, click to view]
ka1cqd
2/28/2006 11:09:28 AM
Hi Jochen,

if i follow what you suggested, i need to #define _UNICODE and then
everything is in the wide format and i don't have to deal with the
conversion? Is this correct?

Thank you,
Fred

[quoted text, click to view]
David Wilkinson
2/28/2006 11:51:33 AM
[quoted text, click to view]

Fred:

A2W is part of MFC/ATL so if it does not work it is because you are not
using it correctly. Did you read the documentation?

Likewise for MultiByteToWideChar().

Which version of VC++ are you using?

David Wilkinson
2/28/2006 1:42:46 PM
[quoted text, click to view]

Fred:

MSDN is the documentation. If you do not tell us what goes wrong, we
cannot help you fix it.

VC5: Visual Studio 97
VC6: Visual studio 6
VC7: Visual Studio 2002.Net
VC7.1: Visual Studio 2003.Net
VC8: Visual Studio 2005

It sounds as if you have VC7 or 7.1. As Jochen mentioned, the ATL
conversion macros were improved as of VC7, though there is a bug (fixed
in VC8) that causes them to fail when converting between UTF-8 to UTF-16
in some languages.

David Wilkinson
Jochen Kalmbach [MVP]
2/28/2006 6:30:45 PM
Hi ka1cqd!
[quoted text, click to view]

Why not create a unicode-app?
And define the following entry:

int _tmain(int argc, _TCHAR* argv[])


[quoted text, click to view]

You shouldn´t use A2W, instead use the new ATL7 macros:
Cx like CA2CW

--
Greetings
Jochen

My blog about Win32 and .NET
Jochen Kalmbach [MVP]
2/28/2006 8:13:59 PM
Hi ka1cqd!

[quoted text, click to view]

Yes. By the way: This is the default setting for all new projects in VC2005.

--
Greetings
Jochen

My blog about Win32 and .NET
Mihai N.
2/28/2006 10:14:58 PM
[quoted text, click to view]
Because you use Visual Studio .NET, you can just select "Use Unicode
Character Set" from Project Properties -> General -> Character Set.
It will define both _UNICODE and UNICODE (for the CRT and Win API
respectively).


--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
William DePalo [MVP VC++]
3/2/2006 11:39:23 PM
[quoted text, click to view]

If you want to keep your application ANSI, you can use GetCommandLineW() and
CommandLineToArgvW().

Regards,
Will

AddThis Social Bookmark Button