Attribute VB_Name = "Module1"
Option Explicit

Private Declare Function GetForegroundWindow Lib "user32" () As Long

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const GWL_WNDPROC = (-4)

 
'레지스터 윈도우 메세지 사용
 Public Const RECEIVE_MSG = "WOOSIM_PRT_OK"
 Public UWM_RECEIVE_MSG As Long
 Public ghWnd As Long

 '***************************************************************************
 '           CONST DEFINE
 '***************************************************************************
    
    Public Const ACK As Integer = &H6
    Public Const NACK As Integer = &H15
    Public Const ENQ As Integer = &H5
    Public Const DATA As Integer = &H44
    Public Const STATUS_REQUST As Integer = &H51
    Public Const STATUS_ANSWER As Integer = &H53
    Public Const EOT As Integer = &H4
    Public Const ETX As Integer = &H3
    Public Const CAN As Integer = &H18
    Public Const MSR_FAIL As Integer = &H4D
    
    ' Return Message
    Private Const SUCCESS As Integer = 1
    Private Const SOCKET_ERROR As Integer = -1
    Private Const CONNECT_FAIL As Integer = -2
    Private Const ALREADY_CONNECTED As Integer = -3

    Private Const NOT_OPEN_THE_PORT As Integer = -11
    Private Const BUFFER_OVERFLOW As Integer = -12
    Private Const OPERATOR_ERROR As Integer = -13
    
    'Barcode type
    Public Const UPCA As Integer = 65
    Public Const UPCE As Integer = 66
    Public Const EAN13 As Integer = 67
    Public Const EAN8 As Integer = 68
    Public Const CODE39 As Integer = 69
    Public Const ITF As Integer = 70
    Public Const CODABAR As Integer = 71
    Public Const CODE93 As Integer = 72
    Public Const CODE128 As Integer = 73

    'print width
    Private Const INCH2 As Integer = 384
    Private Const INCH3 As Integer = 576
    Private Const INCH4 As Integer = 832
    
    Public Declare Function InitPrinter Lib "WoosimPrinter.dll" (ByVal sPortName As String, ByVal BaudRate As Long, ByVal Protocol As Boolean) As Integer
    Public Declare Function ControlCommand Lib "WoosimPrinter.dll" (ByVal Cmd As String, ByVal Length As Long) As Long
    Public Declare Function SaveSpool Lib "WoosimPrinter.dll" (ByVal strData As String, ByVal FontValue As Integer, ByVal Emphasis As Boolean) As Integer
    Public Declare Function PrintSpool Lib "WoosimPrinter.dll" (ByVal Delete_Spool As Boolean) As Integer
    Public Declare Function CardRead Lib "WoosimPrinter.dll" (ByVal Track2 As Boolean)
    Public Declare Function CardCancel Lib "WoosimPrinter.dll" ()
    Public Declare Function ClosePrinter Lib "WoosimPrinter.dll" () As Boolean
    Public Declare Function BmpSaveSpool Lib "WoosimPrinter.dll" (ByVal sBmpFilePath As String, ByVal iXPosition As Long, ByVal iYPosition As Long) As Long
    Public Declare Function InitWlanPrinter Lib "WoosimPrinter.dll" (ByVal sIP_ADDR As String, ByVal iPortNum As Integer, ByVal nTimeout As Integer, ByVal bProtocol As Boolean) As Integer
    Public Declare Function BarcodeSaveSpool Lib "WoosimPrinter.dll" (ByVal ucBarcodeType As Integer, ByVal ucBarWidth As Integer, ByVal ucBarHeight As Integer, ByVal HRI_On As Boolean, ByVal BarcodeData As String, ByVal iPrtWidth As Integer, ByVal iPrtHeight As Integer, ByVal iXPos As Integer) As Integer
    
    '레지스터 윈도우 메세지 선언
    Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
       
    Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
    
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
        
Public Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    If hwnd = GetForegroundWindow Then   '### 후킹하려는 윈도우와 활성화 되어있는 윈도우가 같다면...
        Select Case uMsg
            Case UWM_RECEIVE_MSG
                Select Case wParam
                    Case DATA
                        'MsgBox "데이터"
                         Form1.Label4.Caption = "DATA"
                         ' 이부분에서 들어오는 카드값 lParam 값을 처리해야합니다.
                         
                         Dim strTemp As String
                         Dim lRetVal As Long
                         Dim CardData() As Byte
                         
                         lRetVal = lstrlen(ByVal lParam)
                         
                         'Form1.Text4.Text = lRetVal
                                                
                         ReDim CardData(lRetVal) As Byte
                         
                         Dim i As Long
                                                   
                            CopyMemory CardData(0), ByVal lParam, lRetVal
                        
                            For i = 0 To lRetVal
                                                            
                              strTemp = strTemp & Chr(CardData(i))
                              
                              Form1.Text5.Text = strTemp
                            
                            Next i
                    
                    
                    Case MSR_FAIL
                    Form1.Label4.Caption = "MSR FAIL"
                      'MsgBox "리딩 실패"
                End Select
        End Select
    End If

    WndProc = CallWindowProc(ghWnd, hwnd, uMsg, wParam, lParam)
 
    
End Function
'*****************************************************************************
' 메세지후킹체크
'*****************************************************************************
Public Sub Hook(hwnd As Long)
   ' MsgBox "후크"
    UWM_RECEIVE_MSG = RegisterWindowMessage(RECEIVE_MSG)
    ghWnd = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)
End Sub

Public Sub UnHook(hwnd As Long)
    Dim lngret As Long
    If ghWnd <> 0 Then
        lngret = SetWindowLong(hwnd, GWL_WNDPROC, ghWnd)
    End If
End Sub


