VB2005でソケット関数を使うには?


mis  2007-01-17 04:08:24  No: 97677

VB6で動いているソケット送受信プログラムをVB2005に置き換えたのですが、
VB_Selectの実行結果がエラー(-1)で返ってしまいます。
どうすればよいでしょうか。よろしくお願い致します。

以下はソースの抜粋です
==VB6==========================================================
<構造体宣言>
Global Const FD_SETSIZE = 64      
Type fd_set
    fd_count As Long
    fd_array(FD_SETSIZE - 1) As Long
End Type

Type TimeVal
    tv_sec As Long
    tv_usec As Long
End Type

<API宣言>
Declare Function VB_Select Lib "wsock32" Alias "select" _
(ByVal nfds As Long, ByRef readfds As Any, ByRef writefds As Any,_
 ByRef exceptfds As Any, ByRef timeout As TimeVal) As Long
 
<実行>
Dim TimeOut As TimeVal
Dim wfds As fd_set
Dim rfds As fd_set

TimeOut.tv_sec = 0
TimeOut.tv_usec = 0
rfds.fd_count = 0
wfds.fd_count = 1
wfds.fd_array(0) = iSockID

rc& = VB_Select(64, ByVal 0&, wfds, ByVal 0&, TimeOut)  '送信
Select Case rc&
Case 0
    gliSockERRCode = ERR_PACS_TIMEOUT
Case -1
    gliSockERRCode = ERR_PACS_SELECT
.....
rc& = VB_Select(64, rfds, ByVal 0&, ByVal 0&, TimeOut)  '受信

==VB2005=========================================================  
<構造体宣言>
Public Const FD_SETSIZE As Short = 64 
Structure fd_set
    Dim fd_count As Integer
    <VBFixedArray(FD_SETSIZE - 1)> Dim fd_array() As Integer
    Public Sub Initialize()
        ReDim fd_array(FD_SETSIZE - 1)
    End Sub
End Structure

Structure TimeVal
    Dim tv_sec As Integer
    Dim tv_usec As Integer
End Structure

<API宣言>
Declare Function VB_Select Lib "wsock32" Alias "select" _
  (ByVal nfds As Integer, ByRef readfds As Integer, ByRef writefds As fd_set, _
  ByRef exceptfds As Integer, ByRef TimeOut As TimeVal) As Integer
Declare Function VB_Select Lib "wsock32" Alias "select" _
  (ByVal nfds As Integer, ByRef readfds As fd_set, ByRef writefds As Integer, _
  ByRef exceptfds As Integer, ByRef TimeOut As TimeVal) As Integer
  ・・・・
<実行>
Dim TimeOut As New TimeVal 
Dim wfds As New fd_set
wfds.Initialize()
・・・・
rc = VB_Select(64, 0, wfds, 0, TimeOut)  '送信

rc = VB_Select(0, rfds, 0, 0, TimeOut)  '受信
==================================================================


Hongliang  URL  2007-01-17 06:09:13  No: 97678

VBFixedArray 属性は VB 互換関数に有効なだけで、P/Invoke  には使用できません。代わりに MarshalAs 属性で UnmanagedType.ByValArray を、SizeConst フィールドを併用して指定します。
詳しくは MSDN2 かヘルプかで「アンマネージ コードとの相互運用性」以下をお読み下さい。


Hongliang  URL  2007-01-17 06:10:55  No: 97679

あ、それと、折角 .NET をつかってるんですから、できるならわざわざ API 呼び出しをするよりも System.Net.Sockets の Socket や TcpClient クラスを使った方が宜しいかと思います。


mis  2007-02-26 18:20:02  No: 97680

どうもありがとうございます。
selectのエラーについては解決しました。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加