C++ 在一个程序中调用exe

生成exe的代码

#include 
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
    std::cout << "Hello World!\n";
    std::ifstream in("D:\\wth\\callexe\\Release\\123.txt", ios::binary);
	if (!in)
	{
		cout << "open error:"<<in.erase_event;
	}
	if (argc != 2)
	{
		cout << "input error";
		return 0;
	}
	cout << argc << endl;
	string des = argv[1];
	
	
	ofstream out(des.c_str(), ios::binary | ios_base::trunc);
	/*in.seekg(0, std::ios::end);
	int fileLength = in.tellg();
	in.seekg(0, std::ios::beg);*/
	string str;
	//string all;
	//int index = 0;
	//char *buff = new char[fileLength];
	//string tmp; tmp.append('\0');
	while (!in.eof())
	{
		getline(in, str);
		cout << str << endl;
		out << str;
		//str.clear();
	}

}

调用exe的代码

#include 

#include
#include

#include 
#include 
using namespace std;
int main()
{
    std::cout << "Hello World!\n";
    STARTUPINFO si;
    memset(&si, 0, sizeof(STARTUPINFO));//初始化si在内存块中的值(详见memset函数)
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOW;
    PROCESS_INFORMATION pi;//必备参数设置结束

   // TCHAR sCmd[] = _T("c:\\windows\\system32\\notepad.exe 1593.txt");
    TCHAR sCmd[] = _T("D:\\wth\\callexe\\Release\\readfile.exe");
    TCHAR s[] = _T(" 1539.txt"); //注意添加一个空格
    //CreateProcess(NULL, sCmds,NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)
    if (!CreateProcess(sCmd, s,NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
        cout << "CreateFail!" << endl;
        exit(1);
    }
    else {
        cout << "Success!" << endl;
    }
    //关闭不使用的句柄
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
    return 0;
}

你可能感兴趣的:(C++)