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; }
|