0
votes

I'm confused. I have program which I run as a non-administrator user. This program can write files in my C:\Program Files\ folder.

However, if I launch a second program using CreateProcess from within the first program, the second program cannot write to the C:\Program Files\ folder.

What are the correct parameters to pass into CreateProcess() to use the same access privileges as the first launching program? I've tried setting the 3rd and 4th parameters as NULL but that didn't seem to work.

BOOL RunCmd( char  *pCmd, 
             char  *pParams, 
             char  *pWorkingDir, 
             int    nWaitSecs, 
             BOOL   fQuietMode, 
             DWORD *pdwExitCode )
{
  BOOL                fSuccess = TRUE;
  STARTUPINFO         si;
  PROCESS_INFORMATION pi;

  ZeroMemory( &si, sizeof(si) );
  ZeroMemory( &pi, sizeof(pi) );

  si.cb          = sizeof( si );
  si.dwFlags     = STARTF_USESHOWWINDOW;
  si.wShowWindow = ( fQuietMode ) ? SW_HIDE : SW_SHOW;

  // PDS: This is the important stuff - file handle needs to be inheritable..
  SECURITY_ATTRIBUTES sFileSecurity;
  ZeroMemory( &sFileSecurity, sizeof( sFileSecurity ) );
  sFileSecurity.nLength        = sizeof( sFileSecurity );
  sFileSecurity.bInheritHandle = TRUE;

  char txCmdLine[ MAX_PATH * 2 ];

  strcpy( txCmdLine, "\"" );
  strcat( txCmdLine, pCmd );
  strcat( txCmdLine, "\"" );

  if( pParams )
  {
    // PDS: Add any parameters if we have them..
    strcat( txCmdLine, " " );
    strcat( txCmdLine, pParams );
  }

  int rc;

  // Start the child process. 

      rc = CreateProcess( NULL, // No module name (use command line). 
                          txCmdLine, // Command line. 
                          &sFileSecurity,             // Process handle not inheritable. 
                          &sFileSecurity,             // Thread handle not inheritable. 

                          FALSE,

                          // PDS: Don't pop up window for application.. quiet mode!
                          CREATE_NO_WINDOW,
                          NULL,             // Use parent's environment block. 
                          pWorkingDir,      // Working folder
                          &si,              // Pointer to STARTUPINFO structure.
                          &pi );            // Pointer to PROCESS_INFORMATION structure.
2

2 Answers

1
votes

Is the application that you are launching 64 bit? For 32 processes Windows will re-direct writes to Program Files to %localappdata%\VirtualStore so they succeed but it won't do the same for 64 bit processes.

0
votes

Apologies - the primary program couldn't create the file either. The issue was confused by the fact that the primary program was being launched from an installation program that was run as an administrator. The system then rebooted and the primary program was auto-started with the current user's access privilege.. who didn't have access to the folder.