Fortran 和 C# 之间的管道

Posted

技术标签:

【中文标题】Fortran 和 C# 之间的管道【英文标题】:Pipe between Fortran and C# 【发布时间】:2021-07-26 10:35:38 【问题描述】:

我想在 Fortran 程序和 C# 程序之间建立一个管道。 Fortran 程序将完成繁重的工作,而 C# 程序将提供正在完成的工作的“视图”(使用从 Fortran 程序发送给它的数据)。为了检查这是否可行,我编写了两个小程序。 (Fortran 代码是从 C++ 和 C# 之间的管道数据示例中窃取的。)

Fortran 代码:

  PROGRAM Test

  USE kernel32
  IMPLICIT NONE

  ! Data pipe
  INTEGER*4 EqnData /100/
  ! Paths for pipes
  CHARACTER*128 dataname /'\\.\pipe\EqnData'/

  INTEGER(HANDLE) :: pipe1

  pipe1 = CreateNamedPipe('\\\\.\\pipe\\EqnData'C, PIPE_ACCESS_DUPLEX, &
    PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, &
    1024, 1024, 120 * 1000, NULL)

  PRINT*, pipe1

  ! Open data pipe
  OPEN(UNIT=EqnData, FILE=dataname, ACCESS='STREAM', STATUS='OLD')

  READ* 

  ! Close pipe
  CLOSE(EqnData)

  WRITE (*,*) 'end'

  END

C#代码:

using System;
using System.IO;
using System.IO.Pipes;

class PipeClient

  static void Main(string[] args)
  

    using (NamedPipeClientStream pipeClient
      = new NamedPipeClientStream(".",
                             "EqnData",
                             PipeDirection.InOut))
    
      // Connect to the pipe or wait until the pipe is available.
      Console.Write("Attempting to connect to pipe...");
      pipeClient.Connect();

      Console.WriteLine("Connected to pipe.");
      Console.WriteLine("There are currently 0 pipe server instances open.",
         pipeClient.NumberOfServerInstances);
      using (StreamReader sr = new StreamReader(pipeClient))
      
        // Display the read text to the console
        string temp;
        while ((temp = sr.ReadLine()) != null)
        
          Console.WriteLine("Received from server: 0", temp);
        
      
    
    Console.Write("Press Enter to continue...");
    Console.ReadLine();
  

如果我同时启动这两个程序,我会看到 Fortran 代码创建了一个管道,并且它会到达 READ 语句,但 C# 代码只会到达 pipeClient.Connect(),然后就坐在那里。

我是否正确设置了 C# 方面?或者也许我在 Fortran 方面没有完全正确,以便 C# 客户端“看到”管道?

【问题讨论】:

不确定以 C 结尾的字符串。这在我使用的 Fortran 编译器上不起作用。尝试将 CreateNamedPipe 中的反斜杠数量减半。在读/写之前,您还需要一个 ConnectNamedPipe。至少其中一个程序应该正在编写。如果两者都在读,而没有人在写,那么什么都不会发生。 以下内容可能会有所帮助:csharpcodi.com/vs2/241/AutoHotkey.Interop/src/… 您使用的是哪个 Fortran 编译器?我无法让管道与打开/读/写一起工作。它们确实适用于原生 Windows 调用的 WriteFile 和 ReadFile。 我正在使用英特尔 Fortran。 @cup 的建议起到了作用。为其他人发布了下面的工作代码。 【参考方案1】:

@Cup 是对的。添加到管道的连接并切换到 WriteFile 工作。 Fortran 代码:

  program Test

  use kernel32
  implicit none

  ! Pipes
  integer(HANDLE) :: pipe
  character*128 dataname /'\\.\pipe\EqnData'/
  ! Flags
  integer :: connectNamedPipeErrFlag, errorCode, writeFileErrFlag
  ! Data to send
  character*128 :: message

  pipe = CreateNamedPipe( &                       ! CreateNamesPipe: Creates an instance of a named pipe and returns a handle for subsequent pipe operations
    !                                               https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea
    '\\.\pipe\EqnData', &                         ! lpName: Unique pipe nam in th following form: \\.\pipe\pipename
    PIPE_ACCESS_DUPLEX, &                         ! dwOpenMode: Open mode (eg. PIPE_ACCESS_DUPLEX, PIPE_ACCESS_INBOUND, PIPE_ACCESS_OUTBOUND, etc.)
    PIPE_WAIT, &                                  ! dwPipeMode: Pipe mode (eg. PIPE_WAIT, etc.)
    PIPE_UNLIMITED_INSTANCES, &                   ! nMaxInstances: Maximum number of instances that can be created for this pipe
    1024, &                                       ! nOutBufferSize: Number of bytes to reserve for the output buffer
    1024, &                                       ! nInBufferSize: Number of bytes to reserve for the input buffer
    120 * 1000, &                                 ! nDefaultTimeOut: Default time-out value, in milliseconds
    NULL)                                         ! lpSecurityAttributes: A pointer to a SECURITY_ATTRIBUTES structure
  if(pipe .NE. INVALID_HANDLE_VALUE) then
    write (*,*) 'Pipe created'
  else
    ! TODO:
  endif

  connectNamedPipeErrFlag = ConnectNamedPipe( &   ! ConnectNamedPipe: Returned nonzero value if the connection is successful
    !                                               https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-connectnamedpipe
    pipe, &                                       ! hNamedPipe: Handle to the server end of a named pipe instance (returned by the CreateNamedPipe function)
    NULL)                                         ! lpOverlapped: A pointer to an OVERLAPPED structure
  if(connectNamedPipeErrFlag .NE. 0) then
    write(*,*) 'Pipe connected'
  else
    ! TODO:
  endif

  message = 'Some data'

  writeFileErrFlag = WriteFile( &                 ! Writes data to the specified file or IO device
    !                                               https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile
    pipe, &                                       ! hfile: A handle to the file or IO device
    loc(message), &                               ! lpBuffer: A pointer to the buffer containing the data to be written to the file or device.
    len(trim(message)), &                         ! nNumberOfBytesToWrite: Number of bytes to write
    0, &                                          ! lpNumberOfBytesWritten: number of bytes that were written
    NULL)                                         ! lpOverlapped:
  if (writeFileErrFlag .NE. 0) then
    write (*,*) 'Write successful'
  else
    errorCode = GetLastError()
    if(errorCode .EQ. ERROR_IO_PENDING) then
      ! TODO:
    elseif (errorCode .EQ. ERROR_INVALID_USER_BUFFER ) then
      ! TODO:
    elseif(errorCode .EQ. ERROR_NOT_ENOUGH_MEMORY ) then
      ! TODO:
    elseif(errorCode .EQ. ERROR_OPERATION_ABORTED) then
      ! TODO:
    elseif(errorCode .EQ. ERROR_NOT_ENOUGH_QUOTA) then
      ! TODO:
    else
      ! TODO:
    endif
  endif

  close(pipe)

  write(*,*) 'Pipe closed'

  end program

【讨论】:

另一种选择是***.com/questions/16619888/…

以上是关于Fortran 和 C# 之间的管道的主要内容,如果未能解决你的问题,请参考以下文章

在 C++ 和 Fortran 代码之间传递复数数组

使用 fortran dll 的线程安全 C# 服务

Fortran可分配数组和指针之间的等价性

从 C# 调用 FORTRAN DLL 的受保护内存冲突

在默认类型和 C 互操作类型之间转换 Fortran 字符和逻辑数组

在默认类型和 C 互操作类型之间转换 Fortran 字符和逻辑数组