用GetExitCodeProcess获得进程的退出码;怎么样获得calculator。exe的退出码

dudu_kang1987 2010-12-05 12:14:05
拜托童鞋们帮我看看这段代码;
#include "StdAfx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>


void main(int argc,char *argv[]){

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
HANDLE m_hProcess=0;
// printf("%s\n",argv[1]);
if(argv[1]){
char *p=argv[1];
LPWSTR string= {0};
LPWSTR string1= {0};

LPDWORD dwexitcode;

wsprintf(string,_T("p"));
wsprintf(string1,_T("F:\\calculator.exe %s"),p);


bool bsucess = CreateProcess(NULL,
string1,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);

if(bsucess){
m_hProcess=pi.hProcess;
CloseHandle(pi.hThread);
WaitForSingleObject(m_hProcess,INFINITE);

GetExitCodeProcess(m_hProcess,dwexitcode);

printf("%l",&dwexitcode);
}
}
getchar();
}
下面这个是我的calculator.exe的主函数,是要把上面的代码的命令行参数传给calculator进行计算,将计算结果作为退出码,然后上面的代码获取这个退出码,打印出来;
main(int argc, char *argv[])
{
// printf("%d\n",argc);
if(argv[1])
{
printf("%s=%f\n\n",argv[1],calval(argv[1]));
return calval(argv[1]);
}
else
{
printf("___");

}
getchar();
}
...全文
412 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
dudu_kang1987 2010-12-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 dudu_kang1987 的回复:]
拜托童鞋们帮我看看这段代码;
#include "StdAfx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>


void main(int argc,char *argv[]){

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMe……
[/Quote]
我改了 还是不行;
cranium 2010-12-05
  • 打赏
  • 举报
回复
用这个 GetExitCodeProcess(m_hProcess,dwexitcode) 有点问题。
函数第二参数需要一个指针,但是你的dwexitcode没有指向任何地方。


DWORD dwexitcode;
GetExitCodeProcess(m_hProcess,&dwexitcode);
printf("%d",dwexitcode);
cranium 2010-12-05
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 dudu_kang1987 的回复:]

在我的calculator.exe的主函数中return calval(argv[1]);
返回的是这个函数计算的结果;
在上面的代码中用 GetExitCodeProcess获取calculator的退出码,然后打印出来;
现在创建这个进程,然后把命令行参数传给calculator,就直接把计算结果打印出来了;
这个要怎么实现呢;
[/Quote]

你发的问题不就是实现这个的吗?mian函数的返回值就是程序的返回值。
dudu_kang1987 2010-12-05
  • 打赏
  • 举报
回复
在我的calculator.exe的主函数中return calval(argv[1]);
返回的是这个函数计算的结果;
在上面的代码中用 GetExitCodeProcess获取calculator的退出码,然后打印出来;
现在创建这个进程,然后把命令行参数传给calculator,就直接把计算结果打印出来了;
这个要怎么实现呢;
cranium 2010-12-05
  • 打赏
  • 举报
回复
纠正下我上面写的:

//LPWSTR sting[256]; 这么写错误了,我们要的是字符数组,不是指针数组

TCHAR cmdline[256]; //string不适合做变量名,换成cmdline吧
cranium 2010-12-05
  • 打赏
  • 举报
回复
呃,发现问题了,今天你是我遇到的第3个用指针不分配内存的人。

LPWSTR string= {0}; //完全不明白为什么这么写?
LPWSTR string1= {0};

LPDWORD dwexitcode;

wsprintf(string,_T("p"));
wsprintf(string1,_T("F:\\calculator.exe %s"),p);


string和string1都是作为缓冲提供的,你的那种写法只不过是将指针赋值为0了,内存却一点都没分配,
后面使用wsprintf将字符串格式化到string时,没有可用内存能不出错?。

LPWSTR string[256] ; //定义为局部数组变量,函数结束自动释放
LPWSTR string1[256];

LPDWORD dwexitcode;

wsprintf(string,_T("p"));
wsprintf(string1,_T("F:\\calculator.exe %s"),p);

dudu_kang1987 2010-12-05
  • 打赏
  • 举报
回复
图片挂了。我重发



dudu_kang1987 2010-12-05
  • 打赏
  • 举报
回复
我在控制台里
cranium 2010-12-05
  • 打赏
  • 举报
回复
怎么个不对法? 能说明白些么?
dudu_kang1987 2010-12-05
  • 打赏
  • 举报
回复
wsprintf(string,_T("p"));
wsprintf(string1,_T("F:\\calculator.exe %s"),p);
wsprintf(string3,_T("F:\\calculator.exe"));
bool bsucess = CreateProcess(string3,
string1,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
改成这样, 还是不对呀
cranium 2010-12-05
  • 打赏
  • 举报
回复
看错了,把第一个参数设置为calculator的路径,第二参数,也就是你的string1设置为calculator的启动命令行
cranium 2010-12-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dudu_kang1987 的回复:]
我改了 还是不行;

[/Quote]

不行,指的是程序出错还是结果不是你期望的?

还有发现你的CreateProcess,第二个指代命令行的参数没有设置哇。

bool bsucess = CreateProcess(NULL,
string1,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);

把第二个参数NULL,更改成calculator程序运行的命令行参数。

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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