home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / os / mswindo / programm / win32 / 1893 < prev    next >
Encoding:
Text File  |  1992-11-09  |  3.0 KB  |  79 lines

  1. Newsgroups: comp.os.ms-windows.programmer.win32
  2. Path: sparky!uunet!utcsri!torn!maccs!beame
  3. From: beame@maccs.dcss.mcmaster.ca (Carl Beame)
  4. Subject: Re: DuplicateHandle -- passing handles to other processes
  5. Message-ID: <1992Nov10.060432.4835@maccs.dcss.mcmaster.ca>
  6. Organization: McMaster University, Hamilton, Ontario, Canada.
  7. References: <1992Nov9.163158.328@maccs.dcss.mcmaster.ca> <BxGL9D.Ks9@unx.sas.com>
  8. Date: Tue, 10 Nov 1992 06:04:32 GMT
  9. Lines: 68
  10.  
  11. |> Forgive me if this has already been answered ... but ...
  12. |>
  13. |> I am trying to pass a handle to a named pipe to a process which I have just
  14. |> created. After reading the docs :-), I called DuplicateHandle with
  15. |> the GetCurrentProcess() as the source process handle, the handle returned
  16. |> to me from the create process call as the destination handle and
  17. |> the pipe as the handle to duplicate. The call returned TRUE and filled in
  18. |> the new handle ... I passed this new handle to the new process via another
  19. |> named pipe ... Using the new handle in the created proceedure fails.
  20. |>
  21. |> Help ... What am I doing wrong ... (if anything :-) ).
  22. |>
  23. |> - Carl Beame
  24. |> beame@bws.com
  25. >
  26. >Post your code fragment.  I've coded this but not tested it yet.
  27. >-- 
  28. >Dave Kolb                     Opinions are mine not SAS'
  29.  
  30. Well here is the code fragment. This code tries to pass the handle 
  31. "Remote" to the created process. The interprocess communication does work and
  32. the duplicated handle value is correctly passed to the new process, but when
  33. the handle is used in the new process it fails:
  34.  
  35. {
  36.     STARTUPINFO  sui;
  37.     PROCESS_INFORMATION pi;
  38.     HANDLE    Local,Remote,newh;
  39.     char    my_pipe[40];
  40.     int    ret;
  41.  
  42. /* Remote is handle to pass */
  43.     Remote = CreateFile("\\\\.\\PIPE\\somepipe", GENERIC_WRITE, 0, NULL, 
  44.             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  45. /* Create unique name pipe for this process */
  46.     wsprintf(my_pipe,"\\\\.\\PIPE\\myipipe%d",Remote);
  47.     Local = CreateNamedPipe (my_pipe, 
  48.                   PIPE_ACCESS_OUTBOUND               // 1 way pipe.
  49.                   | FILE_FLAG_WRITE_THROUGH,        // Use write through.
  50.            PIPE_WAIT                         // Wait on messages.
  51.                | PIPE_TYPE_BYTE,
  52.                PIPE_UNLIMITED_INSTANCES,         // Unlimited instance limit.
  53.                0,                                // Buffer sizes.
  54.                0,
  55.                5000,                         // Specify time out.
  56.                NULL);                            // No securities specified.
  57.  
  58.  
  59.     memset(&sui,0,sizeof(sui));
  60.     sui.cb=sizeof(sui);
  61.     sui.wShowWindow=SW_SHOWMINNOACTIVE;
  62.  
  63. /* command_line contains the program to run and the string my_pipe to get
  64.    the passed handle from. */
  65.  
  66.     CreateProcess(NULL,command_line,NULL,NULL,TRUE,DETACHED_PROCESS,
  67.             NULL,NULL,&sui,&pi));
  68.     ConnectNamedPipe(Local, NULL);
  69.     DuplicateHandle(GetCurrentProcess(),Remote,pi.hProcess,&newh,
  70.         GENERIC_READ,TRUE,DUPLICATE_SAME_ACCESS);
  71.     WriteFile(Local,&newh,sizeof(HANDLE),&ret,NULL);
  72.     CloseHandle(pi.hProcess);
  73.     CloseHandle(pi.hThread);
  74.     CloseHandle(Local);
  75.     DisconnectNamedPipe(Local);
  76. }
  77.  
  78. - Carl Beame
  79.