简体   繁体   中英

Launching 64bit process from 32bit program

I have a 32bit program which I am trying to run on a 64bit computer. I'm running the vssadmin command to get the restore point info and saving it to a text file. Now, it works fine on my 32bit computer and I was testing it on my friend's 64 bit computer and it doesn't work.

If I use system() from within a 32bit application running on a 64 bit system, will it use a 32bit command prompt? That's the only thing I can think that would stop it working as I have manually run the command from a command prompt and it works fine.

If this is the case? Is there anyway to force it to use the right command prompt?

In both Linux and Windows [and I beleive BSD/MacOS too], 32-bit applications can start a 64-bit process using the relevant "create a new process" system call [which system() does some several layers down inside the shell that it starts].

The only restriction is that a 32-bit executable file can't use a 64-bit shared library (.so or .dll) or vice versa. But a new process starts by loading a new executable and at that point the process can be 32- or 64-bit based on the executable file itself.

You should always avoid using system() because

  • It is resource heavy
  • It defeats security -- you don't know if it's a valid command or does the same thing on every system( see Mats Petersson answer), you could even start up programs you didn't intend to start up. The danger is that when you directly execute a program, it gets the same privileges as your program -- meaning that if, for example, you are running as system administrator then the malicious program you just inadvertently executed is also running as system administrator. If that doesn't scare you silly, check your pulse.
  • Anti virus programs hate it, your program could get flagged as a virus.

you should use CreateProcess() .

You can use Createprocess() to just start up an .exe and creating a new process for it. The application will run independent from the calling application.

here's an example i used in one of my projects:

VOID startup(LPCTSTR lpApplicationName)
{
   // additional information
   STARTUPINFO si;     
   PROCESS_INFORMATION pi;

   // set the size of the structures
   ZeroMemory( &si, sizeof(si) );
   si.cb = sizeof(si);
   ZeroMemory( &pi, sizeof(pi) );

  // start the program up
  CreateProcess( lpApplicationName,   // the path
    argv[1],        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

NOTE: as i said earlier, system() does not do the same on different machines, Mats Petersson explained why

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM