演示通过环境变量在不同进程间传递数据
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
SetEnvironmentVariable("zzz","abc");//设置环境变量为“zzz”,值为“abc”
if(! CreateProcess("D:\\VS_project\\algorithm\\Debug\\test.exe",//test.exe只是接收变量“zzz”的值,并打印,具体代码和下面相似
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
cout<<"createprocess failed"<<endl;
}
//下面用于接收环境变量
LPSTR lp=NULL;
DWORD dw;
lp=(LPTSTR) malloc(MAX_PATH*sizeof(TCHAR));
GetEnvironmentVariableA(TEXT("LSERVRC"),lp,MAX_PATH); //演示获取系统环境变量"LSERVRC"的值
cout<<lp;
system("pause");
}