VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CWinsock"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'programmed by ShareVB
Option Explicit

'réseaux TCP/IP (INET)
Private Const AF_INET As Long = 2
'UDP
Private Const SOCK_DGRAM As Long = 2
'TCP
Private Const SOCK_STREAM As Long = 1

'infos sur la dll WinSock
Private Type WSADATA
    wVersion As Integer
    wHighVersion As Integer
    szDescription As String * 256
    szSystemStatus As String * 128
    iMaxSockets As Integer
    iMaxUdpDg As Integer
    lpVendorInfo As Long
End Type
'init de la dll Winsock
Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVersionRequired As Long, ByRef lpWSAData As WSADATA) As Long
'libération de la dll Winsock
Private Declare Function WSACleanup Lib "ws2_32.dll" () As Long

'formatte un message d'erreur du système
Private Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000
Private Declare Function FormatMessage Lib "kernel32.dll" Alias "FormatMessageA" (ByVal dwFlags As Long, ByRef lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, ByRef Arguments As Long) As Long

'==================================================
'méthodes privées
'==================================================

'init de la dll WinSock
Private Sub InitWinsock(Optional ByVal dwVersion As Long = &H202&)
    Dim WSA As WSADATA
    WSAStartup dwVersion, WSA
End Sub

'libération de la dll WinSock
Private Sub CleanupWinsock()
    WSACleanup
End Sub

'==================================================
'méthodes publiques
'==================================================

'formate le code d'erreur errCode sous une formet textuelle
'===========================================================
'errCode : code d'erreur à formater
'revnoie l'erreur textuelle
Public Function FormatError(ByVal errCode As Long) As String
Dim l As Long
FormatError = Space(256)
l = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, errCode, 0&, FormatError, 256, ByVal 0&)
FormatError = Mid$(FormatError, 1, l)
End Function

'crée un socket UDP
'renvoie le socket créé
Public Function CreateUDPSocket() As CSocket
    Set CreateUDPSocket = New CSocket
    CreateUDPSocket.Create SOCK_DGRAM
End Function

'crée un socket TCP
'renvoie le socket créé
Public Function CreateTCPSocket() As CSocket
    Set CreateTCPSocket = New CSocket
    CreateTCPSocket.Create SOCK_STREAM
End Function

'==================================================
'constructeur et destructeur
'==================================================

Private Sub Class_Initialize()
InitWinsock
End Sub

Private Sub Class_Terminate()
CleanupWinsock
End Sub
