|
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)
printf ("Mutex '%s' found. Waiting...\n", argv [1]);
bAbandoned = 0;
Sleep (300);
CloseHandle (hMutex);
}
} else
hMutex = OpenMutex (SYNCHRONIZE, FALSE, argv [1]);
if (NULL != hMutex)
{
printf ("Mutex '%s' found.\n", argv [1]);
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
printf ("Mutex '%s' doesn't exist.\n", argv [1]);
i = 2;
while (i < argc - 2)
{
if (!CopyFile (argv [i], argv [i + 1], FALSE))
{
printf ("Error copying '%s' to '%s'.\n", argv [i], argv [i + 1]);
iRet = 1;
} else
printf ("File '%s' successfully copied to '%s'.\n", argv [i], argv [i + 1]);
i += 2;
}
if (bExecute)
{
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
)
{
printf ("CreateProcess failed (error %d) for '%s'.\n", GetLastError(), argv [i - 1]);
iRet = 2;
} else
puts ("Ok.");
}
return iRet;
}
Open WaitMutCopy.c
|