使用ShellExecute打开程序后无法运行

时间:2019-02-16 08:52:54

标签: c++ fstream shellexecute

编辑: 原来问题是在iosfwd中,它在第506行引发了异常:

抛出异常:写访问冲突。 _Left为0xCCCCCCCC。

要回答: How to Fix: Exception thrown: write access violation. _Left was 0xCCCCCCCC


我正在制作一个程序,它在同一目录中启动另一个程序,该程序使用ShellExecute更新文本文件。

该程序首先使用getline接收字符串,然后使用ShellExecute启动另一个使用fstream更新文件的程序。 这是程序的相关部分:

        cout << "Please enter Program Name:" << endl;
        getline(cin.ignore(), a);
        system("cls");
        os.open("tempstring.txt");
        os << a;
        os.close();
        LPCSTR filename = "NewProgScript.exe";
        LPCSTR Location = NULL;
        ShellExecute(NULL, "open", filename, NULL, Location, SW_SHOWNORMAL);

        /*>> >> >> >> >> rest is irrelevant << << << << << <<*/

        bool end = false;
        do
        {
            system("cls");
            is.open("tempdone.txt");
            is >> x;
            is.close();
            if (x == 1) { end = true; }
            if (x == 2) { os.open("tempdone.txt"); os << 0; os.close(); Errors Err; Err.Error(41); }
            cout << "Please wait while Files Update." << endl;
            Sleep(1000);
        } while (end == false);

NewProgScript.exe”在这里启动。 NewProgScript.exe代码:

#include "pch.h"
#include <fstream>
#include <string>
#include <Windows.h>
#include <iostream>
using namespace std;

int main()
{
ofstream os;
ifstream is;
string a, nouse;
string Progs[20];
is.open("tempstring.txt");
getline(is, nouse);
is.close();
cout << "UPDATING DATABASE.. DO NOT EXIT!" << endl;
string name, ending, end;
ending = ".txt";
name = "Prog";
for (int count = 1; count < 20 + 1; count++)
{
    end = name + to_string(count) + ending;
    is.open(end);
    getline(is, Progs[count]);
    is.close();
}

for (int count = 1; count < 20 + 1; count++)
{
    end = name + to_string(count) + ending;
    if (Progs[count] == "NULL")
    {
        os.open("end");
        os << nouse;
        os.close();

        os.open("tempdone.txt");
        os << 1;
        os.close();
        Sleep(500);
        exit(EXIT_SUCCESS);
    }
}

os.open("tempdone.txt");
os << 2;
os.close();
}

问题在于,当这是使用ShellExecute的启动器时,它不会更新文件“ tempdone.txt” 但是,当我手动单击“外部打开”时,它确实起作用。 我该如何解决?

P.S:很长的帖子对不起!我也很抱歉我犯了愚蠢或天真的错误,我还不是很好:/

0 个答案:

没有答案
相关问题