PageRenderTime 65ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/vbip-ftp-client-library-1.0.4/MSocketSupport.bas

http://xbot2003.googlecode.com/
Basic | 1174 lines | 581 code | 33 blank | 560 comment | 2 complexity | 8213896b1751407ee0cb0e0a6fca612c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. Attribute VB_Name = "MSocketSupport"
  2. '********************************************************************************
  3. 'MSocketSupport module
  4. 'Copyright 2002 by Oleg Gdalevich
  5. 'Visual Basic Internet Programming website (http://www.vbip.com)
  6. '********************************************************************************
  7. 'This module contains API declarations and helper functions for the CSocket class
  8. '********************************************************************************
  9. 'Version: 1.0.12 Modified: 17-OCT-2002
  10. '********************************************************************************
  11. 'To get latest version of this code please visit the following web page:
  12. 'http://www.vbip.com/winsock-api/csocket-class/csocket-class-01.asp
  13. '********************************************************************************
  14. Option Explicit
  15. '
  16. Public Const INADDR_NONE = &HFFFF
  17. '
  18. Public Const SOCKET_ERROR = -1
  19. Public Const INVALID_SOCKET = -1
  20. Public Const INADDR_ANY = &H0
  21. '
  22. Public Const FD_SETSIZE = 64
  23. '
  24. '/*
  25. ' * Define constant based on rfc883, used by gethostbyxxxx() calls.
  26. ' */
  27. Public Const MAXGETHOSTSTRUCT = 1024
  28. '
  29. '/*
  30. ' * WinSock 2 extension -- manifest constants for shutdown()
  31. ' */
  32. Public Const SD_RECEIVE = &H0
  33. Public Const SD_SEND = &H1
  34. Public Const SD_BOTH = &H2
  35. '
  36. Public Const MSG_OOB = &H1 '/* process out-of-band data */
  37. Public Const MSG_PEEK = &H2 '/* peek at incoming message */
  38. Public Const MSG_DONTROUTE = &H4 '/* send without using routing tables */
  39. Public Const MSG_PARTIAL = &H8000 '/* partial send or recv for message xport */
  40. '
  41. Public Const FD_READ = &H1&
  42. Public Const FD_WRITE = &H2&
  43. Public Const FD_OOB = &H4&
  44. Public Const FD_ACCEPT = &H8&
  45. Public Const FD_CONNECT = &H10&
  46. Public Const FD_CLOSE = &H20&
  47. '
  48. Public Const SOL_SOCKET = 65535
  49. '
  50. ' option flags per socket
  51. Public Const SO_DEBUG = &H1& ' Turn on debugging info recording
  52. Public Const SO_ACCEPTCONN = &H2& ' Socket has had listen() - READ-ONLY.
  53. Public Const SO_REUSEADDR = &H4& ' Allow local address reuse.
  54. Public Const SO_KEEPALIVE = &H8& ' Keep connections alive.
  55. Public Const SO_DONTROUTE = &H10& ' Just use interface addresses.
  56. Public Const SO_BROADCAST = &H20& ' Permit sending of broadcast msgs.
  57. Public Const SO_USELOOPBACK = &H40& ' Bypass hardware when possible.
  58. Public Const SO_LINGER = &H80& ' Linger on close if data present.
  59. Public Const SO_OOBINLINE = &H100& ' Leave received OOB data in line.
  60. Public Const SO_DONTLINGER = Not SO_LINGER
  61. Public Const SO_EXCLUSIVEADDRUSE = Not SO_REUSEADDR ' Disallow local address reuse.
  62. ' Additional options.
  63. Public Const SO_SNDBUF = &H1001& ' Send buffer size.
  64. Public Const SO_RCVBUF = &H1002& ' Receive buffer size.
  65. Public Const SO_ERROR = &H1007& ' Get error status and clear.
  66. Public Const SO_TYPE = &H1008& ' Get socket type - READ-ONLY.
  67. '
  68. Public Const WSADESCRIPTION_LEN = 257
  69. Public Const WSASYS_STATUS_LEN = 129
  70. '
  71. Public Type WSAData
  72. wVersion As Integer
  73. wHighVersion As Integer
  74. szDescription As String * WSADESCRIPTION_LEN
  75. szSystemStatus As String * WSASYS_STATUS_LEN
  76. iMaxSockets As Integer
  77. iMaxUdpDg As Integer
  78. lpVendorInfo As Long
  79. End Type
  80. '
  81. Public Type sockaddr_in
  82. sin_family As Integer
  83. sin_port As Integer
  84. sin_addr As Long
  85. sin_zero(1 To 8) As Byte
  86. End Type
  87. Public Type fd_set
  88. fd_count As Long '// how many are SET?
  89. fd_array(1 To FD_SETSIZE) As Long '// an array of SOCKETs
  90. End Type
  91. '
  92. '/*
  93. ' * All Windows Sockets error constants are biased by WSABASEERR from
  94. ' * the "normal"
  95. ' */
  96. Public Const WSABASEERR = 10000
  97. '/*
  98. ' * Windows Sockets definitions of regular Microsoft C error constants
  99. ' */
  100. Public Const WSAEINTR = (WSABASEERR + 4)
  101. Public Const WSAEBADF = (WSABASEERR + 9)
  102. Public Const WSAEACCES = (WSABASEERR + 13)
  103. Public Const WSAEFAULT = (WSABASEERR + 14)
  104. Public Const WSAEINVAL = (WSABASEERR + 22)
  105. Public Const WSAEMFILE = (WSABASEERR + 24)
  106. '/*
  107. ' * Windows Sockets definitions of regular Berkeley error constants
  108. ' */
  109. Public Const WSAEWOULDBLOCK = (WSABASEERR + 35)
  110. Public Const WSAEINPROGRESS = (WSABASEERR + 36)
  111. Public Const WSAEALREADY = (WSABASEERR + 37)
  112. Public Const WSAENOTSOCK = (WSABASEERR + 38)
  113. Public Const WSAEDESTADDRREQ = (WSABASEERR + 39)
  114. Public Const WSAEMSGSIZE = (WSABASEERR + 40)
  115. Public Const WSAEPROTOTYPE = (WSABASEERR + 41)
  116. Public Const WSAENOPROTOOPT = (WSABASEERR + 42)
  117. Public Const WSAEPROTONOSUPPORT = (WSABASEERR + 43)
  118. Public Const WSAESOCKTNOSUPPORT = (WSABASEERR + 44)
  119. Public Const WSAEOPNOTSUPP = (WSABASEERR + 45)
  120. Public Const WSAEPFNOSUPPORT = (WSABASEERR + 46)
  121. Public Const WSAEAFNOSUPPORT = (WSABASEERR + 47)
  122. Public Const WSAEADDRINUSE = (WSABASEERR + 48)
  123. Public Const WSAEADDRNOTAVAIL = (WSABASEERR + 49)
  124. Public Const WSAENETDOWN = (WSABASEERR + 50)
  125. Public Const WSAENETUNREACH = (WSABASEERR + 51)
  126. Public Const WSAENETRESET = (WSABASEERR + 52)
  127. Public Const WSAECONNABORTED = (WSABASEERR + 53)
  128. Public Const WSAECONNRESET = (WSABASEERR + 54)
  129. Public Const WSAENOBUFS = (WSABASEERR + 55)
  130. Public Const WSAEISCONN = (WSABASEERR + 56)
  131. Public Const WSAENOTCONN = (WSABASEERR + 57)
  132. Public Const WSAESHUTDOWN = (WSABASEERR + 58)
  133. Public Const WSAETOOMANYREFS = (WSABASEERR + 59)
  134. Public Const WSAETIMEDOUT = (WSABASEERR + 60)
  135. Public Const WSAECONNREFUSED = (WSABASEERR + 61)
  136. Public Const WSAELOOP = (WSABASEERR + 62)
  137. Public Const WSAENAMETOOLONG = (WSABASEERR + 63)
  138. Public Const WSAEHOSTDOWN = (WSABASEERR + 64)
  139. Public Const WSAEHOSTUNREACH = (WSABASEERR + 65)
  140. Public Const WSAENOTEMPTY = (WSABASEERR + 66)
  141. Public Const WSAEPROCLIM = (WSABASEERR + 67)
  142. Public Const WSAEUSERS = (WSABASEERR + 68)
  143. Public Const WSAEDQUOT = (WSABASEERR + 69)
  144. Public Const WSAESTALE = (WSABASEERR + 70)
  145. Public Const WSAEREMOTE = (WSABASEERR + 71)
  146. '/*
  147. ' * Extended Windows Sockets error constant definitions
  148. ' */
  149. Public Const WSASYSNOTREADY = (WSABASEERR + 91)
  150. Public Const WSAVERNOTSUPPORTED = (WSABASEERR + 92)
  151. Public Const WSANOTINITIALISED = (WSABASEERR + 93)
  152. Public Const WSAEDISCON = (WSABASEERR + 101)
  153. Public Const WSAENOMORE = (WSABASEERR + 102)
  154. Public Const WSAECANCELLED = (WSABASEERR + 103)
  155. Public Const WSAEINVALIDPROCTABLE = (WSABASEERR + 104)
  156. Public Const WSAEINVALIDPROVIDER = (WSABASEERR + 105)
  157. Public Const WSAEPROVIDERFAILEDINIT = (WSABASEERR + 106)
  158. Public Const WSASYSCALLFAILURE = (WSABASEERR + 107)
  159. Public Const WSASERVICE_NOT_FOUND = (WSABASEERR + 108)
  160. Public Const WSATYPE_NOT_FOUND = (WSABASEERR + 109)
  161. Public Const WSA_E_NO_MORE = (WSABASEERR + 110)
  162. Public Const WSA_E_CANCELLED = (WSABASEERR + 111)
  163. Public Const WSAEREFUSED = (WSABASEERR + 112)
  164. '
  165. '/* Authoritative Answer: Host not found */
  166. Public Const WSAHOST_NOT_FOUND = (WSABASEERR + 1001)
  167. '/* Non-Authoritative: Host not found, or SERVERFAIL */
  168. Public Const WSATRY_AGAIN = (WSABASEERR + 1002)
  169. '/* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  170. Public Const WSANO_RECOVERY = (WSABASEERR + 1003)
  171. '/* Valid name, no data record of requested type */
  172. Public Const WSANO_DATA = (WSABASEERR + 1004)
  173. '
  174. '
  175. 'Socket types
  176. '
  177. Public Enum SocketType
  178. SOCK_STREAM = 1 ' /* stream socket */
  179. SOCK_DGRAM = 2 ' /* datagram socket */
  180. SOCK_RAW = 3 ' /* raw-protocol interface */
  181. SOCK_RDM = 4 ' /* reliably-delivered message */
  182. SOCK_SEQPACKET = 5 ' /* sequenced packet stream */
  183. End Enum
  184. '
  185. Public Enum AddressFamily
  186. '
  187. AF_UNSPEC = 0 '/* unspecified */
  188. '/*
  189. ' * Although AF_UNSPEC is defined for backwards compatibility, using
  190. ' * AF_UNSPEC for the "af" parameter when creating a socket is STRONGLY
  191. ' * DISCOURAGED. The interpretation of the "protocol" parameter
  192. ' * depends on the actual address family chosen. As environments grow
  193. ' * to include more and more address families that use overlapping
  194. ' * protocol values there is more and more chance of choosing an
  195. ' * undesired address family when AF_UNSPEC is used.
  196. ' */
  197. AF_UNIX = 1 '/* local to host (pipes, portals) */
  198. AF_INET = 2 '/* internetwork: UDP, TCP, etc. */
  199. AF_IMPLINK = 3 '/* arpanet imp addresses */
  200. AF_PUP = 4 '/* pup protocols: e.g. BSP */
  201. AF_CHAOS = 5 '/* mit CHAOS protocols */
  202. AF_NS = 6 '/* XEROX NS protocols */
  203. AF_IPX = AF_NS '/* IPX protocols: IPX, SPX, etc. */
  204. AF_ISO = 7 '/* ISO protocols */
  205. AF_OSI = AF_ISO '/* OSI is ISO */
  206. AF_ECMA = 8 '/* european computer manufacturers */
  207. AF_DATAKIT = 9 '/* datakit protocols */
  208. AF_CCITT = 10 '/* CCITT protocols, X.25 etc */
  209. AF_SNA = 11 '/* IBM SNA */
  210. AF_DECnet = 12 '/* DECnet */
  211. AF_DLI = 13 '/* Direct data link interface */
  212. AF_LAT = 14 '/* LAT */
  213. AF_HYLINK = 15 '/* NSC Hyperchannel */
  214. AF_APPLETALK = 16 '/* AppleTalk */
  215. AF_NETBIOS = 17 '/* NetBios-style addresses */
  216. AF_VOICEVIEW = 18 '/* VoiceView */
  217. AF_FIREFOX = 19 '/* Protocols from Firefox */
  218. AF_UNKNOWN1 = 20 '/* Somebody is using this! */
  219. AF_BAN = 21 '/* Banyan */
  220. AF_ATM = 22 '/* Native ATM Services */
  221. AF_INET6 = 23 '/* Internetwork Version 6 */
  222. AF_CLUSTER = 24 '/* Microsoft Wolfpack */
  223. AF_12844 = 25 '/* IEEE 1284.4 WG AF */
  224. AF_MAX = 26
  225. '
  226. End Enum
  227. '
  228. '/*
  229. ' * Protocols
  230. ' */
  231. Public Enum SocketProtocol
  232. IPPROTO_IP = 0 '/* dummy for IP */
  233. IPPROTO_ICMP = 1 '/* control message protocol */
  234. IPPROTO_IGMP = 2 '/* internet group management protocol */
  235. IPPROTO_GGP = 3 '/* gateway^2 (deprecated) */
  236. IPPROTO_TCP = 6 '/* tcp */
  237. IPPROTO_PUP = 12 '/* pup */
  238. IPPROTO_UDP = 17 '/* user datagram protocol */
  239. IPPROTO_IDP = 22 '/* xns idp */
  240. IPPROTO_ND = 77 '/* UNOFFICIAL net disk proto */
  241. IPPROTO_RAW = 255 '/* raw IP packet */
  242. IPPROTO_MAX = 256
  243. End Enum
  244. '
  245. Public Type HOSTENT
  246. hName As Long
  247. hAliases As Long
  248. hAddrType As Integer
  249. hLength As Integer
  250. hAddrList As Long
  251. End Type
  252. '
  253. Public Declare Function gethostbyaddr Lib "ws2_32.dll" (addr As Long, ByVal addr_len As Long, ByVal addr_type As Long) As Long
  254. Public Declare Function gethostbyname Lib "ws2_32.dll" (ByVal host_name As String) As Long
  255. Public Declare Function gethostname Lib "ws2_32.dll" (ByVal host_name As String, ByVal namelen As Long) As Long
  256. Public Declare Function getservbyname Lib "ws2_32.dll" (ByVal serv_name As String, ByVal proto As String) As Long
  257. Public Declare Function getprotobynumber Lib "ws2_32.dll" (ByVal proto As Long) As Long
  258. Public Declare Function getprotobyname Lib "ws2_32.dll" (ByVal proto_name As String) As Long
  259. Public Declare Function getservbyport Lib "ws2_32.dll" (ByVal Port As Integer, ByVal proto As Long) As Long
  260. Public Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long
  261. Public Declare Function inet_ntoa Lib "ws2_32.dll" (ByVal inn As Long) As Long
  262. Public Declare Function htons Lib "ws2_32.dll" (ByVal hostshort As Integer) As Integer
  263. Public Declare Function htonl Lib "ws2_32.dll" (ByVal hostlong As Long) As Long
  264. Public Declare Function ntohl Lib "ws2_32.dll" (ByVal netlong As Long) As Long
  265. Public Declare Function ntohs Lib "ws2_32.dll" (ByVal netshort As Integer) As Integer
  266. Public Declare Function api_socket Lib "ws2_32.dll" Alias "socket" (ByVal af As Long, ByVal s_type As Long, ByVal Protocol As Long) As Long
  267. Public Declare Function api_closesocket Lib "ws2_32.dll" Alias "closesocket" (ByVal s As Long) As Long
  268. Public Declare Function api_connect Lib "ws2_32.dll" Alias "connect" (ByVal s As Long, ByRef name As sockaddr_in, ByVal namelen As Long) As Long
  269. Public Declare Function getsockname Lib "ws2_32.dll" (ByVal s As Long, ByRef name As sockaddr_in, ByRef namelen As Long) As Long
  270. Public Declare Function getpeername Lib "ws2_32.dll" (ByVal s As Long, ByRef name As sockaddr_in, ByRef namelen As Long) As Long
  271. Public Declare Function api_bind Lib "ws2_32.dll" Alias "bind" (ByVal s As Long, ByRef name As sockaddr_in, ByRef namelen As Long) As Long
  272. Public Declare Function api_select Lib "ws2_32.dll" Alias "select" (ByVal nfds As Long, ByRef readfds As Any, ByRef writefds As Any, ByRef exceptfds As Any, ByRef TimeOut As Long) As Long
  273. Public Declare Function recv Lib "ws2_32.dll" (ByVal s As Long, ByRef buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
  274. Public Declare Function send Lib "ws2_32.dll" (ByVal s As Long, ByRef buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
  275. Public Declare Function shutdown Lib "ws2_32.dll" (ByVal s As Long, ByVal how As Long) As Long
  276. Public Declare Function api_listen Lib "ws2_32.dll" Alias "listen" (ByVal s As Long, ByVal backlog As Long) As Long
  277. Public Declare Function api_accept Lib "ws2_32.dll" Alias "accept" (ByVal s As Long, ByRef addr As sockaddr_in, ByRef addrlen As Long) As Long
  278. Public Declare Function setsockopt Lib "ws2_32.dll" (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, ByVal optlen As Long) As Long
  279. Public Declare Function getsockopt Lib "ws2_32.dll" (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, optlen As Long) As Long
  280. Public Declare Function sendto Lib "ws2_32.dll" (ByVal s As Long, ByRef buf As Any, ByVal buflen As Long, ByVal flags As Long, ByRef toaddr As sockaddr_in, ByVal tolen As Long) As Long
  281. Public Declare Function recvfrom Lib "ws2_32.dll" (ByVal s As Long, ByRef buf As Any, ByVal buflen As Long, ByVal flags As Long, ByRef from As sockaddr_in, ByRef fromlen As Long) As Long
  282. Public Declare Function WSAAsyncSelect Lib "ws2_32.dll" (ByVal s As Long, ByVal hwnd As Long, ByVal wMsg As Long, ByVal lEvent As Long) As Long
  283. Public Declare Function WSAAsyncGetHostByAddr Lib "ws2_32.dll" (ByVal hwnd As Long, ByVal wMsg As Long, ByRef lngAddr As Long, ByVal lngLenght As Long, ByVal lngType As Long, buf As Any, ByVal lngBufLen As Long) As Long
  284. Public Declare Function WSAAsyncGetHostByName Lib "ws2_32.dll" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal strHostName As String, buf As Any, ByVal buflen As Long) As Long
  285. Public Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVR As Long, lpWSAD As WSAData) As Long
  286. Public Declare Function WSACleanup Lib "ws2_32.dll" () As Long
  287. '
  288. Private Const GWL_WNDPROC = -4
  289. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  290. Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
  291. Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
  292. 'Added: 04-MAR-2002
  293. Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
  294. 'Added: 17-OCT-2002
  295. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc&, ByVal hwnd&, ByVal Msg&, ByVal wParam&, ByVal lParam&) As Long
  296. Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
  297. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
  298. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
  299. Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
  300. '
  301. Public Const GMEM_FIXED = &H0
  302. Public Const GMEM_MOVEABLE = &H2
  303. '
  304. Public Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  305. Public Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
  306. Public Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
  307. Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
  308. '---------------------------------------------
  309. 'Modified: 23-AUG-2002
  310. '---------------------------------------------
  311. 'The variable scope has been changed to Public to be
  312. 'visible from the CSocket class module
  313. 'Private m_lngWindowHandle As Long
  314. Public p_lngWindowHandle As Long
  315. '---------------------------------------------
  316. Private m_colSockets As Collection
  317. Private m_colResolvers As Collection
  318. Private m_colMemoryBlocks As Collection
  319. Private m_lngPreviousValue As Long
  320. Private m_blnGetHostRecv As Boolean
  321. Private m_blnWinsockInit As Boolean
  322. Private m_lngMaxMsgSize As Long
  323. Private Const WM_USER = &H400
  324. '
  325. 'Private Const RESOLVE_MESSAGE = WM_USER + 1
  326. 'Private Const SOCKET_MESSAGE = WM_USER + 2
  327. '
  328. Private m_lngResolveMessage As Long 'Added: 04-MAR-2002
  329. '---------------------------------------------
  330. 'Modified: 23-AUG-2002
  331. '---------------------------------------------
  332. 'The variable scope has been changed to Public to be
  333. 'visible from the CSocket class module
  334. 'Private m_lngWinsockMessage As Long 'Added: 04-MAR-2002
  335. Public p_lngWinsockMessage As Long
  336. '---------------------------------------------
  337. '
  338. Private Const OFFSET_4 = 4294967296#
  339. Private Const MAXINT_4 = 2147483647
  340. Private Const OFFSET_2 = 65536
  341. Private Const MAXINT_2 = 32767
  342. Private Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  343. '
  344. 'This the callback function of the window created to hook
  345. 'messages sent by the Winsock service. It handles only two
  346. 'types of messages - network events for the sockets the
  347. 'WSAAsyncSelect fucntion was called for, and the messages
  348. 'sent in response to the WSAAsyncGetHostByName and
  349. 'WSAAsyncGetHostByAddress Winsock API functions.
  350. '
  351. 'Then the message is received, this function creates illegal
  352. 'reference to the instance of the CSocket class and calls
  353. 'either the PostSocketEvent or PostGetHostEvent method of the
  354. 'class to pass that message to the class.
  355. '
  356. Dim objSocket As CSocket 'the illegal reference to an
  357. 'instance of the CSocket class
  358. Dim lngObjPointer As Long 'pointer to the existing instance
  359. 'of the CSocket class
  360. Dim lngEventID As Long 'network event
  361. Dim lngErrorCode As Long 'code of the error message
  362. Dim lngMemoryHandle As Long 'descriptor of the allocated
  363. 'memory object
  364. Dim lngMemoryPointer As Long 'pointer to the allocated memory
  365. Dim lngHostAddress As Long '32-bit host address
  366. Dim strHostName As String 'a host hame
  367. Dim udtHost As HOSTENT 'structure of the data in the
  368. 'allocated memory block
  369. Dim lngIpAddrPtr As Long 'pointer to the IP address string
  370. '
  371. On Error GoTo ERORR_HANDLER
  372. '
  373. If uMsg = p_lngWinsockMessage Then 'Modified: 04-MAR-2002
  374. '
  375. 'All the pointers to the existing instances of the CSocket class
  376. 'are stored in the m_colSockets collection. Key of the collection's
  377. 'item contains a value of the socket handle, and a value of the
  378. 'collection item is the Long value that is a pointer the object,
  379. 'instance of the CSocket class. Since the wParam argument of the
  380. 'callback function contains a value of the socket handle the
  381. 'function has received the network event message for, we can use
  382. 'that value to get the object's pointer. With the pointer value
  383. 'we can create the illegal reference to the object to be able to
  384. 'call any Public or Friend subroutine of that object.
  385. '
  386. Set objSocket = SocketObjectFromPointer(CLng(m_colSockets("S" & wParam)))
  387. '
  388. 'Retrieve the network event ID
  389. lngEventID = LoWord(lParam)
  390. 'Retrieve the error code
  391. lngErrorCode = HiWord(lParam)
  392. '
  393. 'Forward the message to the instance of the CSocket class
  394. objSocket.PostSocketEvent lngEventID, lngErrorCode
  395. '
  396. ElseIf uMsg = m_lngResolveMessage Then 'Modified: 04-MAR-2002
  397. '
  398. 'A message has been received in response to the call of
  399. 'the WSAAsyncGetHostByName or WSAAsyncGetHostByAddress.
  400. '
  401. 'Retrieve the error code
  402. lngErrorCode = HiWord(lParam)
  403. '
  404. 'The wParam parameter of the callback function contains
  405. 'the task handle returned by the original function call
  406. '(see the ResolveHost function for more info). This value
  407. 'is used as a key of the m_colResolvers collection item.
  408. 'The item of that collection contains a pointer to the
  409. 'instance of the CSocket class. So, if we know a value
  410. 'of the task handle, we can find out the pointer to the
  411. 'object which called the ResolveHost function in this module.
  412. '
  413. 'Get the object pointer by the task handle value
  414. lngObjPointer = CLng(m_colResolvers("R" & wParam))
  415. '
  416. 'A value of the pointer to the instance of the CSocket class
  417. 'is used also as a key for the m_colMemoryBlocks collection
  418. 'item that contains a handle of the allocated memory block
  419. 'object. That memory block is the buffer where the
  420. 'WSAAsyncGetHostByName and WSAAsyncGetHostByAddress functions
  421. 'store the result HOSTENT structure.
  422. '
  423. 'Get the handle of the allocated memory block object by the
  424. 'pointer to the instance of the CSocket class.
  425. lngMemoryHandle = CLng(m_colMemoryBlocks("S" & lngObjPointer))
  426. '
  427. 'Lock the memory block and get address of the buffer where
  428. 'the HOSTENT structure data is stored.
  429. lngMemoryPointer = GlobalLock(lngMemoryHandle)
  430. '
  431. 'Create an illegal reference to the instance of the
  432. 'CSocket class
  433. Set objSocket = SocketObjectFromPointer(lngObjPointer)
  434. '
  435. 'Now we can forward the message to that instance.
  436. '
  437. If lngErrorCode <> 0 Then
  438. '
  439. 'If the host was not resolved, pass the error code value
  440. objSocket.PostGetHostEvent 0, 0, "", lngErrorCode
  441. '
  442. Else
  443. '
  444. 'Move data from the allocated memory block to the
  445. 'HOSTENT structure - udtHost
  446. CopyMemory udtHost, ByVal lngMemoryPointer, Len(udtHost)
  447. '
  448. 'Get a 32-bit host address
  449. CopyMemory lngIpAddrPtr, ByVal udtHost.hAddrList, 4
  450. CopyMemory lngHostAddress, ByVal lngIpAddrPtr, 4
  451. '
  452. 'Get a host name
  453. strHostName = StringFromPointer(udtHost.hName)
  454. '
  455. 'Call the PostGetHostEvent friend method of the objSocket
  456. 'to forward the retrieved information.
  457. objSocket.PostGetHostEvent wParam, lngHostAddress, strHostName
  458. '
  459. End If
  460. '
  461. 'The task to resolve the host name is completed, thus we don't
  462. 'need the allocated memory block anymore and corresponding items
  463. 'in the m_colMemoryBlocks and m_colResolvers collections as well.
  464. '
  465. 'Unlock the memory block
  466. Call GlobalUnlock(lngMemoryHandle)
  467. 'Free that memory
  468. Call GlobalFree(lngMemoryHandle)
  469. '
  470. 'Rremove the items from the collections
  471. m_colMemoryBlocks.Remove "S" & lngObjPointer
  472. m_colResolvers.Remove "R" & wParam
  473. '
  474. 'If there are no more resolving tasks in progress,
  475. 'destroy the collection objects to free resources.
  476. If m_colResolvers.Count = 0 Then
  477. Set m_colMemoryBlocks = Nothing
  478. Set m_colResolvers = Nothing
  479. End If
  480. '
  481. '---------------------------------------------------------------------
  482. 'Added: 17-OCT-2002
  483. Else
  484. 'Pass other messages to the original window procedure
  485. WindowProc = CallWindowProc(m_lngPreviousValue, hwnd, uMsg, wParam, lParam)
  486. '---------------------------------------------------------------------
  487. End If
  488. '
  489. EXIT_LABEL:
  490. '
  491. Exit Function
  492. '
  493. ERORR_HANDLER:
  494. '
  495. 'Err.Raise Err.Number, "CSocket.WindowProc", Err.Description
  496. '
  497. 'GoTo EXIT_LABEL
  498. '
  499. End Function
  500. Public Function RegisterSocket(ByVal lngSocketHandle As Long, ByVal lngObjectPointer As Long) As Boolean
  501. '********************************************************************************
  502. 'Author :Oleg Gdalevich
  503. 'Date/Time :17-12-2001
  504. 'Purpose :Adds the socket to the m_colSockets collection, and
  505. ' registers that socket with WSAAsyncSelect Winsock API
  506. ' function to receive network events for the socket.
  507. ' If this socket is the first one to be registered, the
  508. ' window and collection will be created in this function as well.
  509. 'Arguments :lngSocketHandle - the socket handle
  510. ' lngObjectPointer - pointer to an object, instance of the CSocket class
  511. 'Returns :If the argument is valid and no error occurred - True.
  512. '********************************************************************************
  513. '
  514. On Error GoTo ERROR_HANDLER 'Added: 04-JUNE-2002
  515. '
  516. Dim lngEvents As Long
  517. Dim lngRetValue As Long
  518. '
  519. If p_lngWindowHandle = 0 Then
  520. '
  521. 'We have no window to catch the network events.
  522. 'Create a new one.
  523. p_lngWindowHandle = CreateWinsockMessageWindow
  524. '
  525. If p_lngWindowHandle = 0 Then
  526. '
  527. 'Cannot create a new window.
  528. '---------------------------------------------------
  529. 'Added: 04-JUNE-2002
  530. '---------------------------------------------------
  531. 'Set the error info to pass to the caller subroutine
  532. Err.Number = sckOpCanceled
  533. Err.Description = "The operation was canceled."
  534. Err.Source = "MSocketSupport.RegisterSocket"
  535. '---------------------------------------------------
  536. 'Just exit to return False
  537. Exit Function
  538. '
  539. End If
  540. '
  541. End If
  542. '
  543. 'The m_colSockets collection holds information
  544. 'about all the sockets. If the current socket is
  545. 'the first one, create the collection object.
  546. If m_colSockets Is Nothing Then
  547. Set m_colSockets = New Collection
  548. 'Debug.Print "The m_colSockets is created"
  549. End If
  550. '
  551. 'Add a new item to the m_colSockets collection.
  552. 'The item key contains the socket handle, and the item's data
  553. 'is the pointer to the instance of the CSocket class.
  554. m_colSockets.Add lngObjectPointer, "S" & lngSocketHandle
  555. '
  556. 'The lngEvents variable contains a bitmask of events we are
  557. 'going to catch with the window callback function.
  558. lngEvents = FD_CONNECT Or FD_READ Or FD_WRITE Or FD_CLOSE Or FD_ACCEPT
  559. '
  560. 'Force the Winsock service to send the network event notifications
  561. 'to the window which handle is p_lngWindowHandle.
  562. lngRetValue = WSAAsyncSelect(lngSocketHandle, p_lngWindowHandle, p_lngWinsockMessage, lngEvents) 'Modified:04-MAR-2002
  563. '
  564. '------------------------------------------------------------------
  565. 'Added: 04-JUNE-2002
  566. '------------------------------------------------------------------
  567. If lngRetValue = SOCKET_ERROR Then
  568. '
  569. 'If the WSAAsyncSelect call failed this function must
  570. 'return False. In this case, the caller subroutine will
  571. 'raise an error. Let's pass the error info with the Err object.
  572. '
  573. RegisterSocket = False
  574. '
  575. Err.Number = Err.LastDllError
  576. Err.Description = GetErrorDescription(Err.LastDllError)
  577. Err.Source = "MSocketSupport.RegisterSocket"
  578. '
  579. Else
  580. '
  581. RegisterSocket = True
  582. '
  583. End If
  584. '-------------------------------------------------------------------
  585. 'Debug.Print lngSocketHandle & ": registered"
  586. '
  587. Exit Function 'Added: 04-JUNE-2002
  588. '
  589. ERROR_HANDLER: 'Added: 04-JUNE-2002
  590. '
  591. RegisterSocket = False 'Added: 04-JUNE-2002
  592. '
  593. End Function
  594. Public Function UnregisterSocket(ByVal lngSocketHandle As Long) As Boolean
  595. '********************************************************************************
  596. 'Author :Oleg Gdalevich
  597. 'Date/Time :17-12-2001
  598. 'Purpose :Removes the socket from the m_colSockets collection
  599. ' If it is the last socket in that collection, the window
  600. ' and colection will be destroyed as well.
  601. 'Returns :If the argument is valid and no error occurred - True.
  602. '********************************************************************************
  603. '
  604. If (lngSocketHandle = INVALID_SOCKET) Or (m_colSockets Is Nothing) Then
  605. '
  606. 'Something wrong with the caller of this function :)
  607. 'Return False
  608. Exit Function
  609. '
  610. End If
  611. '
  612. Call WSAAsyncSelect(lngSocketHandle, p_lngWindowHandle, 0&, 0&)
  613. '
  614. 'Remove the socket from the collection
  615. m_colSockets.Remove "S" & lngSocketHandle
  616. '
  617. UnregisterSocket = True
  618. '
  619. 'Debug.Print lngSocketHandle & ": unregistered"
  620. '
  621. If m_colSockets.Count = 0 Then
  622. '
  623. 'If there are no more sockets in the collection
  624. 'destroy the collection object and the window
  625. '
  626. Set m_colSockets = Nothing
  627. '
  628. 'Debug.Print "m_colSockets destroyed"
  629. '
  630. UnregisterSocket = DestroyWinsockMessageWindow
  631. '
  632. End If
  633. '
  634. End Function
  635. Public Function ResolveHost(strHostAddress As String, ByVal lngObjectPointer As Long) As Long
  636. '********************************************************************************
  637. 'Author :Oleg Gdalevich
  638. 'Date/Time :17-12-2001
  639. 'Purpose :Receives requests to resolve a host address from the CSocket class.
  640. 'Returns :If no errors occurred - ID of the request. Otherwise - 0.
  641. '********************************************************************************
  642. '
  643. 'Since this module is supposed to serve several instances of the
  644. 'CSocket class, this function can be called to start several
  645. 'resolving tasks that could be executed simultaneously. To
  646. 'distinguish the resolving tasks the m_colResolvers collection
  647. 'is used. The key of the collection's item contains a pointer to
  648. 'the instance of the CSocket class and the item's data is the
  649. 'Request ID, the value returned by the WSAAsyncGetHostByXXXX
  650. 'Winsock API function. So in order to get the pointer to the
  651. 'instance of the CSocket class by the task ID value the following
  652. 'line of code can be used:
  653. '
  654. 'lngObjPointer = CLng(m_colResolvers("R" & lngTaskID))
  655. '
  656. 'The WSAAsyncGetHostByXXXX function needs the buffer (the buf argument)
  657. 'where the data received from DNS server will be stored. We cannot use
  658. 'a local byte array for this purpose as this buffer must be available
  659. 'from another subroutine in this module - WindowProc, also we cannot
  660. 'use a module level array as several tasks can be executed simultaneously
  661. 'At least, we need a dynamic module level array of arrays - too complicated.
  662. 'I decided to use Windows API functions for allocation some memory for
  663. 'each resolving task: GlobalAlloc, GlobalLock, GlobalUnlock, and GlobalFree.
  664. '
  665. 'To distinguish those memory blocks, the m_colMemoryBlocks collection is
  666. 'used. The key of the collection's item contains value of the object
  667. 'pointer, and the item's value is a handle of the allocated memory
  668. 'block object, value returned by the GlobalAlloc function. So in order to
  669. 'get value of the handle of the allocated memory block object by the
  670. 'pointer to the instance of CSocket class we can use the following code:
  671. '
  672. 'lngMemoryHandle = CLng(m_colMemoryBlocks("S" & lngObjPointer))
  673. '
  674. 'Why do we need all this stuff?
  675. '
  676. 'The problem is that the callback function give us only the resolving task
  677. 'ID value, but we need information about:
  678. ' - where the data returned from the DNS server is stored
  679. ' - which instance of the CSocket class we need to post the info to
  680. '
  681. 'So, if we know the task ID value, we can find out the object pointer:
  682. ' lngObjPointer = CLng(m_colResolvers("R" & lngTaskID))
  683. '
  684. 'If we know the object pointer value we can find out where the data is strored:
  685. ' lngMemoryHandle = CLng(m_colMemoryBlocks("S" & lngObjPointer))
  686. '
  687. 'That's it. :))
  688. '
  689. Dim lngAddress As Long '32-bit host address
  690. Dim lngRequestID As Long 'value returned by WSAAsyncGetHostByXXX
  691. Dim lngMemoryHandle As Long 'handle of the allocated memory block object
  692. Dim lngMemoryPointer As Long 'address of the memory block
  693. '
  694. 'Allocate some memory
  695. lngMemoryHandle = GlobalAlloc(GMEM_FIXED, MAXGETHOSTSTRUCT)
  696. '
  697. If lngMemoryHandle > 0 Then
  698. '
  699. 'Lock the memory block just to get the address
  700. 'of that memory into the lngMemoryPointer variable
  701. lngMemoryPointer = GlobalLock(lngMemoryHandle)
  702. '
  703. If lngMemoryPointer = 0 Then
  704. '
  705. 'Memory allocation error
  706. Call GlobalFree(lngMemoryHandle)
  707. Exit Function
  708. '
  709. Else
  710. 'Unlock the memory block
  711. GlobalUnlock (lngMemoryHandle)
  712. '
  713. End If
  714. '
  715. Else
  716. '
  717. 'Memory allocation error
  718. Exit Function
  719. '
  720. End If
  721. '
  722. 'If this request is the first one, create the collections
  723. If m_colResolvers Is Nothing Then
  724. Set m_colMemoryBlocks = New Collection
  725. Set m_colResolvers = New Collection
  726. End If
  727. '
  728. '------------------------------------------------------------------
  729. 'Added: 09-JULY-2002
  730. '------------------------------------------------------------------
  731. Dim strKey As String
  732. '
  733. strKey = "S" & CStr(lngObjectPointer)
  734. '
  735. Call RemoveIfExists(strKey)
  736. '------------------------------------------------------------------
  737. 'Remember the memory block location
  738. m_colMemoryBlocks.Add lngMemoryHandle, strKey
  739. '
  740. '------------------------------------------------------------------
  741. 'Modified: 08-JULY-2002
  742. '------------------------------------------------------------------
  743. 'Here is a major change. Since version 1.0.6 (08-JULY-2002) the
  744. 'SCocket class doesn't try to resolve the IP address into a
  745. 'domain name while connecting.
  746. '------------------------------------------------------------------
  747. '
  748. 'Try to get 32-bit address
  749. 'lngAddress = inet_addr(strHostAddress)
  750. '
  751. 'If lngAddress = INADDR_NONE Then
  752. '
  753. 'If strHostAddress is not an IP address, try to resolve by name
  754. lngRequestID = WSAAsyncGetHostByName(p_lngWindowHandle, m_lngResolveMessage, strHostAddress, ByVal lngMemoryPointer, MAXGETHOSTSTRUCT) 'Modified: 04-MAR-2002
  755. '
  756. 'Else
  757. '
  758. 'strHostAddress contains an IP address, resolve by address to get a host name
  759. ' lngRequestID = WSAAsyncGetHostByAddr(p_lngWindowHandle, m_lngResolveMessage, lngAddress, 4&, AF_INET, ByVal lngMemoryPointer, MAXGETHOSTSTRUCT) 'Modified: 04-MAR-2002
  760. '
  761. 'End If
  762. '
  763. '------------------------------------------------------------------
  764. '
  765. If lngRequestID <> 0 Then
  766. '
  767. 'If the call of the WSAAsyncGetHostByXXXX is successful, the
  768. 'lngRequestID variable contains the task ID value.
  769. 'Remember it.
  770. m_colResolvers.Add lngObjectPointer, "R" & CStr(lngRequestID)
  771. '
  772. 'Return value
  773. ResolveHost = lngRequestID
  774. '
  775. Else
  776. '
  777. 'If the call of the WSAAsyncGetHostByXXXX is not successful,
  778. 'remove the item from the m_colMemoryBlocks collection.
  779. m_colMemoryBlocks.Remove ("S" & CStr(lngObjectPointer))
  780. '
  781. 'Free allocated memory block
  782. Call GlobalFree(lngMemoryHandle)
  783. '
  784. 'If there are no more resolving tasks in progress,
  785. 'destroy the collection objects.
  786. If m_colResolvers.Count = 0 Then
  787. Set m_colResolvers = Nothing
  788. Set m_colMemoryBlocks = Nothing
  789. End If
  790. '
  791. 'Set the error info.
  792. Err.Number = Err.LastDllError
  793. Err.Description = GetErrorDescription(Err.LastDllError)
  794. Err.Source = "MSocketSupport.ResolveHost"
  795. '
  796. End If
  797. '
  798. End Function
  799. Private Function CreateWinsockMessageWindow() As Long
  800. '********************************************************************************
  801. 'Author :Oleg Gdalevich
  802. 'Date/Time :17-12-2001
  803. 'Purpose :Creates a window to hook the winsock messages
  804. 'Returns :The window handle
  805. '********************************************************************************
  806. '
  807. 'Create a window. It will be used for hooking messages for registered
  808. 'sockets, and we'll not see this window as the ShowWindow is never called.
  809. p_lngWindowHandle = CreateWindowEx(0&, "STATIC", "SOCKET_WINDOW", 0&, 0&, 0&, 0&, 0&, 0&, 0&, App.hInstance, ByVal 0&)
  810. '
  811. If p_lngWindowHandle = 0 Then
  812. '
  813. 'I really don't know - is this possible? Probably - yes,
  814. 'due the lack of the system resources, for example.
  815. '
  816. 'In this case the function returns 0.
  817. '
  818. Else
  819. '
  820. 'Register a callback function for the window created a moment ago in this function
  821. 'm_lngPreviousValue - stores the returned value that is the pointer to the previous
  822. 'callback window function. We'll need this value to destroy the window.
  823. m_lngPreviousValue = SetWindowLong(p_lngWindowHandle, GWL_WNDPROC, AddressOf WindowProc)
  824. '
  825. 'Just to let the caller know that the function was executed successfully
  826. CreateWinsockMessageWindow = p_lngWindowHandle
  827. '
  828. 'Debug.Print "The window is created: " & p_lngWindowHandle
  829. '
  830. End If
  831. '
  832. End Function
  833. Private Function DestroyWinsockMessageWindow() As Boolean
  834. '********************************************************************************
  835. 'Author :Oleg Gdalevich
  836. 'Date/Time :17-12-2001
  837. 'Purpose :Destroyes the window
  838. 'Returns :If the window was destroyed successfully - True.
  839. '********************************************************************************
  840. '
  841. On Error GoTo ERR_HANDLER
  842. '
  843. 'Return the previous window procedure
  844. SetWindowLong p_lngWindowHandle, GWL_WNDPROC, m_lngPreviousValue
  845. 'Destroy the window
  846. DestroyWindow p_lngWindowHandle
  847. '
  848. 'Debug.Print "The window " & p_lngWindowHandle & " is destroyed"
  849. '
  850. 'Reset the window handle variable
  851. p_lngWindowHandle = 0
  852. 'If no errors occurred, the function returns True
  853. DestroyWinsockMessageWindow = True
  854. '
  855. ERR_HANDLER:
  856. End Function
  857. Private Function SocketObjectFromPointer(ByVal lngPointer As Long) As CSocket
  858. '
  859. Dim objSocket As CSocket
  860. '
  861. CopyMemory objSocket, lngPointer, 4&
  862. Set SocketObjectFromPointer = objSocket
  863. CopyMemory objSocket, 0&, 4&
  864. '
  865. End Function
  866. Private Function LoWord(lngValue As Long) As Long
  867. LoWord = (lngValue And &HFFFF&)
  868. End Function
  869. Private Function HiWord(lngValue As Long) As Long
  870. '
  871. If (lngValue And &H80000000) = &H80000000 Then
  872. HiWord = ((lngValue And &H7FFF0000) \ &H10000) Or &H8000&
  873. Else
  874. HiWord = (lngValue And &HFFFF0000) \ &H10000
  875. End If
  876. '
  877. End Function
  878. Public Function GetErrorDescription(ByVal lngErrorCode As Long) As String
  879. '
  880. Dim strDesc As String
  881. '
  882. Select Case lngErrorCode
  883. '
  884. Case WSAEACCES
  885. strDesc = "Permission denied."
  886. Case WSAEADDRINUSE
  887. strDesc = "Address already in use."
  888. Case WSAEADDRNOTAVAIL
  889. strDesc = "Cannot assign requested address."
  890. Case WSAEAFNOSUPPORT
  891. strDesc = "Address family not supported by protocol family."
  892. Case WSAEALREADY
  893. strDesc = "Operation already in progress."
  894. Case WSAECONNABORTED
  895. strDesc = "Software caused connection abort."
  896. Case WSAECONNREFUSED
  897. strDesc = "Connection refused."
  898. Case WSAECONNRESET
  899. strDesc = "Connection reset by peer."
  900. Case WSAEDESTADDRREQ
  901. strDesc = "Destination address required."
  902. Case WSAEFAULT
  903. strDesc = "Bad address."
  904. Case WSAEHOSTDOWN
  905. strDesc = "Host is down."
  906. Case WSAEHOSTUNREACH
  907. strDesc = "No route to host."
  908. Case WSAEINPROGRESS
  909. strDesc = "Operation now in progress."
  910. Case WSAEINTR
  911. strDesc = "Interrupted function call."
  912. Case WSAEINVAL
  913. strDesc = "Invalid argument."
  914. Case WSAEISCONN
  915. strDesc = "Socket is already connected."
  916. Case WSAEMFILE
  917. strDesc = "Too many open files."
  918. Case WSAEMSGSIZE
  919. strDesc = "Message too long."
  920. Case WSAENETDOWN
  921. strDesc = "Network is down."
  922. Case WSAENETRESET
  923. strDesc = "Network dropped connection on reset."
  924. Case WSAENETUNREACH
  925. strDesc = "Network is unreachable."
  926. Case WSAENOBUFS
  927. strDesc = "No buffer space available."
  928. Case WSAENOPROTOOPT
  929. strDesc = "Bad protocol option."
  930. Case WSAENOTCONN
  931. strDesc = "Socket is not connected."
  932. Case WSAENOTSOCK
  933. strDesc = "Socket operation on nonsocket."
  934. Case WSAEOPNOTSUPP
  935. strDesc = "Operation not supported."
  936. Case WSAEPFNOSUPPORT
  937. strDesc = "Protocol family not supported."
  938. Case WSAEPROCLIM
  939. strDesc = "Too many processes."
  940. Case WSAEPROTONOSUPPORT
  941. strDesc = "Protocol not supported."
  942. Case WSAEPROTOTYPE
  943. strDesc = "Protocol wrong type for socket."
  944. Case WSAESHUTDOWN
  945. strDesc = "Cannot send after socket shutdown."
  946. Case WSAESOCKTNOSUPPORT
  947. strDesc = "Socket type not supported."
  948. Case WSAETIMEDOUT
  949. strDesc = "Connection timed out."
  950. Case WSATYPE_NOT_FOUND
  951. strDesc = "Class type not found."
  952. Case WSAEWOULDBLOCK
  953. strDesc = "Resource temporarily unavailable."
  954. Case WSAHOST_NOT_FOUND
  955. strDesc = "Host not found."
  956. Case WSANOTINITIALISED
  957. strDesc = "Successful WSAStartup not yet performed."
  958. Case WSANO_DATA
  959. strDesc = "Valid name, no data record of requested type."
  960. Case WSANO_RECOVERY
  961. strDesc = "This is a nonrecoverable error."
  962. Case WSASYSCALLFAILURE
  963. strDesc = "System call failure."
  964. Case WSASYSNOTREADY
  965. strDesc = "Network subsystem is unavailable."
  966. Case WSATRY_AGAIN
  967. strDesc = "Nonauthoritative host not found."
  968. Case WSAVERNOTSUPPORTED
  969. strDesc = "Winsock.dll version out of range."
  970. Case WSAEDISCON
  971. strDesc = "Graceful shutdown in progress."
  972. Case Else
  973. strDesc = "Unknown error."
  974. End Select
  975. '
  976. GetErrorDescription = strDesc
  977. '
  978. End Function
  979. Public Function InitWinsockService() As Long
  980. '
  981. 'This functon does two things; it initializes the Winsock
  982. 'service and returns value of maximum size of the UDP
  983. 'message. Since this module is supposed to serve multiple
  984. 'instances of the CSocket class, this function can be
  985. 'called several times. But we need to call the WSAStartup
  986. 'Winsock API function only once when the first instance of
  987. 'the CSocket class is created.
  988. '
  989. Dim lngRetVal As Long 'value returned by WSAStartup
  990. Dim strErrorMsg As String 'error description string
  991. Dim udtWinsockData As WSAData 'structure to pass to WSAStartup as an argument
  992. '
  993. If Not m_blnWinsockInit Then
  994. '
  995. 'start up winsock service
  996. lngRetVal = WSAStartup(&H101, udtWinsockData)
  997. '
  998. If lngRetVal <> 0 Then
  999. '
  1000. 'The system cannot load the Winsock library.
  1001. '
  1002. Select Case lngRetVal
  1003. Case WSASYSNOTREADY
  1004. strErrorMsg = "The underlying network subsystem is not " & _
  1005. "ready for network communication."
  1006. Case WSAVERNOTSUPPORTED
  1007. strErrorMsg = "The version of Windows Sockets API support " & _
  1008. "requested is not provided by this particular " & _
  1009. "Windows Sockets implementation."
  1010. Case WSAEINVAL
  1011. strErrorMsg = "The Windows Sockets version specified by the " & _
  1012. "application is not supported by this DLL."
  1013. End Select
  1014. '
  1015. Err.Raise Err.LastDllError, "MSocketSupport.InitWinsockService", strErrorMsg
  1016. '
  1017. Else
  1018. '
  1019. 'The Winsock library is loaded successfully.
  1020. '
  1021. m_blnWinsockInit = True
  1022. '
  1023. 'This function returns returns value of
  1024. 'maximum size of the UDP message
  1025. m_lngMaxMsgSize = IntegerToUnsigned(udtWinsockData.iMaxUdpDg)
  1026. InitWinsockService = m_lngMaxMsgSize
  1027. '
  1028. m_lngResolveMessage = RegisterWindowMessage(App.EXEName & ".ResolveMessage") 'Added: 04-MAR-2002
  1029. p_lngWinsockMessage = RegisterWindowMessage(App.EXEName & ".WinsockMessage") 'Added: 04-MAR-2002
  1030. '
  1031. '
  1032. End If
  1033. '
  1034. Else
  1035. '
  1036. 'If this function has been called before by another
  1037. 'instance of the CSocket class, the code to init the
  1038. 'Winsock service must not be executed, but the function
  1039. 'returns maximum size of the UDP message anyway.
  1040. InitWinsockService = m_lngMaxMsgSize
  1041. '
  1042. End If
  1043. '
  1044. End Function
  1045. Public Sub CleanupWinsock()
  1046. '********************************************************************************
  1047. 'This subroutine is called from the Class_Terminate() event
  1048. 'procedure of any instance of the CSocket class. But the WSACleanup
  1049. 'Winsock API function is called only if the calling object is the
  1050. 'last instance of the CSocket class within the current process.
  1051. '********************************************************************************
  1052. '
  1053. 'If the Winsock library was loaded
  1054. 'before and there are no more sockets.
  1055. If m_blnWinsockInit And m_colSockets Is Nothing Then
  1056. '
  1057. 'Unload library and free the system resources
  1058. Call WSACleanup
  1059. '
  1060. 'Turn off the m_blnWinsockInit flag variable
  1061. m_blnWinsockInit = False
  1062. '
  1063. End If
  1064. '
  1065. End Sub
  1066. Public Function StringFromPointer(ByVal lPointer As Long) As String
  1067. '
  1068. Dim strTemp As String
  1069. Dim lRetVal As Long
  1070. '
  1071. 'prepare the strTemp buffer
  1072. strTemp = String$(lstrlen(ByVal lPointer), 0)
  1073. '
  1074. 'copy the string into the strTemp buffer
  1075. lRetVal = lstrcpy(ByVal strTemp, ByVal lPointer)
  1076. '
  1077. 'return a string
  1078. If lRetVal Then StringFromPointer = strTemp
  1079. '
  1080. End Function
  1081. Public Function UnsignedToLong(Value As Double) As Long
  1082. '
  1083. 'The function takes a Double containing a value in the 
  1084. 'range of an unsigned Long and returns a Long that you 
  1085. 'can pass to an API that requires an unsigned Long
  1086. '
  1087. If Value < 0 Or Value >= OFFSET_4 Then Error 6 ' Overflow
  1088. '
  1089. If Value <= MAXINT_4 Then
  1090. UnsignedToLong = Value
  1091. Else
  1092. UnsignedToLong = Value - OFFSET_4
  1093. End If
  1094. '
  1095. End Function
  1096. Public Function LongToUnsigned(Value As Long) As Double
  1097. '
  1098. 'The function takes an unsigned Long from an API and 
  1099. 'converts it to a Double for display or arithmetic purposes
  1100. '
  1101. If Value < 0 Then
  1102. LongToUnsigned = Value + OFFSET_4
  1103. Else
  1104. LongToUnsigned = Value
  1105. End If
  1106. '
  1107. End Function
  1108. Public Function UnsignedToInteger(Value As Long) As Integer
  1109. '
  1110. 'The function takes a Long containing a value in the range 
  1111. 'of an unsigned Integer and returns an Integer that you 
  1112. 'can pass to an API that requires an unsigned Integer
  1113. '
  1114. If Value < 0 Or Value >= OFFSET_2 Then Error 6 ' Overflow
  1115. '
  1116. If Value <= MAXINT_2 Then
  1117. UnsignedToInteger = Value
  1118. Else
  1119. UnsignedToInteger = Value - OFFSET_2
  1120. End If
  1121. '
  1122. End Function
  1123. Public Function IntegerToUnsigned(Value As Integer) As Long
  1124. '
  1125. 'The function takes an unsigned Integer from and API and 
  1126. 'converts it to a Long for display or arithmetic purposes
  1127. '
  1128. If Value < 0 Then
  1129. IntegerToUnsigned = Value + OFFSET_2
  1130. Else
  1131. IntegerToUnsigned = Value
  1132. En

Large files files are truncated, but you can click here to view the full file