Contents
English (United Kingdom)French (Fr)Deutsch (DE-CH-AT)
Search
Login
Navigation
Home Source Code C/C++ WaitMutCopy.c
Most Recent
Featured Articles
Joomla 1.5 Featured Articles
Navigation
Home Source Code C/C++ WaitMutCopy.c
Who Is Online?
We have 15 guests online
English (United Kingdom)French (Fr)Deutsch (DE-CH-AT)
WaitMutCopy.c E-mail
User Rating: / 0
PoorBest 
Source Code - C/C++
Written by Thomas   
Monday, 02 February 2009 17:08

 

This is the source code for the command line utility WaitMutCopy.

The program first waits for a mutex and then copies a file.

 

Open WaitMutCopy.c
 

WaitMutCopy.c:

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "version.h"
 
int main (int argc, char *argv[], char *envp[])
{
  HANDLE        hMutex;
  DWORD        dwErr;
  int          iRet    = 0;
  int          i      = 1;
  int          bAbandoned  = FALSE;
  int          bExecute  = FALSE;
  STARTUPINFO      si;
  PROCESS_INFORMATION  pi;
 
  puts (PROGRAM_NAME " - " PROGRAM_DESCR ".");
  puts ("Version " VERSION_STRING);
  puts ("");
  if (argc < 4)
  {
    puts ("Invocation:");
    puts ("WAITMUTCOPY mutexname [sourcef_n destf_n...] sourcefile destfile [/S] [/A] [/E]\n");
    puts ("/S waits until the mutex is signalled (default).");
    puts ("/A waits until the mutex doesn't exist anymore.");
    puts ("/E starts destfile after the copy process.");
    puts ("All other arguments are ignored.\n");
    puts ("Description:");
    puts ("Waits for the mutex mutexname, then copies the first source file over the first");
    puts ("destination file, etc. The last copy operation occurs from sourcefile to");
    puts ("destfile. /E starts destfile if it is an executable file.\n");
    puts ("Exit code 0: Success.");
    puts ("Exit code 1: Error during a copy operation.");
    puts ("Exit code 2: Error executing destfile.\n");
    puts ("This software is freeware. Go to http://www.dateiliste.com to get\nits source code.");
    return 1;
  }
  do
  {
    if (!strcmp (argv [i], "/A"))
      bAbandoned = TRUE;
    if (!strcmp (argv [i], "/E"))
      bExecute = TRUE;
    i ++;
  } while (i < argc);
  if (bAbandoned)
  {
    while (hMutex = OpenMutex (SYNCHRONIZE, FALSE, argv [1]))
    {
      if (bAbandoned)
        {FNAMEL}.html">printf ("Mutex '%s' found. Waiting...\n", argv [1]);
      bAbandoned = 0;
      Sleep (300);
      CloseHandle (hMutex);
    }
  } else
    hMutex = OpenMutex (SYNCHRONIZE, FALSE, argv [1]);
  if (NULL != hMutex)
  {
    {FNAMEL}.html">printf ("Mutex '%s' found.\n", argv [1]);
    {FNAMEL}.html">printf ("Waiting for mutex '%s'...", argv [1]);
    dwErr = WaitForSingleObject (hMutex, INFINITE);
    switch (dwErr)
    {
    case WAIT_ABANDONED: puts (" Abandoned.");
      break;
    case WAIT_OBJECT_0: puts (" Signalled.");
      break;
    case WAIT_TIMEOUT: puts (" Timeout.");
    }
    ReleaseMutex (hMutex);
    CloseHandle (hMutex);
  } else
    {FNAMEL}.html">printf ("Mutex '%s' doesn't exist.\n", argv [1]);
  i = 2;
  while (i < argc - 2)
  {
    if (!CopyFile (argv [i], argv [i + 1], FALSE))
    {
      {FNAMEL}.html">printf ("Error copying '%s' to '%s'.\n", argv [i], argv [i + 1]);
      iRet = 1;
    } else
      {FNAMEL}.html">printf ("File '%s' successfully copied to '%s'.\n", argv [i], argv [i + 1]);
    i += 2;
  }
  if (bExecute)
  {
    {FNAMEL}.html">printf ("Starting '%s'... ", argv [i - 1]);
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    if  (!CreateProcess (argv [i - 1],  // Module name.
        NULL,          // 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
      )
    {
      {FNAMEL}.html">printf ("CreateProcess failed (error %d) for '%s'.\n", GetLastError(), argv [i - 1]);
      iRet = 2;
    } else
      puts ("Ok.");
  }
  return iRet;
}
 
Open WaitMutCopy.c

 

 

Last Updated on Friday, 26 February 2010 16:32
 
You need to login or register to post comments.
Discuss this item on the forums. (0 posts)

No Comments.