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

G

Guest

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
 
G

Guest

one additional requirement is that what ever i do works in the non-.NET
environment, meaning the old NT environment.
 
D

David Wilkinson

ka1cqd said:
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

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
 
G

Guest

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.
 
J

Jochen Kalmbach [MVP]

Hi ka1cqd!
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.

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

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

They include:
the convert utilities A2W but that does not compile because of unknow
variables declared in the convert header file.

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

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
D

David Wilkinson

ka1cqd said:
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.

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
 
G

Guest

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

Jochen Kalmbach said:
Hi ka1cqd!
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.

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

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

They include:
the convert utilities A2W but that does not compile because of unknow
variables declared in the convert header file.

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

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
J

Jochen Kalmbach [MVP]

Hi ka1cqd!
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?

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

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
M

Mihai N.

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?
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).
 
W

William DePalo [MVP VC++]

ka1cqd said:
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.

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

Regards,
Will
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top