Advertisement
Guest User

Cocus class

a guest
Apr 3rd, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.76 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Private Type COMSTAT
  4. fCtsHold As Long
  5. fDsrHold As Long
  6. fRlsdHold As Long
  7. fXoffHold As Long
  8. fXoffSent As Long
  9. fEof As Long
  10. fTxim As Long
  11. fReserved As Long
  12. cbInQue As Long
  13. cbOutQue As Long
  14. End Type
  15.  
  16. Private Type COMMTIMEOUTS
  17. ReadIntervalTimeout As Long
  18. ReadTotalTimeoutMultiplier As Long
  19. ReadTotalTimeoutConstant As Long
  20. WriteTotalTimeoutMultiplier As Long
  21. WriteTotalTimeoutConstant As Long
  22. End Type
  23.  
  24. Private Type DCB
  25. DCBlength As Long
  26. BaudRate As Long
  27. fBinary As Long
  28. fParity As Long
  29. fOutxCtsFlow As Long
  30. fOutxDsrFlow As Long
  31. fDtrControl As Long
  32. fDsrSensitivity As Long
  33. fTXContinueOnXoff As Long
  34. fOutX As Long
  35. fInX As Long
  36. fErrorChar As Long
  37. fNull As Long
  38. fRtsControl As Long
  39. fAbortOnError As Long
  40. fDummy2 As Long
  41. wReserved As Integer
  42. XonLim As Integer
  43. XoffLim As Integer
  44. ByteSize As Byte
  45. Parity As Byte
  46. StopBits As Byte
  47. XonChar As Byte
  48. XoffChar As Byte
  49. ErrorChar As Byte
  50. EofChar As Byte
  51. EvtChar As Byte
  52. End Type
  53.  
  54. Private Type OVERLAPPED
  55. Internal As Long
  56. InternalHigh As Long
  57. offset As Long
  58. OffsetHigh As Long
  59. hEvent As Long
  60. End Type
  61.  
  62. Private Type SECURITY_ATTRIBUTES
  63. nLength As Long
  64. lpSecurityDescriptor As Long
  65. bInheritHandle As Long
  66. End Type
  67.  
  68. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  69. Private Declare Function GetLastError Lib "kernel32" () As Long
  70. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
  71. Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
  72. Private Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
  73. Private Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
  74. Private Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As DCB) As Long
  75. Private Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
  76. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  77. Private Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
  78.  
  79. Private Declare Sub RtlMoveMemory Lib "kernel32" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
  80. Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
  81. Private Declare Function VirtualFree Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
  82. Private Declare Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As Long
  83. Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
  84. Private Declare Function LoadLibrary Lib "kernel32.dll" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  85. Private Declare Function IsBadCodePtr Lib "kernel32" (ByVal lpfn As Long) As Long
  86. Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
  87. Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
  88. 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
  89. Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
  90.  
  91. Private c_strComSettings As String
  92. Private c_lngComPort As Long
  93. Private c_lngHandle As Long
  94. Private c_lngTimerDelay As Long
  95. Private c_blnTimerRunning As Boolean
  96. Private c_lMem As Long
  97. Private c_lvThunk() As Long
  98. Private c_lhWnd As Long
  99.  
  100. Public Event DataReceived(ByVal strData As String)
  101.  
  102.  
  103. Public Property Let ComPort(ByVal lngNewComPort As Long)
  104. If Not (c_lngHandle = -1) Then
  105. Call KillCom
  106. c_lngComPort = lngNewComPort
  107. Call InitCom
  108. Else
  109. c_lngComPort = lngNewComPort
  110. End If
  111. End Property
  112.  
  113. Public Property Get ComPort() As Long
  114. ComPort = c_lngComPort
  115. End Property
  116.  
  117. Public Property Let ComSettings(ByVal strNewComSettings As String)
  118. If Not (c_lngHandle = -1) Then
  119. Call KillCom
  120. c_strComSettings = LCase$(strNewComSettings)
  121. Call InitCom
  122. Else
  123. c_strComSettings = LCase$(strNewComSettings)
  124. End If
  125. End Property
  126.  
  127. Public Property Get ComSettings() As String
  128. ComSettings = c_strComSettings
  129. End Property
  130.  
  131. Public Property Let TimerDelay(ByVal lngNewTimerDelay As Long)
  132. If Not (c_lngHandle = -1) Then
  133. If c_blnTimerRunning Then
  134. Call StopTimer
  135. c_lngTimerDelay = lngNewTimerDelay
  136. Call StartTimer
  137. Else
  138. c_lngTimerDelay = lngNewTimerDelay
  139. End If
  140. Else
  141. c_lngTimerDelay = lngNewTimerDelay
  142. End If
  143. End Property
  144.  
  145. Public Property Get TimerDelay() As Long
  146. TimerDelay = c_lngTimerDelay
  147. End Property
  148.  
  149. Public Property Let TimerEnabled(ByVal blnNewTimerEnabled As Boolean)
  150. If Not (c_lngHandle = -1) Then
  151. If c_blnTimerRunning And Not (blnNewTimerEnabled) Then
  152. Call StopTimer
  153. ElseIf Not (c_blnTimerRunning) And blnNewTimerEnabled Then
  154. Call StartTimer
  155. End If
  156. End If
  157. End Property
  158.  
  159. Public Property Get TimerEnabled() As Boolean
  160. TimerEnabled = c_blnTimerRunning
  161. End Property
  162.  
  163.  
  164. Public Function InitCom() As Boolean
  165. Dim strCom As String
  166. Dim coTimeouts As COMMTIMEOUTS
  167. Dim dcbCom As DCB
  168.  
  169. strCom = "COM" & CStr(c_lngComPort) & ":"
  170.  
  171. c_lngHandle = CreateFile(strCom, &HC0000000, 0, 0&, &H3, 0, 0)
  172.  
  173. If c_lngHandle = -1 Then
  174. Exit Function
  175. End If
  176.  
  177. With coTimeouts
  178. .ReadIntervalTimeout = 20
  179. .ReadTotalTimeoutConstant = 1
  180. .ReadTotalTimeoutMultiplier = 1
  181. .WriteTotalTimeoutConstant = 10
  182. .WriteTotalTimeoutMultiplier = 1
  183. End With
  184.  
  185. If SetCommTimeouts(c_lngHandle, coTimeouts) = -1 Then
  186. Call CloseHandle(c_lngHandle)
  187. Exit Function
  188. End If
  189.  
  190. If BuildCommDCB(c_strComSettings, dcbCom) = -1 Then
  191. Call CloseHandle(c_lngHandle)
  192. Exit Function
  193. End If
  194.  
  195. If SetCommState(c_lngHandle, dcbCom) = -1 Then
  196. Call CloseHandle(c_lngHandle)
  197. Exit Function
  198. End If
  199.  
  200. Me.TimerEnabled = True
  201. InitCom = True
  202. End Function
  203.  
  204. Public Function KillCom() As Boolean
  205. If Not (c_lngHandle = -1) Then
  206. Call CloseHandle(c_lngHandle)
  207. If c_blnTimerRunning Then
  208. Call StopTimer
  209. End If
  210. c_lngHandle = -1
  211. KillCom = True
  212. End If
  213. End Function
  214.  
  215. Public Function ReadCom() As String
  216. Dim bvRead() As Byte
  217. Dim lngBytesReaded As Long
  218.  
  219. If Not (c_lngHandle = -1) Then
  220. ReDim bvRead(255)
  221.  
  222. Call ReadFile(c_lngHandle, bvRead(0), 255, lngBytesReaded, ByVal 0)
  223.  
  224. If lngBytesReaded > 0 Then
  225. ReDim Preserve bvRead(lngBytesReaded - 1)
  226. ReadCom = StrConv(bvRead, vbUnicode)
  227.  
  228. Call FlushFileBuffers(c_lngHandle)
  229. End If
  230. End If
  231. End Function
  232.  
  233. Public Function WriteCom(ByVal strWrite As String) As Boolean
  234. Dim bvWrite() As Byte
  235. Dim strWriteTemp As String
  236. Dim lngBytesTotal As Long
  237. Dim lngBytesWrited As Long
  238. Dim lngBytesTotalWrited As Long
  239.  
  240. If Not (c_lngHandle = -1) Then
  241. lngBytesTotal = Len(strWrite)
  242. Do
  243. If Len(strWrite) > 255 Then
  244. strWriteTemp = Left$(strWrite, 255)
  245. strWrite = Mid$(strWrite, 255)
  246. Else
  247. strWriteTemp = strWrite
  248. strWrite = ""
  249. End If
  250.  
  251. ReDim bvWrite(Len(strWriteTemp) - 1)
  252. bvWrite = StrConv(strWriteTemp & vbNullChar, vbFromUnicode)
  253.  
  254. Call WriteFile(c_lngHandle, bvWrite(0), Len(strWriteTemp), lngBytesWrited, ByVal 0)
  255.  
  256. lngBytesTotalWrited = lngBytesTotalWrited + lngBytesWrited
  257.  
  258. If Len(strWrite) = 0 Then Exit Do
  259. Loop
  260.  
  261. WriteCom = (lngBytesTotalWrited = lngBytesTotal)
  262. End If
  263. End Function
  264.  
  265. Private Sub Class_Initialize()
  266. c_strComSettings = "9600,n,8,1"
  267. c_lngComPort = 1
  268. c_lngHandle = -1
  269. c_lngTimerDelay = 1000
  270.  
  271. c_lhWnd = CreateWindowEx(0, "static", vbNullString, _
  272. 0, 0, 0, 0, 0, 0, 0, App.hInstance, 0)
  273. End Sub
  274.  
  275. Private Sub Class_Terminate()
  276. If Not (c_lngHandle = -1) Then
  277. Call KillCom
  278. End If
  279.  
  280. Call StopTimer
  281. Call VirtualFree(c_lMem, 0, &H8000&)
  282. Call DestroyWindow(c_lhWnd)
  283. End Sub
  284.  
  285. Private Function StartTimer() As Boolean
  286. Call StopTimer
  287. StartTimer = Not (SetTimer(c_lhWnd, ObjPtr(Me), c_lngTimerDelay, Timer_AddressOf) = 0)
  288. c_blnTimerRunning = StartTimer
  289. End Function
  290.  
  291. Private Function StopTimer() As Boolean
  292. StopTimer = Not (KillTimer(c_lhWnd, ObjPtr(Me)) = 0)
  293. c_blnTimerRunning = Not StopTimer
  294. End Function
  295.  
  296. Private Function Timer_AddressOf() As Long
  297. Dim nAddr As Long
  298. Dim nThunkNo As Long
  299.  
  300. nAddr = zAddressOf
  301. If c_lMem = 0 Then
  302. ReDim c_lvThunk(0 To 21) As Long
  303. c_lMem = VirtualAlloc(c_lMem, 88, &H1000&, &H40&)
  304. End If
  305. If c_lvThunk(0) = 0 Then
  306. c_lvThunk(3) = GetProcAddress(GetModuleHandleA("kernel32"), "IsBadCodePtr")
  307. c_lvThunk(4) = &HBB60E089
  308. c_lvThunk(5) = VarPtr(c_lvThunk(0))
  309. c_lvThunk(6) = &H73FFC589: c_lvThunk(7) = &HC53FF04: c_lvThunk(8) = &H7B831F75: c_lvThunk(9) = &H20750008: c_lvThunk(10) = &HE883E889: c_lvThunk(11) = &HB9905004: c_lvThunk(13) = &H74FF06E3: c_lvThunk(14) = &HFAE2008D: c_lvThunk(15) = &H53FF33FF: c_lvThunk(16) = &HC2906104: c_lvThunk(18) = &H830853FF: c_lvThunk(19) = &HD87401F8: c_lvThunk(20) = &H4589C031: c_lvThunk(21) = &HEAEBFC
  310. End If
  311. c_lvThunk(0) = ObjPtr(Me)
  312. c_lvThunk(1) = nAddr
  313. c_lvThunk(2) = GetProcAddress(GetModuleHandleA("vba6"), "EbMode")
  314. c_lvThunk(12) = 4
  315. c_lvThunk(17) = 16
  316. nAddr = c_lMem + (0 * 88)
  317. RtlMoveMemory nAddr, VarPtr(c_lvThunk(0)), 88
  318. Timer_AddressOf = nAddr + 16
  319. End Function
  320.  
  321. Private Function zAddressOf() As Long
  322. Dim bSub As Byte
  323. Dim bVal As Byte
  324. Dim nAddr As Long
  325. Dim i As Long
  326. Dim J As Long
  327.  
  328. RtlMoveMemory VarPtr(nAddr), ObjPtr(Me), 4
  329. If zProbe(nAddr + &H1C, i, bSub) Then
  330. i = i + 4: J = i + 1024
  331. Do While i < J
  332. RtlMoveMemory VarPtr(nAddr), i, 4
  333. If IsBadCodePtr(nAddr) Then
  334. RtlMoveMemory VarPtr(zAddressOf), i - (1 * 4), 4
  335. Exit Do
  336. End If
  337. RtlMoveMemory VarPtr(bVal), nAddr, 1
  338. If bVal <> bSub Then
  339. RtlMoveMemory VarPtr(zAddressOf), i - (1 * 4), 4
  340. Exit Do
  341. End If
  342. i = i + 4
  343. Loop
  344. End If
  345. End Function
  346.  
  347. Private Function zProbe(ByVal nStart As Long, ByRef nMethod As Long, ByRef bSub As Byte) As Boolean
  348. Dim bVal As Byte
  349. Dim nAddr As Long
  350. Dim nLimit As Long
  351. Dim nEntry As Long
  352.  
  353. nAddr = nStart
  354. nLimit = nAddr + 32
  355. Do While nAddr < nLimit
  356. RtlMoveMemory VarPtr(nEntry), nAddr, 4
  357. If nEntry <> 0 Then
  358. RtlMoveMemory VarPtr(bVal), nEntry, 1
  359. If bVal = &H33 Or bVal = &HE9 Then
  360. nMethod = nAddr: bSub = bVal
  361. zProbe = True: Exit Function
  362. End If
  363. End If
  364. nAddr = nAddr + 4
  365. Loop
  366. End Function
  367.  
  368. Private Function TimerProc(ByVal lv1 As Long, ByVal lv2 As Long, ByVal lv3 As Long, ByVal lv4 As Long) As Long
  369. Dim strRead As String
  370.  
  371. strRead = ReadCom
  372. If Len(strRead) > 0 Then
  373. RaiseEvent DataReceived(strRead)
  374. End If
  375. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement