
' Jose includes used

#INCLUDE "cCTSQL/cCTSQLiteClient.bi"
#INCLUDE ONCE "Afx/CWindow.inc"
USING Afx

dim shared HWND_MAIN_STARTBUTTON as HWnd

CONST IDC_MAIN_STARTBUTTON = 28000


DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL szCmdLine AS ZSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG

   END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

' // Forward declaration
DECLARE FUNCTION WndProc (BYVAL hWnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG

   ' // Create the main window
   DIM pWindow AS CWindow
   pWindow.Create(NULL, "", @WndProc)
   pWindow.SetClientSize(300, 210)
   pWindow.Center

   ' // Add and show a button control 
   
   HWND_MAIN_STARTBUTTON = pWindow.AddControl("Button", pWindow.hWindow, IDC_MAIN_STARTBUTTON, "&Start", 105, 90, 75, 28, BS_PUSHBUTTON,-1)
   ShowWindow( HWND_MAIN_STARTBUTTON, SW_SHOW)   
   
   ' // Dispatch Windows messages
   
   FUNCTION = pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main window callback procedure
' ========================================================================================
FUNCTION WndProc (BYVAL hWnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

Dim oClient           as cCTSQLiteClient
Dim iErrorCode        as Long
Dim sErrorDescription as String
Dim sReceive          as String
Dim sSend             as String = "Client Echo Request..."

   SELECT CASE uMsg

      CASE WM_COMMAND
         
         If (LoWord(wParam) = IDC_MAIN_STARTBUTTON) Andalso (HiWord(wParam) = BN_CLICKED) Then

            oClient.ClientSharedKey(0,1,2,3,4,5)

            If oClient.FindServer(iErrorCode,sErrorDescription) = True Then
   
               Print "Server found at " + oClient.ServerIP + " on port " + Str(oClient.ServerPort)
               oClient.OpenServerConnection(iErrorCode,sErrorDescription)
               print "Server version=" + oClient.ServerVersion
               
' Server is now waiting for a request               
               
               Print "Client sending: " + sSend
               oClient.RequestSendAndReceive(sSend,sReceive,iErrorCode,sErrorDescription)
               Print "Server response: " + sReceive 
      
               oClient.CloseServerConnection()
      
            Else
   
               Print "Server not found: " + Str(iErrorCode) + " - " + sErrorDescription

            End If

         end if

      CASE WM_NOTIFY

      CASE WM_SIZE

    	CASE WM_DESTROY
         ' // End the application by sending an WM_QUIT message
         PostQuitMessage(0)
         EXIT FUNCTION

   END SELECT

   ' // Default processing of Windows messages
   FUNCTION = DefWindowProc(hWnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================