65.9K
CodeProject 正在变化。 阅读更多。
Home

命令行类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.42/5 (9投票s)

2001年9月28日

1分钟阅读

viewsIcon

127652

downloadIcon

1746

一个允许轻松获取命令行参数的类。

概述

我创建这个类的目的是为了方便地获取命令行参数。
这受到问题论坛中多次提出的以下问题启发:
- 如何获取命令行参数?

为此,我创建了CCommandLine

C命令行类。

CCommandLine::CCommandLine

构造一个CCommandLine对象。

CCommandLine();

CCommandLine::GetFirstParameter

GetFirstParameter返回第一个标志及其参数。

BOOL GetFirstParameter(CString& strFlag, CString& strParam);

参数

CString& strFlag 指向一个用于返回标志的缓冲区的指针。

CString& strParam 指向一个用于返回参数的缓冲区的指针。

如果函数有参数要返回,则返回TRUE

备注

如果命令行有一个没有标志的参数,则strFlag变量将为空。

参见示例


CCommandLine::GetNextParameter

GetNextParameter返回下一个标志及其参数。

BOOL GetNextParameter(CString& strFlag, CString& strParam);

参数

CString& strFlag 指向一个用于返回标志的缓冲区的指针。

CString& strParam 指向一个用于返回参数的缓冲区的指针。

如果函数有参数要返回,则返回TRUE

备注

如果命令行有一个没有标志的参数,则strFlag变量将为空。

参见示例


CCommandLine::GetCommandLine

GetCommandLine返回正在运行的应用程序的命令行字符串。

void GetCommandLine(CString& strCommand);

参数

CString& strCommand 指向一个用于返回命令行的缓冲区的指针。


CCommandLine::GetAppName

GetAppName返回正在运行的应用程序的应用程序名称。

void GetAppName(CString& strAppName);

参数

CString& strAppName 指向一个用于返回应用程序名称的缓冲区的指针。


CCommandLine::GetAppPath

GetAppPath返回应用程序被执行的完整路径。

void GetAppPath(CString& strAppPath);

参数

CString& strAppPath 指向一个用于返回应用程序路径的缓冲区的指针。


示例

BOOL CTestApp::InitInstance()
{
	CCommandLine pCmd;
	CString strFlag = _T("");
	CString strParam = _T("");

	BOOL bRet = pCmd.GetFirstParameter(strFlag, strParam);
	while(bRet)
	{
		DoSomethingWithTheParameter(strFlag, strParam);
		bRet = pCmd.GetNextParameter(strFlag, strParam);
	}
	.....
	.....
}

Carlos A. Antollini。

© . All rights reserved.