The following program, compiled as a.exe and invoked as "a.exe parent", prints "bad." How do I make it print "good?"
Edit: GetLastError returns 2
/* Inter-process Communication */
#include <windows.h>
#include <assert.h>
#include <stdio.h>
static HANDLE semaphore;
static STARTUPINFO StartupInfo;
static PROCESS_INFORMATION ProcessInfo;
static char *Args = "a.exe child";
int createChildProcess()
{
memset(&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
if (!CreateProcess( NULL, Args, NULL, NULL, FALSE,
0,
NULL,
NULL,
&StartupInfo,
&ProcessInfo))
{
return 0;
}
return 1;
}
int main(int argc, char * argv[])
{
if(!strcmp(argv[1], "child")) {
semaphore = OpenSemaphore(SYNCHRONIZE|SEMAPHORE_MODIFY_STATE,
FALSE, "Global\\EZShare");
if(semaphore==NULL) {
printf("bad\n");
}
else {
printf("good\n");
}
}
else {
semaphore = CreateSemaphore(NULL, 1, 1, "Global\\EZShare");
assert(semaphore!=NULL);
assert(createChildProcess());
}
}