通过命令行获取计算机参数,Win32命令行参数的传到和获取

Win32命令行参数的传入和获取

Win32控制台,如何传入和获取命令行参数的有关问题,

有几种解决办法,

总结出来,朋友们一起分享

// tt.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include

#include "afxinet.h"

#include

#include

using namespace std;

#define BUFSIZE MAX_PATH

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

{

///*************

//传入或者获取win32命令行传参数

//**************/

//方法一

wstring strExePath=L"C:\\Program Files\\111.exe";

wstring strParamete=L"W";

ShellExecute(NULL, _T("open"), strExePath.c_str(), strParamete.c_str(), NULL, SW_SHOWNORMAL);

方法二

///**********

这种传参数的方法不可行

//argv[1]=L"C:\\111.exe";

//argv[2]=L"G";

//***********/

方法三 使用for循环

for(int i=0; i< argc; i++)

{

_TCHAR* Str_Cmd = argv[i];

string sStr_Cmd ;

//Unicode转换为ASCII,也即是宽字节转换为单字符,代码

int UnicLen;

UnicLen=::WideCharToMultiByte(CP_UTF8,NULL,Str_Cmd,wcslen(Str_Cmd),NULL,0,NULL,NULL);

char*str_ASCII=new char[UnicLen+1];

ZeroMemory(str_ASCII,UnicLen+1);

::WideCharToMultiByte(CP_UTF8,NULL,Str_Cmd,wcslen(Str_Cmd),str_ASCII,UnicLen,NULL,NULL);

str_ASCII[UnicLen]='\0';

/

sStr_Cmd = str_ASCII;

wcout<

}

/**************

方法四GetCommandLineW()

***************/

/*wstring str_cmd;

str_cmd = ::GetCommandLineW();

*/

/**************

方法五CommandLineToArgvW()

***************/

/**

LPWSTR *szArgList;

int argCount;

szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);

if (szArgList == NULL)

{

MessageBox(NULL, L"Unable to parse command line", L"Error", MB_OK);

return 10;

}

for(int i = 0; i < argCount; i++)

{

MessageBox(NULL, szArgList[i], L"Arglist contents", MB_OK);

}

LocalFree(szArgList);

***/

//判断结果

if(argc == 1)

{

cout<

//return 0;

}else if(argc == 2){

cout<

}else{

cout<

}

//方法六 AfxGetApp()->m_lpCmdLine;

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值