tqsheng

go.....
随笔 - 366, 文章 - 18, 评论 - 101, 引用 - 0
数据加载中……

WinMain支持命令行参数的代码

 

#include "stdafx.h"
#include 
<STRING.h>
#include 
<STDIO.H>
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     
int       nCmdShow)
{
     
// TODO: Place code here.
    char *pCmdLine = GetCommandLine();
    
char  sep[] = " ";
    
char *token = NULL;
    
char  argv[10][10= {0};
    
int      argc = 0;
    
    token 
= strtok(pCmdLine,sep);
    
while (token != NULL)
    {
        strcpy(argv[argc
++],token);
        OutputDebugString(token);
        token 
= strtok(NULL,sep);
    }
    
return 0;
}


WinMain支持命令行参数的代码

#include <windows.h>
#include 
<stdio.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
{
        
//for the command line;
        
        
int argc = 0;
        
        LPWSTR 
*lpszArgv = NULL;

        
        LPWSTR szCmdLine 
=(LPWSTR)::GetCommandLineW(); //获取命令行参数;
        
        lpszArgv 
= ::CommandLineToArgvW((const unsigned short *)szCmdLine, &argc); //拆分命令行参数字符串;
        

        
        
if (lpszArgv == NULL)
        {
        
                MessageBox(NULL, 
"Unable to parse command line""Error", MB_OK);
                
return 10;
        }
        
        
for(int i = 0; i < argc; i++)

        {
                
char   str[MAX_PATH];
                memset(str,
0,MAX_PATH);
        
//将LPWSTR转换为char *:
        WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,lpszArgv[i], -1,str,200,NULL,NULL);
        MessageBox(NULL,str, 
"Arglist contents", MB_OK);
            
        }
        
        LocalFree(lpszArgv);
        
return 0;
}


一个简单的 方法用stdlib.h中的__argc,__argv

#include <windows.h>
#include 
<stdlib.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
{
        
//for the command line;
        for(int i = 0; i <__argc; i++)

        {
                
char   str[MAX_PATH];
                
char * str1;
                memset(str,
0,MAX_PATH);
                
//下面是获取命令行参数值的典型用法:
            lstrcpy(str,__argv[i]);
                str1
=strdup(__argv[i]);
            
//MessageBox(NULL,str, "Arglist contents", MB_OK);
                MessageBox(NULL,str1, "Arglist contents", MB_OK);
            
        }
        
return 0;
}





 

posted on 2012-03-30 07:01 tqsheng 阅读(563) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理