#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
LPCTSTR lpName(_T("zzc"));
LPCTSTR lpValue(_T("88888"));
//设置环境变量的值
::SetEnvironmentVariable(lpName,lpValue);
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
if( !CreateProcess( _T("D:\\绘图编程\\父子进程通信-环境变量\\Child\\Debug\\Child.exe"),
NULL,
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE/*DETACHED_PROCESS*/,//新进程使用自己的可控制台窗口
NULL,
NULL,
&si,
&pi) )
{
cout <<"shibai" <<endl;
}
else{
cout << "chenggong" << endl;
}
//关闭子进程的主线程句柄,因为不在使用了
CloseHandle(pi.hThread);
//子进程终止时,子进程句柄会变为已触发状态
WaitForSingleObject(pi.hProcess,INFINITE);
//获取子进程的退出码
DWORD dwExitCode;
GetExitCodeProcess(pi.hProcess,&dwExitCode);
cout << "子进程的退出码:" << dwExitCode << endl;
//关闭子进程句柄
CloseHandle(pi.hProcess);
system("pause");
return 0;
}
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
LPCTSTR lpName(_T("zzc"));
int nlength = ::GetEnvironmentVariable(lpName,NULL,0);
LPTSTR lpValue = new TCHAR[nlength+1];
memset(lpValue,'\0',nlength+1);
::GetEnvironmentVariable(lpName,lpValue,nlength+1);
setlocale(LC_ALL, "chs");
_tprintf(lpValue);
cout <<lpValue<<endl;
system("pause");
return 10;
}