0

im a beginner with cpp, and wanted to ask you how to check on application start if the name is "test.exe", if it already is it will just print "yes, this is right", if it's not it will rename it to test.exe

Tried searching for it online, and i could only find a function to rename it ONLY specifying the file name. Es:

int value;
 
value = rename("fake.exe", "test.exe");

But if people renamed it to something different than fake.exe it obviously won't work.

EDIT after some answers: Guys, as i said the code works, but only if the name of the file (in this case) is fake.exe, which no one would rename it like that.

So i'm asking to get a part of code that changes the name of the app whatever the name is.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Doggho f
  • 9
  • 2
  • 1
    _"argv[0] is the pointer to the initial character of a null-terminated multibyte string that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment)."_ https://en.cppreference.com/w/cpp/language/main_function It's not exactly what you asked. People can set a symlink to your application and call it through the symlink. In that case the name of the symlink will be passed as first parameter. – Thomas Sablik Jun 20 '20 at 14:15
  • 2
    You can’t rename it because it is a running process! – Andrew Truckle Jun 20 '20 at 14:16
  • @AndrewTruckle as i said the function i use works, but the problem is that it only works if the name of the file is `fake.exe` – Doggho f Jun 20 '20 at 14:19
  • 1
    Some operating systems will not let you rename the executable for a running process. Some operating system will not let you rename the executable unless your account has privileges to do that operation. – Eljay Jun 20 '20 at 14:19
  • @ThomasSablik sorry but i don't see the point in your answer, nor i understand it – Doggho f Jun 20 '20 at 14:19
  • 2
    Your question title is completely misleading. Your question is not how to rename a file. You know how to rename a file. Your question is how to find the path and name of the executable. My first comment describes how you can get the path to the executable. – Thomas Sablik Jun 20 '20 at 14:20
  • As i said @ThomasSablik im a beginner, so i thought this was the best possible title. – Doggho f Jun 20 '20 at 14:21
  • The link in my first comment describes how to get the path to your file. You can use that path to rename the file. – Thomas Sablik Jun 20 '20 at 14:22
  • @tho Passing the executable name as the first command line argument is a *convention*, not a contract. – IInspectable Jun 20 '20 at 17:28
  • @IInspectable OP uses Windows. Do you know a Windows system with different behavior? – Thomas Sablik Jun 20 '20 at 18:32
  • @tho That convention isn't controlled by the OS. Any program is at liberty to call [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) as it sees fit. – IInspectable Jun 20 '20 at 19:41
  • @IInspectable So you have an better idea? – Thomas Sablik Jun 20 '20 at 20:03
  • @tho Like `GetModuleHandle` and `GetModuleFileName` (and friends)? – IInspectable Jun 20 '20 at 20:30
  • @IInspectable Probably you should write an answer. I don't have a Windows system and I'm not Windows developer. – Thomas Sablik Jun 20 '20 at 20:38

0 Answers0