- 題名: 名前付きパイプ通信について
- 日時: 2003/08/06 15:23:41
- ID: 257
- この記事の返信元:
- (なし)
- この記事への返信:
- [268] Re[1]: 名前付きパイプ通信について2003/08/07 16:37:52
- ツリーを表示
ココへUPされたsourceが元だからコーディングの問題ではなく、 多分環境的な違いだと思われます。 (ドメインに参加しないスタンドアロンな端末のローカルadministratorアカウントで Server・Client同居で試されたら絶対動くと思う。 OS,.net,VS6をクリーンインストしたらさらに完璧。) '************************************************************ .net Client Private Const GENERIC_WRITE = &H40000000 Private Const GENERIC_READ = &H80000000 Private Const FILE_SHARE_WRITE = &H2 Private Const FILE_SHARE_READ = &H1 Private Const OPEN_EXISTING = 3 Private Const FILE_ATTRIBUTE_NORMAL = &H80 Private Const INVALID_HANDLE_VALUE = -1 <StructLayout(LayoutKind.Sequential)> _ Public Structure SECURITY_ATTRIBUTES Public nLength As Integer Public lpSecurityDescriptor As Integer Public bInheritHandle As Boolean End Structure Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _ ByVal lpFileName As String, _ ByVal dwDesiredAccess As Integer, _ ByVal dwShareMode As Integer, _ ByRef lpSecurityAttributes As Integer, _ ByVal dwCreationDisposition As Integer, _ ByVal dwFlagsAndAttributes As Integer, _ ByVal hTemplateFile As Integer) As Integer Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _ ByVal lpFileName As String, _ ByVal dwDesiredAccess As Integer, _ ByVal dwShareMode As Integer, _ ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _ ByVal dwCreationDisposition As Integer, _ ByVal dwFlagsAndAttributes As Integer, _ ByVal hTemplateFile As Integer) As Integer Private Declare Auto Function PeekNamedPipe Lib "kernel32" ( _ ByVal hNamedPipe As Int32, _ ByVal lpBuffer As Byte(), _ ByVal nBufferSize As Int32, _ ByRef lpBytesRead As Int32, _ ByRef lpTotalBytesAvail As Int32, _ ByRef lpBytesLeftThisMessage As Int32) As Int32 Private Declare Auto Function ReadFile Lib "kernel32" Alias "ReadFile" ( _ ByVal hFile As Int32, _ ByVal lpBuffer As Byte(), _ ByVal nNumberOfBytesToRead As Int32, _ ByRef lpNumberOfBytesRead As Int32, _ ByVal lpOverlapped_NULL As Int32) As Int32 Private Declare Auto Function CloseHandle Lib "kernel32" ( _ ByVal hObject As Int32) As Int32 Private m_hFile As Integer Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim sa As SECURITY_ATTRIBUTES sa.nLength = Len(sa) m_hFile = CreateFile(TextBoxPipeName.Text, _ GENERIC_WRITE Or GENERIC_READ, _ FILE_SHARE_WRITE Or FILE_SHARE_READ, _ sa, _ OPEN_EXISTING, _ FILE_ATTRIBUTE_NORMAL, 0) Label1.Text = m_hFile & " (" & INVALID_HANDLE_VALUE & "=失敗)" Label2.Text = "" Label3.Text = "" Label4.Text = "" End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim ret As Integer Dim buf(1023) As Byte Dim intBytesRead As Integer Dim intTotalBytesAvail As Integer Dim intBytesLeftThisMessage As Integer ret = PeekNamedPipe(m_hFile, buf, buf.Length, _ intBytesRead, intTotalBytesAvail, _ intBytesLeftThisMessage) Label2.Text = String.Format( _ "戻り(0=失敗)={0} : 読バイト={1} : 総バイト={2} : 残バイト{3}", _ New Object() {ret, intBytesRead, intTotalBytesAvail, _ intBytesLeftThisMessage}) TextBoxReadData.Text = Encoding.Default.GetString(buf) End Sub Private Sub Button3_Click_1(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click Dim ret As Integer Dim buf(1023) As Byte Dim intNumberOfBytesRead As Integer ret = ReadFile(m_hFile, buf, buf.Length, intNumberOfBytesRead, 0) Label3.Text = String.Format("戻り(0=失敗)={0} : 読バイト={1}", _ New Object() {ret, intNumberOfBytesRead}) TextBoxReadData.Text = Encoding.Default.GetString(buf) End Sub Private Sub Button4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button4.Click Label4.Text = CloseHandle(m_hFile) & " (0=失敗)" End Sub '************************************************************ VB6 Server Option Explicit Private Const PIPE_ACCESS_DUPLEX = &H3 Private Const PIPE_TYPE_MESSAGE = &H4 Private Const PIPE_UNLIMITED_INSTANCES = 255 Private Const PIPE_READMODE_MESSAGE = &H2 Private Const PIPE_WAIT = &H0 Private Const INVALID_HANDLE_VALUE = -1 Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Private Declare Function CreateNamedPipe _ Lib "kernel32" Alias "CreateNamedPipeA" ( _ ByVal lpName As String, _ ByVal dwOpenMode As Long, _ ByVal dwPipeMode As Long, _ ByVal nMaxInstances As Long, _ ByVal nOutBufferSize As Long, _ ByVal nInBufferSize As Long, _ ByVal nDefaultTimeOut As Long, _ lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long Private Declare Function WriteFile Lib "kernel32" ( _ ByVal hFile As Long, lpBuffer As Any, _ ByVal nNumberOfBytesToWrite As Long, _ ByRef lpNumberOfBytesWritten As Long, _ ByVal lpOverlapped As Long) As Long Private Declare Function CloseHandle Lib "kernel32" ( _ ByVal hObject As Long) As Long Private m_hPipe As Long Private Sub Command1_Click() Const RECV_MAXLENGTH = 1024 Dim udtSecurityAttributes As SECURITY_ATTRIBUTES With udtSecurityAttributes .nLength = Len(udtSecurityAttributes) .lpSecurityDescriptor = 0 .bInheritHandle = True End With m_hPipe = CreateNamedPipe(txtPipeName.Text, _ PIPE_ACCESS_DUPLEX, _ PIPE_WAIT Or _ PIPE_READMODE_MESSAGE Or _ PIPE_TYPE_MESSAGE, _ PIPE_UNLIMITED_INSTANCES, _ RECV_MAXLENGTH, _ RECV_MAXLENGTH, _ 1000, _ udtSecurityAttributes) Label1 = m_hPipe & " (" & INVALID_HANDLE_VALUE & "=失敗)" Label2 = "" Label3 = "" End Sub Private Sub Command2_Click() Dim lngNumberOfBytesWritten As Long Dim strData As String Dim ret As Long strData = txtWriteString.Text ret = WriteFile(m_hPipe, ByVal strData, LenB(strData), _ lngNumberOfBytesWritten, 0) Label2 = ret & " (0=失敗) : 書込byte数=" & lngNumberOfBytesWritten End Sub Private Sub Command3_Click() Label3 = CloseHandle(m_hPipe) & " (0=失敗) " End Sub
どなたか教えてください。
.NETで名前付きパイプ通信(クライアント側)をしたいのですが、
CreateFile、PeekNamedPipe、ClosePipeの
APIを使用したいのですが、サーバー側を起動させていても、
CreateFileで戻り値:-1しか返ってこなくて、
いろいろ調べてみましたが、うまく動きません。
宣言がまちがっていると思うのですが。。。
(サーバー側は、VBで作成していて、
同じVBで作成したクライアント側とは、パイプ通信できています。)
また、もしかして、これらのAPIにかわるクラスがあるのでしょうか?
宜しくお願い致します。
--CreateFileの宣言
<StructLayout(LayoutKind.Sequential)> _
Public Structure SECURITY_ATTRIBUTES
Public nLength As Integer
Public lpSecurityDescriptor As Integer
Public bInheritHandle As Boolean
End Structure
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByRef lpSecurityAttributes As Integer, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer