Thread: Command Line Arguments in Unicode

  1. #1
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278

    Command Line Arguments in Unicode

    What is the best method to access the command line arguments in a single source Unicode/ASCII program?

    I use __argc and __argv[] to access command line arguments from within MSVC++, however this does not work for strings defined as TCHAR for compliance with Unicode. Is GetCommandLine() and CommandLineToArgvW() the way to go?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    GetCommandLine is good...but you could also;

    Code:
    #define UNICODE
    #define _UNICODE
    #include <windows.h>   
    #include <tchar.h> 
    
    int WINAPI _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
    	LPTSTR lpCmdLine,int nCmdShow)
    {
    	TCHAR szBuff[] = _T("Hello World");
    	return MessageBox(HWND_DESKTOP,szBuff,_T(""),MB_OK);
    }

  3. #3
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    _TCHAR* argv[]?

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227

    Re: Command Line Arguments in Unicode

    Originally posted by JasonD
    I use __argc and __argv[] to access command line arguments from within MSVC++
    You can continue to do so if you wish: __targv is defined in tchar.h as either __argv or __wargv(_UNICODE).

    You just have to use _tWinMain as in Fordy's example - or just parse the command line (LPTSTR lpCmdLine) from there as Fordy implied.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Thank you all for your replies. I had already known about __wargv, but I did not realize that __targv was defined as either __wargv or __argv depending on if it is a Unicode app or not. I had tested for the definition of UNICODE manually, and found that __wargv was an empty string in the case that it was... The problem is that I did not have the entry point of the app set to _tWinMain. It all works fine now. Information on this topic is rather difficult to find. Thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  2. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  3. Unicode - a lot of confusion...
    By Jumper in forum Windows Programming
    Replies: 11
    Last Post: 07-05-2004, 07:59 AM
  4. Should I go to unicode?
    By nickname_changed in forum C++ Programming
    Replies: 10
    Last Post: 10-13-2003, 11:37 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM