分离WinMain中传进来的字符串(迁移2011-01-14)

Table of Contents

    我们知道WinMain函数是可以接受外部字符串的, 但是它获取的是整个字符串, 如果要获取多个字符串, 就必须把源字符串分离开, 下面是MSDN里介绍的方法:

    #include <windows.h>
    #include <stdio.h>
    #include <shellapi.h>
    int __cdecl main()
    {
       LPWSTR *szArglist;
       int nArgs;
       int i;
       szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
       if( NULL == szArglist )
       {
            wprintf(L"CommandLineToArgvW failed/n");
            return 0;
       }
       else
       {
    	for( i=0; i<nArgs; i++)
    	    printf("%d: %ws/n", i, szArglist[i]);
       }
       LocalFree(szArglist);
       return(1);
    }