From: michaels on
I am trying to use a semaphore handle which was created in a parent
process in the child process, but API calls using the semaphore handle
return an error with GetLastError() returning 6
(ERROR_INVALID_HANDLE).

In the parent process, the semaphore is created with the following
code:

SECURITY_ATTRIBUTES stSecurityAttr;

stSecurityAttr.nLength = (DWORD)sizeof(stSecurityAttr);
stSecurityAttr.lpSecurityDescriptor = NULL;
stSecurityAttr.bInheritHandle = true;
hSemaphore = CreateSemaphore(&stSecurityAttr, 0, 1000000, NULL);

The child process is created like this:

memset(&stStartupInfo, 0, sizeof(STARTUPINFO));
stStartupInfo.cb = sizeof(STARTUPINFO);
stStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
stStartupInfo.wShowWindow = (WORD)SW_HIDE;
iCreateProcess(szChildAppName, NULL, NULL, NULL, true,
CREATE_NO_WINDOW, NULL, NULL, &stStartupInfo, &stProcessInfo);


In the parent process, calls to GetHandleInformation() for the
semaphore handle return TRUE and the HANDLE_FLAG_INHERIT flag. In the
child process, calls to GetHandleInformation() or ReleaseSemaphore()
using the same handle which was obtained in the parent process return
FALSE, and GetLastError() returns ERROR_INVALID_HANDLE.

I am using Windows XP SP3. Has the ability to inherit handles been
removed? Is there some new undocumented requirement to make this
work?

Thank You


LM


From: Preben Friis on
"LM" <michaels(a)diaginc.com> wrote in message
news:db9fbdb1-7b97-4758-aa35-b0740e7f2ea4(a)9g2000yqa.googlegroups.com...
>I am trying to use a semaphore handle which was created in a parent
> process in the child process, but API calls using the semaphore handle
> return an error with GetLastError() returning 6
> (ERROR_INVALID_HANDLE).
....
> In the
> child process, calls to GetHandleInformation() or ReleaseSemaphore()
> using the same handle which was obtained in the parent process return
> FALSE, and GetLastError() returns ERROR_INVALID_HANDLE.

You can not use the same handle value in both processes; Handles values are
only valid for a single process.

Use DuplicateHandle() to get a handle-value that you can use in your child
process.

Much more information here: http://book.51cto.com/art/200808/87778.htm

/Preben Friis