Copying a file from C:\Windows\System32 folder to C:\Windows\SysWOW64 folder using Fortran and/or C++

1.1k views Asked by At

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

1

There are 1 answers

0
selbie On

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.