关于命令行参数的使用问题,实在没分了给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(),找不到指定模块?

...全文
27 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\"",

15,471

社区成员

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

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