0

I am trying to copy a file from C:\Windows\System32 folder to C:\Windows\SysWOW64 folder using Fortran and/or C++ code(s).

Fortran code:

call system ('copy C:\Windows\System32\filename.extension C:\Windows\SysWOW64\filename.extension')
end

C++ code:

#include <iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main() {
   system('copy C:\Windows\System32\filename.extension C:\Windows\SysWOW64\filename.extension');
   return 0;
}

Execution of the codes above returns an error as follows:

The system cannot find the file specified.

When I enter

copy C:\Windows\System32\filename.extension C:\Windows\SysWOW64\filename.extension

on the Command Prompt in Administrator mode, it works fine and returns

1 file(s) copied.

Any idea how can I copy a file from C:\Windows\System32 folder to C:\Windows\SysWOW64 folder using Fortran and/or C++ programming languages?

Thank you very much in advance for your time and help in this matter,

Bakbergen

  • 2
    Your C++ coded program is most likely compiled as x86 application which results in starting 32-bit version of `cmd.exe` in directory `%SystemRoot%\SysWOW64` and the file to copy exists only as 64-bit version in `%SystemRoot%\System32` on 64-bit Windows. See the Microsoft documentation about [File System Redirector](https://learn.microsoft.com/en-us/windows/win32/winprog64/file-system-redirector). There should be never copied a 64-bit DLL/EXE from 64-bit system directory to the 32-bit system directory on 64-bit Windows. – Mofi Jun 07 '22 at 06:47
  • 1
    How are tags [tag:cacls] and [tag:controlled-folder-access] relevant to this question? – Evg Jun 07 '22 at 06:49
  • 32-bit executables should use `%SystemRoot%\Sysnative` on 64-bit Windows to reference a file existing only as 64-bit version in the directory `%SystemRoot%\System32` or a subdirectory of it or even better make use of [Wow64DisableWow64FsRedirection,](https://learn.microsoft.com/en-us/windows/win32/api/wow64apiset/nf-wow64apiset-wow64disablewow64fsredirection) and [Wow64EnableWow64FsRedirection](https://learn.microsoft.com/en-us/windows/win32/api/wow64apiset/nf-wow64apiset-wow64enablewow64fsredirection.md) as described by Microsoft. – Mofi Jun 07 '22 at 06:50
  • 1
    You can also consider actually using valid C++ code. That isn't a valid string in C++, and that isn't proper directory separation either (backslashes need be doubled up for proper escaping). Not that *any* of this is actually a good idea. – WhozCraig Jun 07 '22 at 06:53
  • By the way: `#include` is not good C code (missing space). The C++ directive would be `#include `, see [cstdlib](https://www.cplusplus.com/reference/cstdlib/). The C header file `string.h` should be never included in a C++ source code file because of C++ has the [std::string class](https://www.cplusplus.com/reference/string/string/) . – Mofi Jun 07 '22 at 06:54
  • What is the non-standard `system` Fortran subroutine you are trying to use? – francescalus Jun 07 '22 at 21:00

1 Answers1

2

On Windows, there's an API called CopyFile. Example in C++:

 std::wstring source = L"C:\\Windows\\System32\\filename.extension";
 std::wstring dest = L"C:\\Windows\\SysWOW64\\filename.extension";
 BOOL result = CopyFileW(source.c_str(), dest.c_str(), TRUE);
 DWORD dwLastError = result : 0 : GetLastError();

The above code will work just fine when compiled as a 64-bit executable and run on 64-bit Windows with administrator priveleges. However be advised:

  • The SysWow64 folder doesn't exist on 32-bit Windows.

  • On 64-bit Windows, if your code is compiled as 32-bit, it won't see the SysWow64 folder. That's because it's already been mapped as the System32 folder. You should read up on the File System Redirector here

  • Needs admin privs to run. App compatibility in Windows might redirect the file copy operation to a private per-app or per-user folder anyway.

  • Don't hardcode these paths. Use APIs such as GetSystemWow64Directory and GetSystemDirectory

  • You really shouldn't be able to mucking with files in the Windows System folders anyway. This is reserved for the Operating System. No one should be putting stuff here - as that creates application compatibility and versioning issues. I know it's an easy way to get EXEs and DLLs in "the path" so they load easier, but it's completely the wrong way to do it.

selbie
  • 100,020
  • 15
  • 103
  • 173