Click here to Skip to main content
16,017,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Is there a way to make a call back to the original CMD.EXE that spawned my program?

Basically, I'm trying to write a console app, which does a heap of work to return a value, and along the way, it may change directory and/or drive. What I would like, is when it exits, to leave the prompt in the same drive:/directory in which the program found itself last.

As expected, the program basically gets its own environment when it starts, and when control is handed back to CMD.EXE, the prompt is in the same folder as whence it started. This to me, is actually completely expected behavior. What I'm looking for, is a way to override this.

I have thought of somehow trying to wrap this up in a batch file, however the two issues are firstly, the program has to also return a second result, and secondly, although I'm already breaking the logic up over binary code and a batch file, I want as much as possible in the binary code.

Perhaps think of this as a fancy CD/CHDIR command.

If anyone can point me in the right direction, I would be most appreciative.

Currently, I have code written in C++, but at the end of the day, I'm happy to re-write in C, C#, Rust, or Go at a minimum.

Thanks!

Jason.

What I have tried:

C++ and a couple of different libraries, C$
Posted
Updated 7-Sep-20 23:49pm

1 solution

When your app starts, get the current working directory, and before the app terminates, set the working directory back to that saved path.
 
Share this answer
 
Comments
ozisej 10-Sep-20 3:46am    
Thanks for your time, but the issue is the other way around... My code moves through directories as it goes about its thing, but when it exits, cmd.exe is still in the same directory in which the program started, not the directory in which the program last found itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900