0

My code has to move itself and some assets to a known directory(C:\windows) and it does so by check its current directory, copying the files and then restarting. To debug the program i use some msgboxes which return the various variables and i noticed that after the program copies itself and restarts,in the windows directory, the variable which holds GetCurrentDirectoryA() has still the same value as before the program moved itself. because of this the program keeps trying to move itself to the windows directory even after it has moved itself in an endless loop. is there anyway to fix this?

EDIT: i just noticed how dumb i was assuming GetCurrentDirectoryA() would return the directory of the binary, thanks. :) regarding the answer that says im trying to create a malware.... the program is meant to be a portable .exe and i dont know how to share paths among the various components, so i just copy it to a directory which i consistently know the path to.(through GetWindowsDirectory())

  • Current directory is not the same as the program directory. If you want the program directory, see https://stackoverflow.com/q/143174/11683. – GSerg Jun 24 '21 at 12:56
  • 3
    "My code has to move itself ... to a known directory `C:\windows`". No, it does have to, and it should not. `C:\Windows` is where Windows itself lives. Your program goes in `C:\Program Files` or whatever the local translation is. – MSalters Jun 24 '21 at 13:11
  • actually, it should go into `%ProgramFiles%` rather than `C:\Program Files\`, and most likely a subdirectory thereof (`%ProgramFiles%\MyTool\`). and this is when you create an installer that installs all your assets to a given place, and uses the registry to store the actual installation directory which is then queried by all the components that need it. – umläute Jun 24 '21 at 13:18
  • @PaulMcKenzie "I happen to believe that not having a kernel debugger forces people to think about their problem on a different level than with a debugger... Without a debugger, you basically have to go the next step: understand what the program does. Not just that particular line." -- Linus Torvalds, 2000 – mnistic Jun 24 '21 at 13:30
  • "*i dont know how to share paths among the various components*" - use system-provided shared folders, like `%APPDATA%`, etc. See functions like `SHGetFolderPath()` and `SHGetKnownFolderPath()`. "*so i just copy it to a directory which i consistently know the path to.(through GetWindowsDirectory())*" - Don't use the Windows folder to share files. Non-system files do not belong there. There is a reason why Windows sets aside special folders for apps to use. – Remy Lebeau Jun 24 '21 at 16:11
  • You are looking for [GetModuleFileName](https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew). – IInspectable Jun 24 '21 at 17:57

3 Answers3

1

GetCurrentDirectory() gives you the working directory (where you started your program from). it has nothing to do with where the program binary resides.

sidenote, is this trying to create your first malware that copies itself to some system directory? if not, you should consider creating an installer for your program, that just puts all the assets where they are expected to be, instead of putting that logic into your program itself.

umläute
  • 28,885
  • 9
  • 68
  • 122
1

GetCurrent(working)Directory is an O/S state, not the folder of the running .exe.

You can set it to the folder of the exe, if that's what you need.

To get the folder of the .exe, use GetModuleFileName, but it will include the name of the file, not just the folder, so you have to manipulate the returned string (reverse search for the trailing backslash).

franji1
  • 3,088
  • 2
  • 23
  • 43
  • 1
    thanks alot!! i also appriciate how you didnt waste time insinuating im a criminal... xD – брат волк Jun 24 '21 at 13:08
  • @братволк> it's just that your approach is unorthodox, there are cleaner ways to achieve your intended result. On the other hand, that technique is used a lot by malware (and will even trigger some antivirus). That's why it raises some suspicion. – spectras Jun 24 '21 at 13:13
  • @spectras i read somewhere on a c++ forum that getting local variables such as %appdata% , %homepath% and such is risky since its not consistent so i went with what the WinApi offered. there's no malicious intent whatsover , im just really new to WinApi and C++ in general... – брат волк Jun 24 '21 at 13:23
  • @братволк> copying a binary to any of those places would be suspicious too. If your program is really portable, it should run from any directory, so there is no reason to copy it anywhere. And if the only purpose is to make it available to other programs, there are better ways, such as adding it to the PATH. – spectras Jun 24 '21 at 14:22
1

I am not sure about this as I am not a C++ expert.

I think the problem is in recalling mechanism. GetCurrentDirectoryA() returns the address of the applicaion which called the API. You may have moved assets and other files but restart would still result in running the same application again.

I think you can use thread or another application. For example, you create an hosting application which receives path parameter and runs the program in the path. Then after you moved the application successfully, you call the hosting application, pass the updated path and finish the process. Then, the hosting app will run the application in a new position and possible remove the original one if you want. Hope this could help you

sirius9515
  • 309
  • 1
  • 8