如何写出带执行参数的程序

thisismyway 2012-09-18 09:27:22
如何写出带执行参数的程序啊,可能有人没懂我意思,我先解释下。
比如你可以在运行或命令提示符内输入notepad.exe new.txt,就可以打开C盘目录下new.txt文件,如果没有记事本程序就提示新建一个
再比如在Chrome浏览器,在其桌面快捷方式,选择-"属性"-"快捷方式",然后在"目标"一栏尾部添加参数 --enable-easy-off-store-extension-install ,然后再运行浏览器就可以像以前那样正常安装 Web Store 之外的第三方扩展应用及脚本程序了。


希望有人能用C或C++写出一个小例子,让我参考下,比如写一个两数相加的简单命令行程序,在命令行输入Add.exe 5, 4就能输出结果9的。
...全文
369 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
thisismyway 2012-09-18
  • 打赏
  • 举报
回复
刚刚Google下,确实有int main(int argc,char* argv[])这种命令行传参函数,但我实际上我需要动态完成某项操作,就像批处理一样。char* argv[]数组可以打印出来,却无法做出相应处理动作。
再打个比方,我写了一个删除文件的程序,假设生成名为delete.exe,通过指定参数如delete.exe *.tmp就能完成删除tmp后缀名的临时文件。望详细给出sample源码,不要云里雾里了。
ouyh12345 2012-09-18
  • 打赏
  • 举报
回复
如果是console,则象一楼那样
如果是mfc,则本身就可以带参数了,但参数得自己解析
傻X 2012-09-18
  • 打赏
  • 举报
回复
楼主是你没看懂1楼的意思吧

Test.exe 1 2 3 -enable 就是执行的时候输入的,命令行啊

不过我一直用GetCommandLine()来获取输入参数的
thisismyway 2012-09-18
  • 打赏
  • 举报
回复
看来你没看懂我意思,参数不是在函数里指定的,是在执行时带上的。就像ping命令,ping 127.0.0.1和ping www.baidu.com里127.0.0.1和www.baidu.com是在运行ping.exe时指定的。
你妹的特盗不 2012-09-18
  • 打赏
  • 举报
回复
int main(int argc,char* argv[])
如果程序名字是Test.exe
那么执行命令 Test.exe 1 2 3 -enable
argc = 5
argv[0]=Test.exe
argv[1]=1
argv[2]=2
argv[3]=3
argv[4]=-enable
thisismyway 2012-09-18
  • 打赏
  • 举报
回复
再次感谢各位!
thisismyway 2012-09-18
  • 打赏
  • 举报
回复
感谢各位,马上散分!
1楼、6楼和7楼的lfs09有必要这么气愤吗,本来就是交流的,对某些概念理解错误很正常。这里贴出我在别人显隐系统托盘图标代码基础上添加执行参数的代码:

#include <atlbase.h>
#include <atlconv.h>
#include <CommCtrl.h>
#include <IOSTREAM.H>
#include <STRING.H>


void ShowTrayIcon(char szIcon[],BOOL show)
{
HWND hWnd,hWndPaper;
unsigned long lngPID;
long ret,lngButtons;
HANDLE hProcess;
LPVOID lngAddress;
long lngTextAdr,lngHwndAdr,lngHwnd,lngButtonID;
char strBuff[1024]={0};
char* str = NULL;
char *pp = NULL;

hWnd = FindWindow("Shell_TrayWnd", NULL);
hWnd = FindWindowEx(hWnd, 0, "TrayNotifyWnd", NULL);
hWndPaper = FindWindowEx(hWnd, 0, "SysPager", NULL);
if(!hWndPaper)
hWnd = FindWindowEx(hWnd, 0, "ToolbarWindow32", NULL);
else
hWnd = FindWindowEx(hWndPaper, 0, "ToolbarWindow32", NULL);
ret = GetWindowThreadProcessId(hWnd, &lngPID);
hProcess = OpenProcess(PROCESS_ALL_ACCESS
|PROCESS_VM_OPERATION
|PROCESS_VM_READ
|PROCESS_VM_WRITE,
0,
lngPID);
lngAddress = VirtualAllocEx(hProcess,0, 0x4096, MEM_COMMIT, PAGE_READWRITE);
lngButtons = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);

