home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.ms-windows.programmer.win32
- Path: sparky!uunet!utcsri!torn!maccs!beame
- From: beame@maccs.dcss.mcmaster.ca (Carl Beame)
- Subject: Re: DuplicateHandle -- passing handles to other processes
- Message-ID: <1992Nov10.060432.4835@maccs.dcss.mcmaster.ca>
- Organization: McMaster University, Hamilton, Ontario, Canada.
- References: <1992Nov9.163158.328@maccs.dcss.mcmaster.ca> <BxGL9D.Ks9@unx.sas.com>
- Date: Tue, 10 Nov 1992 06:04:32 GMT
- Lines: 68
-
- |> Forgive me if this has already been answered ... but ...
- |>
- |> I am trying to pass a handle to a named pipe to a process which I have just
- |> created. After reading the docs :-), I called DuplicateHandle with
- |> the GetCurrentProcess() as the source process handle, the handle returned
- |> to me from the create process call as the destination handle and
- |> the pipe as the handle to duplicate. The call returned TRUE and filled in
- |> the new handle ... I passed this new handle to the new process via another
- |> named pipe ... Using the new handle in the created proceedure fails.
- |>
- |> Help ... What am I doing wrong ... (if anything :-) ).
- |>
- |> - Carl Beame
- |> beame@bws.com
- >
- >Post your code fragment. I've coded this but not tested it yet.
- >--
- >Dave Kolb Opinions are mine not SAS'
-
- Well here is the code fragment. This code tries to pass the handle
- "Remote" to the created process. The interprocess communication does work and
- the duplicated handle value is correctly passed to the new process, but when
- the handle is used in the new process it fails:
-
- {
- STARTUPINFO sui;
- PROCESS_INFORMATION pi;
- HANDLE Local,Remote,newh;
- char my_pipe[40];
- int ret;
-
- /* Remote is handle to pass */
- Remote = CreateFile("\\\\.\\PIPE\\somepipe", GENERIC_WRITE, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- /* Create unique name pipe for this process */
- wsprintf(my_pipe,"\\\\.\\PIPE\\myipipe%d",Remote);
- Local = CreateNamedPipe (my_pipe,
- PIPE_ACCESS_OUTBOUND // 1 way pipe.
- | FILE_FLAG_WRITE_THROUGH, // Use write through.
- PIPE_WAIT // Wait on messages.
- | PIPE_TYPE_BYTE,
- PIPE_UNLIMITED_INSTANCES, // Unlimited instance limit.
- 0, // Buffer sizes.
- 0,
- 5000, // Specify time out.
- NULL); // No securities specified.
-
-
- memset(&sui,0,sizeof(sui));
- sui.cb=sizeof(sui);
- sui.wShowWindow=SW_SHOWMINNOACTIVE;
-
- /* command_line contains the program to run and the string my_pipe to get
- the passed handle from. */
-
- CreateProcess(NULL,command_line,NULL,NULL,TRUE,DETACHED_PROCESS,
- NULL,NULL,&sui,&pi));
- ConnectNamedPipe(Local, NULL);
- DuplicateHandle(GetCurrentProcess(),Remote,pi.hProcess,&newh,
- GENERIC_READ,TRUE,DUPLICATE_SAME_ACCESS);
- WriteFile(Local,&newh,sizeof(HANDLE),&ret,NULL);
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- CloseHandle(Local);
- DisconnectNamedPipe(Local);
- }
-
- - Carl Beame
-