how can i change the working directory?

Apr 19, 2019 at 4:57pm
heres my run function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void run(string FileName, string Parameters="", string ActualFolder="")
{
    SHELLEXECUTEINFO ShExecInfo;
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = "open";
    ShExecInfo.lpFile = FileName.c_str();
    ShExecInfo.lpParameters = Parameters.c_str();
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;
    if(ActualFolder!="")
    {
        if(SetCurrentDirectory(ActualFolder.c_str())==false)
            write("error");
    }

    ShellExecuteEx(&ShExecInfo);

    if(ShExecInfo.hProcess)
    CloseHandle(ShExecInfo.hProcess);
}

the IDE creates the exe:
- if i run on IDE, i will get a GNU compiler exe;
- if i run on folder, i will not get the problem...
so i belive the problem is on Current Working Directory... so what you can advice me?
Apr 19, 2019 at 5:09pm
It's not a compiler error if the EXE is being made.

What is the error?

What values are you giving FileName, Parameters, and ActualFolder?

Your program seems to correctly run when I do:
run("test.txt", "", "C:\\test");
And I have a file C:\test\test.txt
Last edited on Apr 19, 2019 at 5:20pm
Apr 19, 2019 at 5:24pm
like i said:
- if i run the exe, on folder, everything works normal;
- if i run the exe, on Code Blocks, the run() don't works fine, because of:
string FileName1="C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin\\x86_64-w64-mingw32-g++.exe";
Current Directory... i belive, the problem here, is the Current Directory
(i can't give you the error message, because it's too fast.... if i use cmd, i will not get the problem)
Last edited on Apr 19, 2019 at 5:26pm
Apr 19, 2019 at 5:30pm
GetLastError() should always be checked after a Win32 function fails if you want to know exactly why a call failed.

Can you post the code where you call run?

Edit: I see what you're saying now. That it's g++ itself that's displaying an error for a split second before it closes.

If the current directory is the problem, make sure you're in the directory that you think you are by printing cd to the screen.

Sloppy, easy way to do it:
1
2
3
4
5
void printCurrentDirectory()
{
    std::cout << "cd: ";
    system("cd");
}
Last edited on Apr 19, 2019 at 5:33pm
Apr 19, 2019 at 5:34pm
1
2
3
4
5
f(Compile.ErrorList.size()==0)
    {
        cout << "\ncreating an exe....\n";
        string FileName1="C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin\\x86_64-w64-mingw32-g++.exe";
        run(FileName1,"-Wall -g -std=c++14  -c \"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.cpp\" -o \"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.o\"","C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin");

on run():
1
2
3
4
5
6
if(ActualFolder!="")
    {
        if(SetCurrentDirectory(ActualFolder.c_str())==false)
            write("error", GetLastError());
    }
    write("error message: ", GetLastError());

i get zero.
or i'm adding the wrong folder path on SetCurrentDirectory()
Apr 19, 2019 at 5:35pm
like i said the error only happens, if i run using the Code Blocks... but not on folder
Apr 19, 2019 at 5:50pm
I'm not sure.

I changed the program to be my own environment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{

    cout << "\ncreating an exe....\n";
    
    string compilerPath = "C:\\MinGW\\bin\\";  //"C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin\\";
    string compiler_exe = "g++.exe";  //"x86_64-w64-mingw32-g++.exe";

    string FileName1 = compilerPath + compiler_exe;
    string parameters = "-Wall -g -std=c++14  -c \"C:\\dev\\test\\test.cpp\" -o \"C:\\dev\\test\\test.o\"";
    string actualFolder = compilerPath;;
    
    run(FileName1, parameters, actualFolder);
}


And it runs fine, and an object file is created. I'm not sure, sorry.

I would say try starting simpler, and execute a simple program that prints its arguments and then pauses, so that the program doesn't disappear as soon as it loads.

Apr 19, 2019 at 6:37pm
yes i tested on just that code and works... maybe the run() is done before the *.cpp file been created... i must see my way of testing if the file is created...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
inline bool IfFileExist (const std::string& name)
{
    std::ifstream f(name.c_str());
    return f.good();
}

void DoEvents()
{
    MSG msg;
    BOOL result;

    while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
    {
        result = ::GetMessage(&msg, NULL, 0, 0);
        if (result == 0) // WM_QUIT
        {
            ::PostQuitMessage(msg.wParam);
            break;
        }
        else if (result == -1)
        {
             // Handle errors/exit application, etc.
        }
        else
        {
            ::TranslateMessage(&msg);
            :: DispatchMessage(&msg);
        }
    }
}

//....
cout << "\ncreating an exe....\n";
        string compilerPath = "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin\\";
        string compiler_exe = "x86_64-w64-mingw32-g++.exe";

        string FileName1 = compilerPath + compiler_exe;
        string parameters = "-Wall -g -std=c++14  -c \"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.cpp\" -o \"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.o\"";
        string actualFolder = compilerPath;
        while( IfFileExist("C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.cpp")==false)
        {
            DoEvents();
        }
        run(FileName1, parameters, actualFolder);

maybe the while() and the IfFileExist() is give me a problem :(
Apr 19, 2019 at 7:08pm
after several tests i found the big problem:
Compile.compile(strCompile,"test.cpp");
like you see, i don't use the folder name here... but after i fix it:
Compile.compile(strCompile,"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.cpp");
the program works fine... thank you so much for all
Topic archived. No new replies allowed.