Convert windows tchar argv list to classical char * argv

Source: Internet
Author: User

Http://www.wincli.com /? P = 72

 

 

Please people used to classical C have hard time adopting the code to Windows types. the code below extends strates one of the frequent questions: how to use tchar arguments with good old code expecting char * in arguments with minimum blood?

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 # Include "stdafx. H" # Include "stdio. H" # Include "stdlib. H" // Returns number of tchars in string Int Wstrlen (_ tchar * wstr) { Int Rochelle idx = 0; While ((( Char *) Wstr) [l_idx]! = 0) l_idx + = 2; Return Rochelle idx; } // Allocate char string and copy tchar-> char-> string Char * Wstrdup (_ tchar * wsrc) { Int Rochelle idx = 0; Int Rochelle Len = wstrlen (wsrc ); Char * L_nstr = ( Char *) Malloc (L_len ); If (L_nstr ){ Do { Rochelle NSTR [Rochelle idx] = ( Char ) Wsrc [l_idx]; Rochelle idx ++; } While (( Char ) Wsrc [l_idx]! = 0 ); } NSTR [l_idx] = 0; Return Rochelle NSTR; } // Allocate argn structure parallel to argv // Argn must be released Char ** Allocate_argn ( Int Argc, _ tchar * argv []) { Char ** L_argn = ( Char **) Malloc (Argc * Sizeof ( Char *)); For ( Int Idx = 0; idx <argc; idx ++ ){ Rochelle argn [idx] = wstrdup (argv [idx]); } Return Rochelle argn; } // Release argn and its content Void Release_argn ( Int Argc, Char ** Nargv) { For ( Int Idx = 0; idx <argc; idx ++ ){ Free (Nargv [idx]); } Free (Nargv ); } //////////////////////////////////////// //////////////////////////////// //////////////////////////////////////// //////////////////////////////// // Use exampe: //////////////////////////////////////// //////////////////////////////// Int _ Tmain ( Int Argc, _ tchar * argv []) { Char ** Argn = allocate_argn (argc, argv ); // Optionally # define argv argn If (Argc> 1 ){ Printf ("Arg 1 = '% s' \ n", argn [1]); // Just argn intead of argv } Release_argn (argc, argn ); Return 0; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.