none
How to convert Unicode MFC command line string to ascii char array ?

    Question

  • Hi,

    I have an application which has some commandline arguments.

    this is running fine in its console version.

    When I try to put all these things in a MFC application the application does not run. I mean Iam not able to feed the application with proper commanline arguments. But I copied the same string here.

    I dont know why?

     

    Please let me know how to proceed.

     

    thanks

    Srikanth.

     

    Monday, November 12, 2007 12:06 PM

Answers

  •  

    You are using the whole command line as argument 0 (argv[0]) and therefore it does not work.

     

    As told, you should better use CommandLineToArgvW.

     

    Code Block

     

    BOOL bInitAPI()

    {

    // Get the command line and parse arguments

    int iArgCount = 0, iIdx;

    LPWSTR * pszArgVectorWide = CommandLineToArgvW(GetCommandLineW(),&iArgCount);

     

    // Convert the wide string array into an ANSI array (input is ASCII-7)

    LPSTR * pszArgVectorAnsi = new LPSTR [iArgCount];

    for (iIdx = 0; iIdx < iArgCount; ++iIdx)

    {

     size_t qStrLen = wcslen(pszArgVectorWide[iIdx]), qConverted = 0;

     pszArgVectorAnsi[iIdx] = new CHAR [qStrLen+1];

     wcstombs_s(&qConverted,pszArgVectorAnsi[iIdx],qStrLen+1,

       pszArgVectorWide [iIdx],qStrLen+1);

    }

     

    // Call your ANSI API here

    // ...

     

    // Delete used memory

    for (iIdx = 0; iIdx < iArgCount; ++iIdx) delete [] pszArgVectorAnsi[iIdx];

    delete [] pszArgVectorAnsi, pszArgVectorAnsi = 0;

    LocalFree(pszArgVectorWide); pszArgVectorWide = 0;

     

    return iIdx>0;

    }

     

     

    Note that since your API is ANSI, no special length check must be done for MBCS.

     

    Tuesday, November 13, 2007 12:18 PM

