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

  1. Newsgroups: comp.os.ms-windows.programmer.win32
  2. Path: sparky!uunet!decwrl!concert!sas!mozart.unx.sas.com!sasdxk
  3. From: sasdxk@skyhawk.unx.sas.com (Dave Kolb)
  4. Subject: Re: DuplicateHandle -- passing handles to other processes
  5. Originator: sasdxk@skyhawk.unx.sas.com
  6. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  7. Message-ID: <BxI8o0.6K@unx.sas.com>
  8. Date: Tue, 10 Nov 1992 14:38:24 GMT
  9. References: <1992Nov9.163158.328@maccs.dcss.mcmaster.ca> <BxGL9D.Ks9@unx.sas.com> <1992Nov10.060432.4835@maccs.dcss.mcmaster.ca>
  10. Nntp-Posting-Host: skyhawk.unx.sas.com
  11. Organization: SAS Institute Inc.
  12. Lines: 93
  13.  
  14.  
  15. In article <1992Nov10.060432.4835@maccs.dcss.mcmaster.ca>, beame@maccs.dcss.mcmaster.ca (Carl Beame) writes:
  16. |> |> Forgive me if this has already been answered ... but ...
  17. |> |>
  18. |> |> I am trying to pass a handle to a named pipe to a process which I have just
  19. |> |> created. After reading the docs :-), I called DuplicateHandle with
  20. |> |> the GetCurrentProcess() as the source process handle, the handle returned
  21. |> |> to me from the create process call as the destination handle and
  22. |> |> the pipe as the handle to duplicate. The call returned TRUE and filled in
  23. |> |> the new handle ... I passed this new handle to the new process via another
  24. |> |> named pipe ... Using the new handle in the created proceedure fails.
  25. |> |>
  26. |> |> Help ... What am I doing wrong ... (if anything :-) ).
  27. |> |>
  28. |> |> - Carl Beame
  29. |> |> beame@bws.com
  30. |> >
  31. |> >Post your code fragment.  I've coded this but not tested it yet.
  32. |> >--
  33. |> >Dave Kolb                     Opinions are mine not SAS'
  34. |>
  35. |> Well here is the code fragment. This code tries to pass the handle
  36. |> "Remote" to the created process. The interprocess communication does work and
  37. |> the duplicated handle value is correctly passed to the new process, but when
  38. |> the handle is used in the new process it fails:
  39. |>
  40. |> {
  41. |>      STARTUPINFO  sui;
  42. |>      PROCESS_INFORMATION pi;
  43. |>      HANDLE  Local,Remote,newh;
  44. |>      char    my_pipe[40];
  45. |>      int     ret;
  46. |>
  47. |> /* Remote is handle to pass */
  48. |>      Remote = CreateFile("\\\\.\\PIPE\\somepipe", GENERIC_WRITE, 0, NULL,
  49. |>                      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  50. |> /* Create unique name pipe for this process */
  51. |>      wsprintf(my_pipe,"\\\\.\\PIPE\\myipipe%d",Remote);
  52. |>      Local = CreateNamedPipe (my_pipe,
  53. |>                     PIPE_ACCESS_OUTBOUND               // 1 way pipe.
  54. |>                     | FILE_FLAG_WRITE_THROUGH,        // Use write through.
  55. |>             PIPE_WAIT                         // Wait on messages.
  56. |>                | PIPE_TYPE_BYTE,
  57. |>                PIPE_UNLIMITED_INSTANCES,         // Unlimited instance limit.
  58. |>                0,                                // Buffer sizes.
  59. |>                0,
  60. |>                5000,                         // Specify time out.
  61. |>                NULL);                            // No securities specified.
  62. |>
  63. |>
  64. |>      memset(&sui,0,sizeof(sui));
  65. |>      sui.cb=sizeof(sui);
  66. |>      sui.wShowWindow=SW_SHOWMINNOACTIVE;
  67. |>
  68. |> /* command_line contains the program to run and the string my_pipe to get
  69. |>    the passed handle from. */
  70. |>
  71. |>      CreateProcess(NULL,command_line,NULL,NULL,TRUE,DETACHED_PROCESS,
  72. |>                      NULL,NULL,&sui,&pi));
  73. |>      ConnectNamedPipe(Local, NULL);
  74. |>      DuplicateHandle(GetCurrentProcess(),Remote,pi.hProcess,&newh,
  75. |>              GENERIC_READ,TRUE,DUPLICATE_SAME_ACCESS);
  76. |>      WriteFile(Local,&newh,sizeof(HANDLE),&ret,NULL);
  77. |>      CloseHandle(pi.hProcess);
  78. |>      CloseHandle(pi.hThread);
  79. |>      CloseHandle(Local);
  80. |>      DisconnectNamedPipe(Local);
  81. |> }
  82. |>
  83. |> - Carl Beame
  84.  
  85. My questions/suggestions are two:
  86.  
  87. - Does the pipe that CreateFile is opening already exist?  If not you
  88.   don't have a valid handle to pass.
  89.  
  90. - You need to set the inherit flag in the lpsa security attributes
  91.   data structure when creating the remote handle.
  92.  
  93. P.S. How does the created process open the local pipe if the remote# is part of
  94. the pipe name and that's what you're trying to pass it?  And why pass it if it
  95. already knows it?  Or why not just have the created process open up the remote pipe?
  96.  
  97. I guess this is a contrived fragment???
  98.  
  99. Let us know...
  100.  
  101. -- 
  102. Dave Kolb                     Opinions are mine not SAS'
  103. SAS Institute, Inc.           EMAIL:      sasdxk@unx.sas.com
  104. SAS Campus Drive - J206       Phone:      (919) 677-8000 x6827
  105. Cary, NC  27513-2414 USA      FAX:        (919) 677-8123
  106.  
  107.