0

A method to auto-update the directory of a file using C++ ?

I have a program which aims to first input the password from the user, and once the password matches, the program will open a file using the ShellExecute() function.
This file is a C++ executable file in .exe format.

The program needs to automatically update the directory in the ShellExecute() function instead of having the programmer or other users manually change it in the code each time the executable file's location is changed. What is the best approach to do so ? I have gone through some of these links, but to no avail :

[1] Finding the last created FILE in the directory, C++
[2] How do I make a file self-update (Native C++)
[3] https://www.daniweb.com/programming/software-development/threads/242600/finding-the-most-recently-created-file-in-a-folder


Please feel free to browse my code provided below :



#include<iostream>
#include<conio.h>
#include<string.h>
#include<unistd.h>
#include<windows.h>
using namespace std;
int main(void)
{
    string pw="";
    char ch;
    int attempts=3;
    
    Initiation:
    system("cls");
    
    cout<<"Password ?\n--> ";
    ch=_getch();
    
    
    while(ch!=13)
    {
        pw.push_back(ch);
        cout<<'*';
        ch=_getch();
    }
    
    
    
    if(pw=="a32bx#$123")
    {
        ShellExecute(NULL, "open", "C:\\Users\\agm\\Documents\\easypeasy.exe", NULL, NULL, SW_SHOWDEFAULT);
    }
    else if(pw!="a32bx#$123")
    {
        attempts--;
        if(attempts>0)
        {
            cout<<"\n\nIncorrect password. You now have "<<attempts<<" attempts remaining. Loading back the main screen...";
            sleep(3);   //This gives time for the user to read the line before it moves to the label named Initiation.
            goto Initiation;    
        }
        if(attempts==0)
            cout<<"\n\nAborting now !";
            
    }
        
    
    
    
}   //END OF CODE



Note : I'm also open to any suggestions on how to improve the overall code.

Community
  • 1
  • 1
  • It is not clear what you are asking for. My best guess is "I want the name of the most recently-created .exe file in the folder in which my helper .exe resides". If so, which part are you having trouble with? – Botje Dec 23 '19 at 11:01
  • @Botje I want to know of a way by which I can make the file's path in the ShellExecute function update by itself during runtime, so that there wouldn't be a need to change the file's path each time the file is moved to a different folder. So that is the part I cannot figure out how to code... – Agneevo Mukherjee Dec 23 '19 at 11:06
  • 1
    Simply restating the requirement in the same words does not help me. Is the "automatically updating file path" equal to "the path in which my helper .exe resides"? If not, how is it determined? Also, *when* is it determined? You make it sounds like the helper should sense when it is moved around even as it is running. – Botje Dec 23 '19 at 11:08
  • On the overall code, let's ignore the fact that you are using a hard-coded password which anybody disassembling your code can get to really easily - maybe you intend to replace it with a proper hashed password later. But it is really easy to rewrite the same logic without using that `goto`, so I would highly recommend doing that for a start. – th33lf Dec 23 '19 at 11:16
  • @Botje Yes, it is the same path in which the helper .exe resides. The helper, or if possible, another parameter could be used to sense where the file is located each time the program is run. – Agneevo Mukherjee Dec 23 '19 at 11:19
  • 1
    @th33lf Of course I don't plan to use that sort of password protection ! I wrote that just to help illustrate what the code will be doing in general. Ok, thanks for your input, I will change the `goto` part afterwards. – Agneevo Mukherjee Dec 23 '19 at 11:22
  • Does this answer your question? [SAFELY get path to running executable in windows API](https://stackoverflow.com/questions/4841546/safely-get-path-to-running-executable-in-windows-api) – Botje Dec 23 '19 at 11:49
  • @Botje Ok, this link seems helpful. I'll just have to make some tweaks on the overall code, and should get a functional code. Thanks ! – Agneevo Mukherjee Dec 24 '19 at 07:59

0 Answers0