All replies

  • There is no difference for the command line arguments in the use of mfc or not.

    I guess that you're trying to parse the command line arguments from a windows (not a console) application. If so, take a look at the CommandLineToArgvW and  GetCommandLineW  functions.

    In CWinApp derived classes, you may also want to comment the default call to ParseCommandLine.

    Hope this helps.

    Monday, November 12, 2007 12:47 PM
  •  

    I tried but that was'nt helpfull. Iam to send the ascii string to a 3rd party lib.

    Ok, how do i convert the commandline string(unicode) in MFC (VS 8  ) to a ascii string.

     

     

    -Srikanth.

     

    Tuesday, November 13, 2007 4:40 AM
  • Tuesday, November 13, 2007 8:32 AM
  •  

    I used Widechartomultibyte but did not get the result.

     

    Iam using some 3rd party code (ACE network).

     

    If I use the console application(commandline string)  then i was able to use them properly.

    but If iam using any windows based (win32 or MFC) may be I was not able to convert it to a proper ascii format.

     

    any ideas to make it.

     

    -Srikanth.

     

    Tuesday, November 13, 2007 10:42 AM
  • It should work.

    Try posting a snippet about how are you calling these functions and how were you retrieving your arguments in your console version. I'll try to have a quick look at it.

    Tuesday, November 13, 2007 10:55 AM
  •  

    Console version -

     

     

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

    {

    Client_Logging_Daemon cld;

    cld.init(argc,argv);

    while(1)

    {

          // do some thing

    }

    return 0;

    }

     

     

     

     Windows version -

    {

    LPWSTR str1 = GetCommandLineW();

    int a;// = strlen(&str1) + 1;

    LPSTR anstr = 0;

    U2A(str1,&anstr);

    if(cld.init(1,&anstr1)!=-1)

    {

    while(1)

    {

       //do something

    }

     

    }

    }

     

    // this function is from net.

     

    DWORD U2A(LPWSTR pszW, LPSTR *ppszA)

    {

    ULONG cbAnsi, cCharacters;

    DWORD dwError;

    // If input is null then just return the same.

    if (pszW == NULL)

    {

    *ppszA = NULL;

    return NOERROR;

    }

    cCharacters = wcslen(pszW)+1;

    // Determine number of bytes to be allocated for ANSI string. An

    // ANSI string can have at most 2 bytes per character (for Double

    // Byte Character Strings.)

    cbAnsi = cCharacters*2;

    // Use of the OLE allocator is not required because the resultant

    // ANSI string will never be passed to another COM component. You

    // can use your own allocator.

    *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);

    if (NULL == *ppszA)

    return E_OUTOFMEMORY;

    // Convert to ANSI.

    if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,

    cbAnsi, NULL, NULL))

    {

    dwError = GetLastError();

    CoTaskMemFree(*ppszA);

    *ppszA = NULL;

    return HRESULT_FROM_WIN32(dwError);

    }

    return NOERROR;

    }

     

     

     

     

     

     

    cld.init(..) fails in windows environment but it works find in windows console env.

     

    any ideas would be very greatfull

     

    Thanks,

    -Srikanth.

    Tuesday, November 13, 2007 11:15 AM
  •  

    You are using the whole command line as argument 0 (argv[0]) and therefore it does not work.

     

    As told, you should better use CommandLineToArgvW.

     

    Code Block

     

    BOOL bInitAPI()

    {

    // Get the command line and parse arguments

    int iArgCount = 0, iIdx;

    LPWSTR * pszArgVectorWide = CommandLineToArgvW(GetCommandLineW(),&iArgCount);

     

    // Convert the wide string array into an ANSI array (input is ASCII-7)

    LPSTR * pszArgVectorAnsi = new LPSTR [iArgCount];

    for (iIdx = 0; iIdx < iArgCount; ++iIdx)

    {

     size_t qStrLen = wcslen(pszArgVectorWide[iIdx]), qConverted = 0;

     pszArgVectorAnsi[iIdx] = new CHAR [qStrLen+1];

     wcstombs_s(&qConverted,pszArgVectorAnsi[iIdx],qStrLen+1,

       pszArgVectorWide [iIdx],qStrLen+1);

    }

     

    // Call your ANSI API here

    // ...

     

    // Delete used memory

    for (iIdx = 0; iIdx < iArgCount; ++iIdx) delete [] pszArgVectorAnsi[iIdx];

    delete [] pszArgVectorAnsi, pszArgVectorAnsi = 0;

    LocalFree(pszArgVectorWide); pszArgVectorWide = 0;

     

    return iIdx>0;

    }

     

     

    Note that since your API is ANSI, no special length check must be done for MBCS.

     

    Tuesday, November 13, 2007 12:18 PM
  • Hi,

     

    thanks very much.

    I tried with this code, the string is successfully converted but i dont know for what good reason Im not able to connect to the server.

     

    Can you please tell me where can I learn all the string related operations(including managed and unmanaged) ?

     

    thanks,

    Srikanth.

     

     

     

    Wednesday, November 14, 2007 5:26 AM
  • The MSDN is always a good start point.

    There are several libraries, the listing is not complete:

    String Manipulation (CRT) (C runtime)
    STL strings (C++ portable)
    Using CString (ATL/MFC) (C++ MFC)
    CComBSTR Class (C++ COM)
    String Class (.net) (C++/CLI)

    The web is also plenty of online courses and samples. Just google a bit around,...
    Wednesday, November 14, 2007 9:02 AM
  • thanks very much,

     

    I got it connected, I had to include support for the windows sockets.... I thought as its a 3rd party lib independent of windows, it does not need windows sockets. 

     

    thanks,

    Srikanth.

     

    Wednesday, November 14, 2007 9:19 AM