关于命令行参数的使用问题,实在没分了给50算了

JoriS 2003-10-15 07:28:27
// mian.cpp
//这个程序可以得到预期的结果,就是显示了命令行参数的植,但是为什么回有错误返回?
#include<stdio.h>
#include<iostream.h>
main(int argc, char *argv[])
{ cout<<endl;
while(argc>=1)
{
printf("%s\n",*argv);
argv++;
argc--;
}
}





//textmain.cpp
#include<iostream.h>
#include<stdio.h>
#include<windows.h>
//#include<

main()
{
STARTUPINFO si={sizeof(si)};
PROCESS_INFORMATION pi;

CreateProcess(
NULL, // name of executable module
"E:\\main.exe i am a boy",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi // command line string
);
cout<<GetLastError();
return 0;

}

为什么会出现126=GetLastError(),找不到指定模块?

...全文
35 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
JoriS 2003-10-18
  • 打赏
  • 举报
回复
哈哈我也喜欢星际
给分了!
quanch 2003-10-16
  • 打赏
  • 举报
回复
MSDN
JoriS 2003-10-16
  • 打赏
  • 举报
回复
请问楼上的文档是从哪里得到的?
quanch 2003-10-16
  • 打赏
  • 举报
回复
Behavior of CreateProcess() When Creating a 32-bit Process
Case 1:

If the ApplicationName parameter is passed and the CommandLine parameter is NULL, then the ApplicationName parameter is also used as the CommandLine. This does not mean that you can pass additional command-line parameters in ApplicationName string. For example, the following call will fail with a "File Not Found" error: CreateProcess( "c:\\MyApp.exe Param1 Param2", NULL, ... )

Case 2:

On the other hand, if the CommandLine parameter is non-NULL and the ApplicationName parameter is NULL, then the API attempts to extract the application name from the CommandLine parameter.

Case 3:

The flexibility of the CreateProcess() function (and a possible point of confusion) arises when you pass a valid string pointer to both the ApplicationName and CommandLine parameters. This allows you to specify the application to be executed as well as the complete command line that is passed to the application. One might assume that the command line passed to the created application is a composite of the ApplicationName and CommandLine parameters, but this is not the case. As a result, a process created by CreateProcess can receive a value other than its .exe name as its "argv[0]" parameter. The following is an example of a call to CreateProcess that produces this "abnormal" behavior: CreateProcess( "c:\\MyApp.exe", "Param1 Param2 Param3", ...)

MyApp's arguments will be as follow: argv[0] == "Param1"
argv[1] == "Param2"
argv[2] == "Param3"

NOTE: ANSI specifications require that argv[0] should be equal to the application name, but CreateProcess gives the calling application the flexibility to override this rule for 32-bit processes.
Behavior of CreateProcess When Executing a 16-bit .exe
CreateProcess() does enforce the ANSI specification for parameters passed to 16-bit applications. This raises a potentially confusing inconsistency between the way CreateProcess works from one application to the next and requires you to know whether the application that you are spawning is a 16- bit or 32-bit executable file. To further complicate the issue, CreateProcess is implemented slightly differently in Windows 95 and Windows NT.

Windows NT Behavior:

If the first "parameter" in the CommandLine is not exactly the same as the ApplicationName string, then it replaces it before executing the application. For example if the ApplicationName and CommandLine parameters are as follows: CreateProcess( "c:\\MyApp16.exe", "Param1 Param2 Param3", ...)

Then, the command line arguments that the application sees are as follows: argv[0] == "c:\MyApp16.exe"
argv[1] == "Param2"
argv[2] == "Param3"

Windows 95 Behavior:

If the first "parameter" in the CommandLine is not exactly the same as the ApplicationName string, then CreateProcess fails with a file not found error. As a result, there is no reason to pass anything but NULL as the ApplicationName argument to CreateProcess in Windows 95 if you are executing a 16-bit application.

楼上说的方法也可以,详细语法是:
LPTSTR lpCommandLine = "C:\\\"Program Files\"\\MyApp\\Myfile.bat \"parameter one\" \"parameter two\";
BOOL bRet = CreateProcess( lpApplicationName, lpCommandLine,
//rest of the parameters of CreateProcess );
孤必有邻 2003-10-16
  • 打赏
  • 举报
回复
程序没有问题,126与你无关,与CreateProcess和main.exe都无关。
看看下面的运行结果:

#include <stdio.h>
#include<windows.h>
void main(void)
{
int i;
for(i=0;i<3;i++)
printf("%d\n",GetLastError());
MessageBox(NULL,"Hello","OK",MB_OK);
for(i=0;i<3;i++)
printf("%d\n",GetLastError());
}
microran2000 2003-10-15
  • 打赏
  • 举报
回复
第一个程序在我的Visual Studio.net2003没有任何问题。
第二个程序建议你把"E:\\main.exe i am a boy",修改
为"E:\\main.exe \"i am a boy\"",
课程基础概述本门课程,大喵将会打着大家从零打造一款属于大家自己的 CLI命令行脚手架工具,本课程主要面向新手同学,对命令行工具开发,前端工具开发感兴趣的同学,可以通过本门课程学习到如何使用Node.JS开发一款适配自身项目特色的脚手架命令行工具。并且也会带着大家推送自己开发完成的CLI工具到NPM线上仓库,供所有人下载安装使用。整个课程,大喵会详细讲解所需要依赖的第三方模块包的具体使用,让同学们可以举一反三开发具备自身特色的CLI工具,最终大喵会运用到前面所讲解的基础内容,独立自主开发一个具备 damiao [ add | delete | list | init ] 四种子命令的CLI脚手架工具。CLI命令行工具命令行工具(Cmmand Line Interface)简称cli,顾名思义就是在命令行终端中使用的工具。我们常用的 git 、npm、vim 等都是 cli 工具,比如我们可以通过 git clone 等命令简单把远程代码复制到本地。在目前前端工程大流行的环境下,vue-cli、create-reate-app、angular-cli 等等方便快捷的命令行脚手架工具诞生,极大的提升了我们的开发效率与质量,我们可以通过这些脚手架工具在本地快速构建我们的开发项目。 课程目录结构1、CLI课程介绍   2、CLI课程纲领和讲师介绍3、CLI课程内容概述    4、CLI命令行工具及场景应用5、CLI课程准备工具和技术   6、CLI课程收获和收益  7、CLI课程实战案例介绍   8、npm CLI 命令行工具发展现状9、创建第一个命令行自定义命令   10、process.argv 接收命令行参数11、commander 工具使用介绍一   12、commander 工具使用介绍二13、inquirer 工具使用介绍   14、ora 加载工具使用介绍15、chalk 颜色工具使用介绍   16、download-git-repo 仓库工具介绍17、创建脚手架命令   18、创建命令可执行文件19、damiao add 命令行指令开发   20、damiao add 模板录入校验21、damiao delete 命令行指令开发   22、damiao list 命令行指令开发23、damiao init 初始化项目指令开发   24、damiao init 指令参数输入校验25、damiao 命令行指令测试.mp4  26、创建npm个人账号及发布npm仓库27、全局安装脚手架工具

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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