0

EDIT : Turns out that the problem is that in iosfwd, it throws the exception at line 506 :

Exception thrown: write access violation. _Left was 0xCCCCCCCC.

To answer :How to Fix: Exception thrown: write access violation. _Left was 0xCCCCCCCC


I am making a program where it launches another program in its same directory that updates a text file using ShellExecute.

The program begins by taking a string using getline and then uses ShellExecute to launch another program that uses fstream to update a file. Here is the relevant part of the program:

        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" is launched here. NewProgScript.exe code:

#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();
}

The problem is that when this is launcher early on using ShellExecute, it does not update the file "tempdone.txt" It does however work when I manually click open it externaly. How can i fix this?

P.S: sorry for the Very long post! Also I am sorry of i make dumb or naive mistakes, i am not very good yet :/

  • 2
    Have you tried checking the result of `ShellExecute`? – Alan Birtles Feb 16 '19 at 09:01
  • Why `getline(cin.ignore(), a);`? Do you want to ignore the first character input? – john Feb 16 '19 at 09:03
  • Does it do the other actions? Do you see the output `"UPDATING DATABASE.. DO NOT EXIT!"`, does the file `"end"` get updated? The most likely explanation is that `ShellExecute` is failing and your second program is not running at all. – john Feb 16 '19 at 09:06
  • 1
    Most of the good questions and answers on SO you undoubtedly must have read are not indented like you've done it - and they are also mostly [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve). You'll attract more interest in helping out by making it easy to help out. – Ted Lyngmo Feb 16 '19 at 09:14
  • Without the [mcve], which you have to extract from your code first, your question is actually considered off-topic. Please also take the [tour] and read [ask]. – Ulrich Eckhardt Feb 16 '19 at 09:39
  • @UlrichEckhardt sorry but i couldn't find a shorter way to say what i wanted – Phoned_Leek25 Feb 16 '19 at 11:36
  • @john i tested it in SW_SHOWNORMAL and it works fine, just doesn't update the file... Also the cin.ignore is because if i dont include it the getline skips the input and doesn't wait – Phoned_Leek25 Feb 16 '19 at 11:37
  • Hard to be sure what's going on because no MCVE, but a couple of points. 1) you are using the wrong function [CreateProcess](https://stackoverflow.com/questions/10747479/createprocess-and-shellexecute-differences) seems right for what you are doing. 2) Undoubtedly the problems you are having are because both programs are trying to access the same file. The first program is trying to read from the file and the second is trying to write to it. The reliable way to handle this kind of issue is to make the first program wait until the second process has completed for that use `WaitForSingleObject` – john Feb 16 '19 at 12:36
  • I should add, that what you are trying to do is called Inter Process Communication (IPC for short). There are better ways than using temporary files to achieve what you are trying to do (it seems you just want the exit code from your launched program). So maybe you should take a break from this program and learn how to do IPC on Windows. Then when you have the knowledge you can go back to this program. – john Feb 16 '19 at 12:40
  • Considering the MCVE, ask yourself a few questions: Is the manual input necessary or can you hardcode the values? Does the executed program have to do anything ar is an empty program enough? The MCVE only has to demonstrate the problem not serve any practical purpose that you were after initially! In any case, you didn't consider the "C" part of MCVE. – Ulrich Eckhardt Feb 16 '19 at 13:39
  • Sorry @UlrichEckhardt – Phoned_Leek25 Feb 17 '19 at 16:23
  • @john thank you for this, i will try the things you said and get back to you, hopefully in the next 2 days. – Phoned_Leek25 Feb 17 '19 at 16:24
  • @john perhaps you could help my out in another question? https://stackoverflow.com/q/54737223/8239084 – Phoned_Leek25 Feb 17 '19 at 20:11
  • @Phoned_Leek25 Sure see my answer over there, that question shouldn't have been closed as it was. – john Feb 17 '19 at 20:23

0 Answers0