for(int i=0 ;i< lngButtons - 1;i++)
{
ret = SendMessage(hWnd,TB_GETBUTTON,i,long(lngAddress));
ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 16),&lngTextAdr,4,0);
if(lngTextAdr != -1)
{
ret = ReadProcessMemory(hProcess, LPVOID(lngTextAdr),strBuff,1024,0);
ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 12),&lngHwndAdr,4,0);
ret = ReadProcessMemory(hProcess, LPVOID(lngHwndAdr),&lngHwnd, 4,0);
ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 4),&lngButtonID,4,0);
USES_CONVERSION;
str = OLE2T((LPOLESTR)(strBuff));
pp=strstr(str,szIcon);
if(pp != NULL)
{
if(show)
{
SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,0);
}
else
{
SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,1);
}
}
}
}
VirtualFreeEx( hProcess, lngAddress, 0X4096, MEM_RELEASE);
CloseHandle(hProcess);
}

/*
调用方法:
char szIcon[] :要隐藏的托盘图标;BOOL show:false 为隐藏图标,true 为显示图标。例如,隐藏
金山词霸的托盘图标:
ShowTrayIcon("金山词霸",false);
*/
/*
void main()
{
ShowTrayIcon("迅雷精简版",true);
}
*/
int main( int argc, char *argv[])
{
if (argc!=3)
{
cout<<"参数调用错误"<<endl;
return 0;
}
else
{
int result1 = strcmp(argv[2],"-show");
int result2 = strcmp(argv[2],"-hide");
if( result1==0)
{
ShowTrayIcon(argv[1],true);
return 1;
}
if( result2==0)
{
ShowTrayIcon(argv[1],false);
return 1;
}
else
{
cout<<"需带参数运行或参数调用错误,请看下面详细说明:"<<endl;
cout<<"HideTrayIcon.exe tray_icon_name [-show/-hide]"<<endl;
cout<<"参数1 tray_icon_name指定要隐藏的托盘图标名,鼠标移动到托盘图标上即可看到;"<<endl;
cout<<"参数2 [-show/-hide]为-show时显示改托盘图标,-hide时隐藏改托盘图标。"<<endl;
cout<<"如 HideTrayIcon.exe 金山词霸 -hide 就可以隐藏金山词霸托盘图标。"<<endl;
return 0;
}
}
}


Eleven 2012-09-18
  • 打赏
  • 举报
回复
GetCommandLine() CommandLineToArgvW()自己解析命令行参数
Gloveing 2012-09-18
  • 打赏
  • 举报
回复
main, wmain
main( int argc, char *argv[ ], char *envp[ ] )
{
program-statements
}

wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
{
program-statements
}

The main function marks the beginning and end of program execution. A C or C++ program must have one function named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, which is wmain.

The main and wmain functions can take the following three optional arguments, traditionally called argc, argv, and envp (in that order):

argc

An integer specifying how many arguments are passed to the program from the command line. Because the program name is considered an argument, argc is at least 1.

argv


An array of null-terminated strings. It can be declared as an array of pointers to char (char *argv[ ] or wchar_t *argv[ ] for wmain) or as a pointer to pointers to char (char **argv or wchar_t **argv for wmain). The first string (argv[0]) is the program name, and each following string is an argument passed to the program from the command line. The last pointer (argv[argc]) is NULL.
你妹的特盗不 2012-09-18
  • 打赏
  • 举报
回复
再解釋一次,不想多說了
以下代碼是爭對CONSOLE 程序的,如果你是mfc,請用3樓的方法.
int main(int argc,char* argv[])
如果程序名字是Test.exe
那么执行命令 Test.exe 1 2 3 -enable
上面的命令是你在DOS環境裡面執行的.
下面的解釋是,你執行命令後,程序獲取到的值.
argc = 5
argv[0]=Test.exe
argv[1]=1
argv[2]=2
argv[3]=3
argv[4]=-enable

int main(int argc,char* argv[])
{
int index=0;
for(index<argc;index++)
printf("%s\n",argv[index]);
}

自己編譯上面代碼,看結果.
真是無語.
你妹的特盗不 2012-09-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

刚刚Google下,确实有int main(int argc,char* argv[])这种命令行传参函数,但我实际上我需要动态完成某项操作,就像批处理一样。char* argv[]数组可以打印出来,却无法做出相应处理动作。
再打个比方,我写了一个删除文件的程序,假设生成名为delete.exe,通过指定参数如delete.exe *.tmp就能完成删除tmp后缀名的临时文件。望详细给出sam……
[/Quote]

一樓的你看不懂, 是我笨,說不清楚,坐等高人解釋.

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