0

I'd like to create a C++ program that constantly checks the executables location and moves it to a defined location if it is not there.

This works for a one time run.

However my issue is that, when I run the executable and place the GetModuleFileName on a loop, and move the executable to a different location, the GetModuleFileName does not return the new location, of which I want it to return it, I am very new to WinAPI and barely understand the basics, so please do not start bashing me or whatever...

NULL
  • 31
  • 1
  • 1
  • 6
  • I'm not sure this is possible. You can check whether your executable file is located in desired dir, but move it back... i'm not a guru, but i think user-mode features forbid such actions. – Petro Korienev Nov 24 '13 at 15:20
  • If you move running executable, you're not moving the current executable - from your process's perspective, you're just moving some file to some location (and I don't know if that is possible, as Petro noted, - can you move file that is currently in the memory?). – Nemanja Boric Nov 24 '13 at 15:22
  • The executable is loaded into memory. It only "remembers" where it was run from. – Yochai Timmer Nov 24 '13 at 15:22
  • What problem are you trying to solve? – David Heffernan Nov 24 '13 at 15:23
  • It would be possible, if the program could determine when and WHERE the executable file is moved. The fact of moving is easy-detectable. Also as @DavidHeffernan noted consider giving more details in question. Also, i have strange feeling that you're beginner malwarer.. – Petro Korienev Nov 24 '13 at 15:24
  • @Yochai This is incorrect. The executable is not **loaded** into memory, it is **mapped**. And the file backing is locked for as long as the file is mapped into any running process. – IInspectable Nov 24 '13 at 16:08
  • Default access rights and Windows filesystem virtualization will probably prevent you from achieving what you want anyway, even if you find a workaround to moving an openend executable (auch as moving it on exit with a helper process). Unless your program **must** be in a location like `%windir%/system32` or `%programs%`, it doesn't really matter if the user moves it elsewhere (and yes, this smells malware). On the other hand, if this does matter, it won't work, because Windows won't let you anyway. – Damon Nov 24 '13 at 16:09
  • Alright thanks, didn't know this is so complicated, guess I won't be able to do this for now.. – NULL Nov 25 '13 at 16:54

2 Answers2

1

GetModuleFileName returns the location of the module that was loaded. The value that is returned remains constant during the lifetime of the module.

What you are asking is a non-sequiter. Files and modules are different. You load a module and then you have a module. If you copy that file elsewhere, that is independent from the module. What's more, when a module is loaded, the file is locked. So you cannot delete the file from which a running module was loaded.

Frankly, since you admit to being new to Win32, I think you should reconsider the solution you have chosen for whatever the real problem is. It is unlikely that your chosen course really is the right solution.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

When your program loads, call GetModuleFileName() to determine the location of the file that was used to start the calling process. If it is not where it needs to be, move the file, then call CreateProcess() to execute the moved file as a new process, and then exit the current process.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770