Announcement

Collapse
No announcement yet.

Making a Program Service

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Making a Program Service

    I have a application that connects to a DB2 or SQl database that
    writes records to the Database. This application runs on a server.
    I would like for the application to start a services. Does anyone
    have any code to show me how this is done.

    Thanks....


    ------------------
    mailto:[email protected][email protected]</A>
    mailto:[email protected][email protected]</A>

  • #2
    these should get you started
    http://www.powerbasic.com/support/pb...ead.php?t=3889 http://www.powerbasic.com/support/pb...ad.php?t=22982

    Comment


    • #3
      I am using the Program with the Command line options. It is starting
      the CIMservice as a service not my program SQLupdate.exe

      Ex cimservice /INSTAll

      How do I get it to start SQLupdate.exe as the Services, Not the program
      example fron the Forum.

      Thanks....

      ------------------
      mailto:[email protected][email protected]</A>
      mailto:[email protected][email protected]</A>

      Comment


      • #4
        net start <service name>
        net stop <service name>

        or from the services tool in control panel

        or if set to automatic, after a reboot

        ------------------
        Paul Dwyer
        Network Engineer
        Aussie in Tokyo

        Comment


        • #5
          joseph,
          following code will start/stop the service "remoteregistry"
          all credit goes to jeroen brouwers
          see http://www.powerbasic.com/support/pb...ead.php?t=7047

          pierre

          Code:
          #compile exe  '#cc 3.02 dos-box#
          #include "win32api.inc"
          #include "wininet.inc"
          #include "psapi.inc"
            
          global debug        as double
          ' ------------------------------------------------------------------------
          declare function servicestop_part2(hsmanager as long, hservice as long, servicename as asciiz, tgp as long) as long
          declare function stopdependantservices(hsmanager as long, hservice as long, servicename as asciiz) as long
          '______________________________________________________________________________
            
          function logresults (s as string) as long
           print s
          end function
          '______________________________________________________________________________
            
          function servicestatus(computername as asciiz, servicename as asciiz) as long
           dim servicestat    as service_status
           dim hsmanager      as long
           dim hservice       as long
           dim hservicestatus as long
            
           on error resume next
           function = 0
            
           hsmanager = openscmanager(computername, $services_active_database, %sc_manager_all_access)
           if hsmanager <> 0 then
             hservice = openservice(hsmanager, servicename, %service_all_access ) ')
             if hservice <> 0 then
               hservicestatus = queryservicestatus(hservice, servicestat)
               function = servicestat.dwcurrentstate
               closeservicehandle hservice
             else
               function = 99 '0 '99
             end if
             closeservicehandle hsmanager
           else
             function = 98 '0 '99
           end if
            
          end function
          '______________________________________________________________________________
            
          function servicestart(computername as asciiz, servicename as asciiz ) as long
           dim hsmanager   as long
           dim hservice    as long
           dim lreturn     as long
           local i         as integer
           local tgp&                  ' loop protection for recursion
            
           on error goto start_error
           function = 0
            
           hsmanager = openscmanager(computername, $services_active_database, %sc_manager_all_access)
           if hsmanager <> 0 then
             hservice = openservice(hsmanager, servicename, %service_all_access )
             if hservice <> 0 then
                 tgp& = 0
          startserv:
                 lreturn = startservice (hservice, 0, 0)
                 if lreturn = 0 then
                   lreturn = getlasterror()
                   logresults "   --- service " & servicename & " error: " & str$(lreturn)
            
                   if tgp& = 0 then
                     ' try one more time
                     sleep 3000
                     incr tgp&
                     gosub startserv
                   else
                     'too bad
                   end if
                 else
                   ' verify running!
                   hit& = 0
                   i = 0
                   do while not servicestatus(", servicename) = %service_running
                     incr i
                     sleep 1000
            
                     if i > 60 then
                       if debug = 1 then
                         logresults "timeout starting " & servicename
                       end if
                       hit& = 1
                       exit do
                     end if
                   loop
            
                   if hit& = 0 then
                     function = 1
                   end if
                 end if
                 closeservicehandle hservice
              else
                 function = 2
              end if
              closeservicehandle hsmanager
           else
           end if
            
           exit function
           start_error:
            
          end function
          '______________________________________________________________________________
            
          function stopdependantservices( hsmanager as long, hservice as long, servicename as asciiz ) as long
           local dwbytesneeded as dword
           local dwservicesreturned as dword
           local tenumstatus() as enum_service_status
           local ldependentservice as long
           local lresult as long
            
           on error goto dependant_error
           function = 0
            
           'get dependent services
           'find out buffer size needed to enumerate
           call enumdependentservices( hservice, %service_active, byval %null, _
                                       byval 0???, dwbytesneeded, dwservicesreturned )
           lresult = getlasterror()
           select case lresult
             case %error_access_denied
               exit function
             case %error_invalid_handle
               function = 2
               exit function
             case %error_invalid_parameter
               exit function
             case %error_more_data
            
               if dwbytesneeded then
                 dim tenumstatus(0) '*  dwbytesneeded \ sizeof(tenumstatus(0))) +1)
            
                 if enumdependentservices( hservice,%service_active, byval varptr(tenumstatus(0)), _
                                                           sizeof(tenumstatus(0)) * ubound(tenumstatus(),1), _
                                                           dwbytesneeded, dwservicesreturned ) then
                   do while dwservicesreturned > 0
                     hit& = 0
            
                     if tenumstatus(dwservicesreturned-1).lpservicename then
                       if debug = 1 then
                         logresults "   --- stopping dependent service " & trim$(tenumstatus(dwservicesreturned-1)[email protected])
                       end if
            
                       ldependentservice = openservice( hsmanager, tenumstatus(dwservicesreturned-1)[email protected], %service_all_access )
                       if ldependentservice <> 0 then
                         if servicestop_part2(hsmanager, ldependentservice, servicename, 0) = 1 then
                           'ok: dependant service stopped
                         else
                           hit& = 1
                         end if
                         call closeservicehandle( ldependentservice )
                         if hit& = 1 then
                           exit function
                         end if
                       else
                         exit function
                       end if
                     end if
                     decr dwservicesreturned
                   loop
                 end if
               else
                 exit function
               end if
           end select
            
           function = 1
           exit function
            
          dependant_error:
            
          end function
          '______________________________________________________________________________
            
          function servicestop_part2(hsmanager as long, hservice as long, servicename as asciiz, tgp as long) as long
              ' tgp input is for recursion loop protection
              on error goto stop_error
              function = 0
            
              dim res as long
              dim servicestat as service_status
              local servicename2  as asciiz * 64
              res = controlservice (hservice, %service_control_stop, servicestat)
            
              if res = 0 then
                  res = getlasterror()
            
                  select case res
                      case %error_access_denied
                          if debug = 1 then
                              logresults "   --- service " & servicename & " : access denied"
                          end if
                      case %error_dependent_services_running
                          if debug = 1 then
                              logresults "   --- service " & servicename & " has dependant services : trying to stop them"
                          end if
            
                          if tgp < 3 then
                              ' copy name as not to mix it up (dependant does recursion)
                              servicename2 = servicename
            
                              if stopdependantservices( hsmanager, hservice, servicename2) = 0 then
                                  incr tgp
                                  ' try again
                                  function = servicestop_part2( hsmanager, hservice, servicename, tgp)
            
                              else
                                  ' ok, dependant services stopped
            
                                  ' try one more time
                                  tgp = 3
                                  function = servicestop_part2( hsmanager, hservice, servicename, tgp)
                              end if
                          end if
                      case %error_invalid_service_control
                          if debug = 1 then
                              logresults "   --- service " & servicename & " : invalid control"
                          end if
                      case %error_service_cannot_accept_ctrl
                          if debug = 1 then
                              logresults "   --- service " & servicename & " : cannot accept control : wait and try again"
                          end if
            
                          sleep 3000
            
                          if tgp < 10 then
                              incr tgp
                              ' try again
                              function = servicestop_part2( hsmanager, hservice, servicename, tgp)
                          else
                              if debug = 1 then
                                  logresults "   --- service " & servicename & " : cannot accept control (x10)"
                              end if
                          end if
                      case %error_service_not_active
                          if debug = 1 then
                              logresults "   --- service " & servicename & " already stopped"
                          end if
                          function = 1
                      case %error_service_request_timeout
                          if debug = 1 then
                              logresults "   --- service " & servicename & " : timeout sending control"
                          end if
            
                          sleep 3000
            
                          if tgp < 3 then
                              incr tgp
                              ' try again
                              function = servicestop_part2( hsmanager, hservice, servicename, tgp)
                          else
                              if debug = 1 then
                                  logresults "   --- service " & servicename & " : timeout sending control (x2)"
                              end if
                          end if
                      case %error_shutdown_in_progress
                          if debug = 1 then
                              logresults "   --- service " & servicename & " : shutdown in progress!!!"
                          end if
                      case else
                  end select
            
              else
                  if debug = 1 then
                      logresults "   --- service " & servicename & " stopped ok"
                  end if
                  ' ok
                  function = 1
              end if
            
              closeservicehandle hservice
            
              ' check stopped signal (to be sure)
              if res = 0 then
            
                  hit& = 0
            
                  do while not servicestatus(", servicename ) = %service_stopped
                      incr hit&
                      sleep 1000
            
                      if hit& > 10 then
                          function = 0
                          if debug = 1 then
                              logresults "waiting for stop " & servicename & " timeout!"
                          end if
                          exit function
                      end if
                  loop
            
                  function = 1
              end if
            
              exit function
          stop_error:
          end function
          '______________________________________________________________________________
            
          function servicestop(computername as asciiz, servicename as asciiz ) as long
            
              on error goto stop_error
            
              dim hsmanager as long
              dim hservice as long
            
              function = 0
            
              hsmanager = openscmanager(computername, $services_active_database, %sc_manager_all_access)
              if hsmanager <> 0 then
            
                  hservice = openservice(hsmanager, servicename, %service_all_access)
                  if hservice <> 0 then
            
                      if servicestop_part2( hsmanager, hservice, servicename, 0) = 1 then
                          function = 1
                      else
                          if debug = 1 then
                              logresults "   --- service " & servicename & " : couldn't be stopped"
                          end if
                      end if
                  else
                      function = 2
                  end if
                  closeservicehandle hsmanager
              else
                  if debug = 1 then
                      logresults "   --- service " & servicename & " : couldn't open openscmanager"
                  end if
              end if
            
              exit function
          stop_error:
               logresults "   --- error stopping service " & servicename & " : " & str$(err) & "; " & str$( getlasterror() )
          end function
          '______________________________________________________________________________
            
          function pbmain() as long
           local retval        as long
           local retval2       as long
           local sservice      as string
           local sserver       as string
           dim   stat(0 to 99) as string
            
           stat(0)  = "call failed. (0)"
           stat(1)  = "service stopped. (1)"
           stat(2)  = "service starting. (2)"
           stat(3)  = "service stopping. (3)"
           stat(4)  = "service started. (4)"
           stat(5)  = "service continuing. (5)"
           stat(6)  = "service pausing. (6)"
           stat(7)  = "service paused. (7)"
           stat(99) = "special case. (hard coded!)"
            
           sserver = " 'empty string for local services or sserver = "\computername"
           sservice = "remoteregistry"
            
           retval = servicestatus(bycopy sserver, bycopy sservice )
           print sservice & " : " & stat(retval)
           if retval = 4 then 'service is running
             retval2 = servicestop(bycopy sserver, bycopy sservice )
             print sservice & " : stopping"
             do
               sleep 500 : retval2 = servicestatus(bycopy sserver, bycopy sservice )
             loop while retval2 = 3
           elseif retval = 1 then 'service is stopped
             retval2 = servicestart(bycopy sserver, bycopy sservice )
             print sservice & " : starting"
             do
               sleep 500 : retval2 = servicestatus(bycopy sserver, bycopy sservice )
             loop while retval2 = 2
           end if
            
           retval2 = servicestatus(bycopy sserver, bycopy sservice )
           print sservice & " : " & stat(retval2)
           print
            
           if retval = 1 then 'service was initially running
             retval = servicestop(bycopy sserver, bycopy sservice )
             print sservice & " : stopping"
             do
               sleep 500 : retval2 = servicestatus(bycopy sserver, bycopy sservice )
             loop while retval2 = 3
           elseif retval = 4 then 'service was initially stopped
             retval = servicestart(bycopy sserver, bycopy sservice )
             print sservice  & " : starting"
             do
               sleep 500 : retval2 = servicestatus(bycopy sserver, bycopy sservice )
             loop while retval2 = 2
           end if
            
           retval2 = servicestatus(bycopy sserver, bycopy sservice )
           print sservice & " : " & stat(retval2)
           print "the end!"
           mouse on : mouse 3, up : waitkey$
            
          end function
          '______________________________________________________________________________

          [this message has been edited by pierre bellisle (edited november 17, 2003).]

          Comment


          • #6
            I am using PBWIN 7.0 compiler. I thank everyone for there help, But
            I still do not have anything working.

            This is what I want to do.

            I have a program CIMsqlupdate.exe that rights records to a Database.

            I need to get this program to run as a Services. Everyone is showing
            me code with missing INC files.

            How to I take the examples that have been sent to me and tie my exe
            program to it.



            ------------------
            mailto:[email protected][email protected]</A>
            mailto:[email protected][email protected]</A>

            Comment


            • #7
              Here is the Code that I am having a problem to get to work.


              The 'SQLProcessing is the Other Program that I need to run as a service

              '-----------------------------------------------------------------------
              '
              ' NT SERVICE Template for 32-bit
              '
              '
              '
              $COMPILE EXE "t:\service2.EXE"

              ' $RESOURCE "RESOURCE.PBR" ' Embed the resources into the exe

              #DIM ALL ' Force ALL vars to have to be declared
              GLOBAL hWindow AS LONG ' You should set this in your WINMAINX to the handle
              ' of your main window.

              #IF NOT %DEF(%WINAPI)

              #INCLUDE "C:\PBWin70\WinAPI\win32api.inc"

              #ENDIF

              #INCLUDE "C:\PBWin70\CIMSQL\cimSQLlib.inc"
              #INCLUDE "c:\SQLTOOLS\SQLT_Pro.INC"

              ' ---------------------
              ' CONSTANT DECLARATIONS
              ' ---------------------

              %EVENTLOG_AUDIT_FAILURE = 16
              %EVENTLOG_AUDIT_SUCCESS = 8
              %EVENTLOG_ERROR_TYPE = 1
              %EVENTLOG_INFORMATION_TYPE = 4
              %EVENTLOG_SUCCESS = 0
              %EVENTLOG_WARNING_TYPE = 2

              ' This constant is used to set logging status.

              %EVENTLOG_LEVEL = %EVENTLOG_AUDIT_FAILURE + %EVENTLOG_AUDIT_SUCCESS + %EVENTLOG_ERROR_TYPE + _
              %EVENTLOG_INFORMATION_TYPE + %EVENTLOG_SUCCESS + %EVENTLOG_WARNING_TYPE
              %SERVICE_WIN32_OWN_PROCESS = &H10&
              %SERVICE_WIN32_SHARE_PROCESS = &H20&
              %SERVICE_WIN32 = %SERVICE_WIN32_OWN_PROCESS + %SERVICE_WIN32_SHARE_PROCESS
              %SERVICE_DEMAND_START = &H3&
              %SERVICE_ERROR_NORMAL = &H1&
              %WAIT_OBJECT_0 = 0&

              ' -------------------------
              ' FUNCTION/SUB DECLARATIONS
              ' -------------------------
              DECLARE FUNCTION Install() AS LONG
              DECLARE FUNCTION InitService() AS LONG
              DECLARE FUNCTION Uninstall() AS LONG
              DECLARE FUNCTION WinErrorMessage(BYVAL ErrNumber AS LONG) AS STRING
              DECLARE FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
              DECLARE FUNCTION ServiceThread(ID AS LONG) AS LONG

              DECLARE SUB Handler(BYVAL ControlValue AS DWORD)
              DECLARE SUB LogNTEvent(Message AS STRING, LogCode AS LONG)
              DECLARE SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
              DECLARE SUB Terminate(ErrCode AS DWORD)
              DECLARE SUB StopService()
              DECLARE SUB PauseService()
              DECLARE SUB ResumeService()

              ' ----------------
              ' GLOBAL VARIABLES
              ' ----------------

              GLOBAL gascServiceName AS ASCIIZ * 8
              GLOBAL gascServiceDisplayName AS ASCIIZ * 256
              GLOBAL gdwdhStatus AS DWORD
              GLOBAL gdwdServiceState AS DWORD
              GLOBAL gdwdServiceStatus AS DWORD
              GLOBAL glnghInstance AS LONG
              GLOBAL glnghPrvInst AS LONG
              GLOBAL glnghTerminateEvent AS LONG
              GLOBAL glnghServiceThread AS LONG
              GLOBAL glngThreadResult AS LONG
              GLOBAL glngRunningService AS LONG
              GLOBAL glngPauseService AS LONG
              GLOBAL glngCmdShow AS LONG
              GLOBAL ptrCmdLine AS ASCIIZ PTR

              FUNCTION WINMAIN(BYVAL hCurInst AS LONG, _
              BYVAL hPrvInst AS LONG, _
              CmdLine AS ASCIIZ PTR, _
              BYVAL CmdShow AS LONG) EXPORT AS LONG



              DIM Result AS LONG
              DIM CmdParm AS STRING
              DIM udtSTE AS SERVICE_TABLE_ENTRY

              ' Set the service name and display name here.

              gascServiceName = "CIMSQL"
              'gascServiceDisplayName = "CIM-ODBC"
              'gascServiceDisplayName = "CIM-ODBC(tm)"
              gascServiceDisplayName = "CIMODBCUPDATE"

              glngHInstance = hCurInst
              glnghPrvInst = hPrvInst
              glngCmdShow = CmdShow
              ptrCmdLine = CmdLine
              ' Get the Command Line parms
              CmdParm = UCASE$(EXTRACT$(@CmdLine, ANY " ,." + CHR$(9)))
              ' Get rid of any seperators
              CmdParm = REMOVE$(CmdParm, ANY "-\/")
              ' If the exe was executed with the Install parm then we just install and quit

              IF CmdParm = "INSTALL" THEN
              ' Install the service.
              Result = Install()

              IF ISFALSE Result THEN

              MSGBOX "An error occured while trying to install this service

              END IF

              ' If the exe was executed with the UnInstall parm then we just uninstall and quit

              ELSEIF CmdParm = "UNINSTALL" THEN
              ' Uninstall the service.
              Result = Uninstall()

              IF ISFALSE Result THEN

              MSGBOX "An error occured while trying to uninstall this service

              END IF

              ' No parms were given that we want so start up as service
              ' This should only happen when the service control manager starts us

              ELSE

              udtSTE.lpServiceName = VARPTR(gascServiceName)
              udtSTE.lpServiceProc = CODEPTR(ServiceMain)
              Result = StartServiceCtrlDispatcher(udtSTE)

              IF Result = 0 THEN
              ExitProcess GetLastError()

              END IF

              END IF

              FUNCTION = 0

              END FUNCTION

              ' Install the service into windows

              FUNCTION Install() AS LONG

              DIM dwdRet AS DWORD
              DIM lngDQEnd AS LONG
              DIM lngDQStart AS LONG
              DIM lngHSCManager AS LONG
              DIM lngHService AS LONG
              DIM ascEXE AS ASCIIZ * %MAX_PATH
              ON ERROR GOTO Error_Install
              ' Assume a failure for now.

              FUNCTION = %FALSE

              lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)

              IF lngHSCManager <> %NULL THEN
              ' OK, we have a handle to the SCM.
              ' Get the full EXE file path.
              dwdRet = GetModuleFileName(glngHInstance, ascEXE, %MAX_PATH)
              IF dwdRet <> 0 THEN
              ' Install the service.
              lngHService = CreateService(lngHSCManager, gascServiceName, gascServiceDisplayName, _
              %SERVICE_ALL_ACCESS, %SERVICE_WIN32_OWN_PROCESS, _
              %SERVICE_AUTO_START, %SERVICE_ERROR_NORMAL, _
              ascEXE, BYVAL %NULL, BYVAL %NULL, _
              BYVAL %NULL, BYVAL %NULL, BYVAL %NULL)
              ' Close any service handles.

              IF lngHService <> %NULL THEN

              ' Success!

              FUNCTION = %TRUE
              CloseServiceHandle lngHService

              END IF

              CloseServiceHandle lngHSCManager

              END IF

              END IF

              FUNCTION = %TRUE

              EXIT FUNCTION

              Error_Install:

              FUNCTION = -1& * ERR
              ON ERROR RESUME NEXT
              ' Close any service handles.

              IF lngHService <> %NULL THEN

              CALL CloseServiceHandle(lngHService)

              END IF

              IF lngHSCManager <> %NULL THEN

              CALL CloseServiceHandle(lngHSCManager)

              END IF

              END FUNCTION

              ' Unistall the service

              FUNCTION Uninstall() AS LONG

              DIM lngHSCManager AS LONG
              DIM lngHService AS LONG
              ON ERROR GOTO Error_Uninstall
              ' Assume a failure for now.
              FUNCTION = %FALSE
              lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)

              IF lngHSCManager <> %NULL THEN
              ' OK, we have a handle to the SCM.
              ' Now open our service.
              lngHService = OpenService(lngHSCManager, gascServiceName, %SERVICE_ALL_ACCESS)

              IF lngHService <> %NULL THEN
              ' Delete the service.

              IF DeleteService(lngHService) <> %NULL THEN
              ' Success!
              FUNCTION = %TRUE

              END IF

              CloseServiceHandle lngHService

              END IF

              CloseServiceHandle lngHSCManager

              END IF

              EXIT FUNCTION

              Error_Uninstall:

              FUNCTION = -1& * ERR
              ON ERROR RESUME NEXT
              ' Close any service handles.

              IF lngHService <> %NULL THEN

              CloseServiceHandle lngHService

              END IF

              IF lngHSCManager <> %NULL THEN

              CloseServiceHandle lngHSCManager

              END IF

              END FUNCTION

              ' Start up the service

              FUNCTION InitService() AS LONG

              DIM lngRet AS LONG
              DIM ID AS LONG
              DIM udtSTE AS SERVICE_TABLE_ENTRY
              DIM lpThreadAttributes AS SECURITY_ATTRIBUTES
              ' Start the main thread for this service
              glnghServiceThread = CreateThread(lpThreadAttributes, 0, CODEPTR(ServiceThread), 0, 0, ID)
              ' Did the thread start OK

              IF glnghServiceThread = 0 THEN

              FUNCTION = %FALSE

              EXIT FUNCTION

              ELSE

              ' Set the global to running
              glngRunningService = %TRUE
              FUNCTION = %TRUE
              EXIT FUNCTION

              END IF

              END FUNCTION

              SUB PauseService()

              ' Set the global indicating that we are not paused
              glngPauseService = %TRUE
              ' Let er rip
              SuspendThread glnghServiceThread
              END SUB

              SUB ResumeService()
              ' Set the global indicating that we are not paused
              glngPauseService = %FALSE
              ' Let er rip
              ResumeThread glnghServiceThread
              END SUB

              SUB StopService()

              ' Set the global flag indicating that the service is not running
              glngRunningService = %FALSE
              ' Set the event so the service will stop
              SetEvent glnghTerminateEvent

              END SUB

              FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, _
              ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG

              DIM udtSS AS SERVICE_STATUS
              ' Reset the global service status value.
              gdwdServiceStatus = CurrentStatus
              ' Setup the UDT.
              udtSS.dwServiceType = %SERVICE_WIN32_OWN_PROCESS
              udtSS.dwCurrentState = CurrentStatus
              ' If we are the process of starting, then don't accept control events

              IF CurrentStatus = %SERVICE_START_PENDING THEN

              udtSS.dwControlsAccepted = 0

              ELSE

              ' Take what was given
              udtSS.dwControlsAccepted = %SERVICE_ACCEPT_STOP + %SERVICE_ACCEPT_PAUSE_CONTINUE + %SERVICE_ACCEPT_SHUTDOWN

              END IF

              ' If a specific ServiceSpecificExitCode is defined, setup the Win32 exit code properly

              IF ServiceSpecificExitCode = 0 THEN

              udtSS.dwWin32ExitCode = ExitCode

              ELSE

              udtSS.dwWin32ExitCode = %ERROR_SERVICE_SPECIFIC_ERROR

              END IF

              ' Specific Exit Code
              udtSS.dwServiceSpecificExitCode = ServiceSpecificExitCode
              udtSS.dwCheckPoint = Checkpoint
              udtSS.dwWaitHint = WaitHint

              IF SetServiceStatus(gdwdHStatus, udtSS) = 0 THEN

              ' Something went wrong so stop the service
              StopService
              FUNCTION = %FALSE

              ELSE

              FUNCTION = %TRUE

              END IF

              END FUNCTION

              SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)

              LOCAL Result AS LONG
              LOCAL lpEventAttributes AS SECURITY_ATTRIBUTES
              ' Register with the SCM
              gdwdHStatus = RegisterServiceCtrlHandler(gascServiceName, CODEPTR(Handler))
              ' Did it work

              IF gdwdHStatus = 0 THEN

              ' No, so terminate
              Terminate GetLastError()

              EXIT SUB

              END IF

              ' Service has been registered and startup is pending

              IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 1, 5000) THEN

              Terminate GetLastError()

              EXIT SUB

              END IF

              ' Create the termination event
              glnghTerminateEvent = CreateEvent(lpEventAttributes, %TRUE, %FALSE, "")

              IF glnghTerminateEvent = 0 THEN

              Terminate GetLastError()
              EXIT SUB

              END IF

              ' Service startup is still pending
              IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 2, 1000) THEN

              Terminate GetLastError()
              EXIT SUB

              END IF

              ' Start the service
              Result = InitService()

              IF ISFALSE Result THEN

              ' Oops
              Terminate GetLastError()
              EXIT SUB

              END IF
              ' Service is now running

              IF ISFALSE SendStatus(%SERVICE_RUNNING, %NO_ERROR, 0, 0, 0) THEN

              Terminate GetLastError()

              EXIT SUB

              END IF

              ' Wait for the signal to end
              WaitForSingleObject glnghTerminateEvent, %INFINITE
              Terminate 0

              EXIT SUB

              END SUB

              SUB Terminate(ErrCode AS DWORD)

              ' If the Terminate Event has already been created then destroy it

              IF glnghTerminateEvent <> 0 THEN

              CloseHandle glnghTerminateEvent

              END IF

              IF gdwdHStatus <> 0 THEN

              ' Send a message to the SCM and tell them that we are stopping

              SendStatus %SERVICE_STOPPED, ErrCode, 0&, 0&, 0&

              END IF

              ' If the thread has started, then kill it

              IF glnghServiceThread <> 0 THEN

              ' Not normally here in a service
              ' However, this program was written as an executable first and converted into a service
              ' There is a global hWindow that is set in the WINMAINX when we start
              ' We need to destroy the window so the WINMAINX will come back to us
              ' Is it a valid window

              IF IsWindow(hWindow) THEN

              ' Yes so destroy it
              DestroyWindow hWindow
              ' This will cause the Message Loop in your WINMAINX to exit and return

              END IF

              ' Close the thread

              CloseHandle glnghServiceThread

              END IF

              END SUB

              SUB Handler(BYVAL ControlValue AS DWORD)

              ' This procedure (by its' name) handles all service requests.
              LOCAL Result AS LONG
              ON ERROR RESUME NEXT
              ' There is no Start option because the ServiceMain takes care of starting the service

              SELECT CASE ControlValue

              CASE %SERVICE_CONTROL_STOP
              ' Set the global Status
              gdwdServiceState = %SERVICE_STOP_PENDING
              ' Tell the SCM that we are stopping
              SendStatus %SERVICE_STOP_PENDING, %NO_ERROR, 0&, 1, 5000
              ' Stop the service
              StopService

              CASE %SERVICE_CONTROL_PAUSE
              ' Are we running and not paused

              IF (ISTRUE glngRunningService) AND (ISFALSE glngPauseService) THEN

              ' Tell the SCM that we are pausing
              SendStatus %SERVICE_PAUSE_PENDING, %NO_ERROR, 0, 1, 1000
              ' Pause it
              PauseService
              ' Set the current state
              gdwdServiceState = %SERVICE_PAUSED

              END IF

              CASE %SERVICE_CONTROL_CONTINUE
              ' Are we running and paused

              IF (ISTRUE glngRunningService) AND (ISTRUE glngPauseService) THEN

              ' Tell the SCM that we are un pausing
              SendStatus %SERVICE_CONTINUE_PENDING, %NO_ERROR, 0, 1, 1000
              ' Resume the service
              ResumeService
              ' Set the current state
              gdwdServiceState = %SERVICE_RUNNING

              END IF

              CASE %SERVICE_CONTROL_INTERROGATE
              ' Don't need to do anything
              ' We will send the current status below


              CASE %SERVICE_CONTROL_SHUTDOWN
              ' We don't do anything with a shutdown

              EXIT SUB

              END SELECT

              ' Tell the SCM the new status

              SendStatus gdwdServiceState, %NO_ERROR, 0, 0, 0

              END SUB

              FUNCTION ServiceThread(ID AS LONG) EXPORT AS LONG

              LOCAL Result AS LONG
              LOCAL Msg AS tagMsg
              MSGBOX "In the Service Thread Function'

              ' Run until we are killed
              ' Result = WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
              ' call WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
              ' I had written a program and compiled it as an exe
              ' To make it a service, I just renamed the WINMAIN to WINMAINX and then
              ' call it like you see above

              END FUNCTION

              'SQLProcessing

              %FALSE = 0
              'Callback Functions
              DECLARE CALLBACK FUNCTION Exit_Dialog()
              DECLARE CALLBACK FUNCTION AboutCim()
              DECLARE CALLBACK FUNCTION Form1_DLGPROC

              'DECLARE FUNCTION Sql_Update_Display (BYVAL A$) AS LONG
              'DECLARE FUNCTION SQLUpdate AS LONG

              'Functions
              DECLARE FUNCTION MakeFont(BYVAL FontTypeSize AS LONG,BYVAL FontWeight AS LONG, _
              BYVAL Italic AS LONG, BYVAL Underline AS LONG, BYVAL StrikeOut AS LONG, _
              BYVAL FaceName AS STRING) AS LONG
              'Subs
              DECLARE SUB ShowDialog_Form1(BYVAL hParent&)
              DECLARE SUB Sql_Update_Display1
              DECLARE SUB OpenSQLFile1()
              ' SQL
              TYPE SQLTYPE1 ' LEN 900

              SQLTYPE AS STRING * 1 ' = "1" ' SqlTypeST
              DATEST AS STRING * 10 ' Date
              USERCODEST AS STRING * 4 ' UserCode
              USERNAMEST AS STRING * 20 ' UserName
              TIME AS STRING * 10 ' TimeST
              SESSION AS LONG ' SessionLO
              TRANSACTION AS LONG ' TransactionLO
              SESSIONREF AS STRING * 13 ' SessionRefST
              TRANSACTIONREF AS STRING * 13 ' TransactionRefST
              'Problem with adding records next 2 fields
              '==========================================================================
              BAGORORDERNO AS STRING * 8 ' BagOrderNumber
              BAGORORDERREF AS STRING * 13 ' BagOrderRef
              '==========================================================================
              BANKGROUPST AS STRING * 3 ' ' BankGroup
              DEPARTMENTLO AS LONG ' ' Department
              TRANSITST AS STRING * 13 ' ROUTING AND TRANSIT NO ' Transit
              TYPEST AS STRING * 1 ' ' Type
              CODEST AS STRING * 1 ' ' Code
              CLASSST AS STRING * 5 ' ' Class
              REFERENCEST AS STRING * 13 ' CUST NUMBER /ACCOUNT ' Reference
              CHARGE_TOST AS STRING * 13 ' ' Charge_To
              BRANCH_NUMBER AS DOUBLE ' LOCATION 10 DIGIT ' Branch_Number
              NAMEST AS STRING * 25 ' NAME ' Name
              ADD1ST AS STRING * 25 ' ADDRESS LINE 1 ' Address1
              ADD2ST AS STRING * 25 ' ADDRESS LINE 2 ' Address2
              ADD3ST AS STRING * 25 ' ADDRESS LINE 3 ' Address3
              ADD4ST AS STRING * 25 ' ADDRESS LINE 4 ' Address4
              PHONEST AS STRING * 15 ' CUSTOMER PHONE ' Phone
              FAXST AS STRING * 15 ' CALLBACK / FAX PHONE ' Fax
              TAXIDST AS STRING * 13 ' TAX ID NUMBER ' TaxId
              SHIPTOST AS STRING * 13 ' DEFAULT SHIP TO ROUTE ' ShopTo
              GLCOSTCENTERLO AS LONG ' ' GlCostCenter
              GLDEPOSITACCTLO AS LONG ' ' GlDepositAcct
              GLORDERACCTLO AS LONG ' ' GlOrderAcct
              GLADJUSTACCTLO AS LONG ' ' GlAdjustAcct
              RETAILBANKLO AS LONG ' ' RetailBank
              TRANSACTIONTYPE AS STRING * 10 ' TransactionType
              ENV_DEP_DECLARED AS DOUBLE ' Env_Dep_Declared
              DECLARED AS DOUBLE ' Declared
              TOTAL_ACTUAL AS DOUBLE ' Total_Actual
              DIFFERENCE AS DOUBLE ' Difference
              CURRENCYTOT AS DOUBLE ' CurrencyTot
              COINTOT AS DOUBLE ' CoinTot
              CASHTOT AS DOUBLE ' CashTot
              ONES AS DOUBLE ' Ones
              TWOS AS DOUBLE ' Twos
              FIVES AS DOUBLE ' Fives
              TENS AS DOUBLE ' Tens
              TWENTIES AS DOUBLE ' Twenties
              FIFTIES AS DOUBLE ' Fifties
              HUNDREDS AS DOUBLE ' Hundreds
              CURR_OTHER AS DOUBLE ' Currency_Other
              PENNIES AS DOUBLE ' Pennies
              NICKELS AS DOUBLE ' Nickles
              DIMES AS DOUBLE ' Dimes
              QUARTERS AS DOUBLE ' Quarters
              HALVES AS DOUBLE ' Halves
              DOLLARS AS DOUBLE ' Dollars
              COIN_OTHER AS DOUBLE ' Coin_Other
              NOTE_PIECES AS LONG ' Note_Pieces
              STRAP_PIECES AS LONG ' Strap_Pieces
              BRICK_PIECES AS LONG ' Brick_Pieces
              STAMP_VALUE AS DOUBLE ' Stamp_Value
              STAMP_PIECES AS LONG ' Stamp_Pieces
              COIN_PIECES AS LONG ' Coin_Pieces
              ROLLS_PIECES AS LONG ' Rolls_Pieces
              BOXES_PIECES AS LONG ' Boxes_Pieces
              BAGS_PIECES AS LONG ' Bags_Pieces
              GOLD_VALUE AS DOUBLE ' Gold_Value
              GOLD_PIECES AS LONG ' Gold_Pieces
              MIX_VALUE AS DOUBLE ' Mix_Value
              MIX_PIECES AS LONG ' Mix_Pieces
              MISC_DOCUMENT AS DOUBLE ' Misc_Document
              MISC_DOCUMENTL AS LONG ' Misc_DocumentL
              MERCHANDISE AS DOUBLE ' Merchandise
              MERCHANDISEL AS LONG ' MerchandiseL
              CHECKS AS DOUBLE ' Checks
              CHECKSL AS LONG ' ChecksL
              CASHIER_CHECKS AS DOUBLE ' Cashier_Checks
              CASHIER_CHECKSL AS LONG ' Cashier_ChecksL
              CREDIT_CARD AS DOUBLE ' Credit_Card
              CREDIT_CARDL AS LONG ' Credit_CardL
              CREDIT_NOCHARGE AS DOUBLE ' Credit_NoCharge
              CREDIT_NOCHARGEL AS LONG ' Credit_NoChargeL
              ADJUST AS DOUBLE ' Adjust
              ADJUSTL AS LONG ' AdjustL
              SLIP AS DOUBLE ' Slip
              SLIPL AS LONG ' SlipL
              ENTRIES AS LONG ' Entries
              SECONDS AS LONG ' Seconds
              'Problem with Field
              '===========================================================================
              REFERENCETEXT AS STRING * 30: ' ReferenceTextST
              '===========================================================================
              WRITEOFFDO AS DOUBLE ' = TWRITEOFFDO: 'AS DOUBLE ' WriteOff
              WRITEOFF AS LONG ' = WRITEOFF: 'AS LONG ' WriteOffD
              FOREIGNDO AS DOUBLE ' ' ForeignD
              FOREIGNLO AS LONG ' ForeignL
              'Problem with The Buffer*
              '===================================================================
              BUFFER5ST AS STRING * 25 ' Buffer5
              BUFFER6ST AS STRING * 13 ' Buffer6
              BUFFER7ST AS STRING * 1 ' Buffer7
              BUFFER8ST AS STRING * 1 ' Buffer8
              BUFFER9ST AS STRING * 1 ' Buffer9
              BUFFERAST AS STRING * 1 ' BufferA
              BUFFERBST AS STRING * 1 ' BufferB
              BUFFERCST AS STRING * 1 ' BufferC
              BUFFERDST AS STRING * 1 ' BufferD
              BUFFEREST AS STRING * 1 ' BufferE
              BUFFERFST AS STRING * 1 ' BufferF
              BUFFERGST AS STRING * 1 ' BufferG
              BUFFERHST AS STRING * 1 ' BufferH
              BUFFERIST AS STRING * 1 ' BufferI
              BUFFERJST AS STRING * 77 ' BufferJ
              '===================================================================
              EXEMPTST AS STRING * 1 ' Exemp
              MODELST AS STRING * 1 ' Model
              UPDATED AS STRING * 1 ' Y=UPDATED ' Updated

              END TYPE

              'Variables

              GLOBAL DB1OPEN AS LONG
              GLOBAL lResult&
              GLOBAL hDC AS LONG
              GLOBAL DisplayRecord AS STRING
              GLOBAL CIMSQLFILE$

              GLOBAL SQ1 AS SQLTYPE1

              'FUNCTION WINMAIN(BYVAL hCurInstance AS LONG, _
              ' BYVAL hPrevInstance AS LONG, _
              ' lpszCmdLine AS ASCIIZ PTR, _
              ' BYVAL nCmdShow AS LONG) _
              ' EXPORT AS LONG

              FUNCTION WINMAINX(BYVAL glngHInstance AS LONG , _
              BYVAL glnghPrvInst AS LONG ,_
              BYVAL ptrCmdLine AS ASCIIZ PTR * 40 ,_
              BYVAL glngCmdShow AS LONG)EXPORT AS LONG

              MSGBOX "In the WInMainX"

              ON ERROR RESUME NEXT:
              ERRCLEAR:
              LOCAL hDlg AS LONG
              GLOBAL sqlCount&
              '================================================
              ' This Must be Here Please do not move next line
              SQL_Authorize &h284FA389
              '================================================
              SQL_Init

              'IF SQL_Init <> %SUCCESS THEN
              ' SQL_Msgbox "SQL TOOLS INIT FAILURE", %MSGBOX_OK
              ' SQL_Shutdown
              ' EXIT FUNCTION
              'END IF

              SQL_OpenDB "GARDENIADATA.DSN" ' This gives the Login ODBC Box

              'Need error Checking here
              '========================

              'SQL_OpenDB("HMGardenia.DSN") ' Home Lan ConnectionSQL Server
              'SQL_OpenDB("HomeGardenia.DSN")

              Compiled_Date = "10.02.2003" 'DATE$
              ON ERROR RESUME NEXT: ERRCLEAR:
              DIM SHOWLINEST AS GLOBAL STRING:
              GLOBAL DisplayText AS STRING
              GLOBAL hMenu AS LONG
              LOCAL Count&
              LOCAL CC1 AS INIT_COMMON_CONTROLSEX
              CC1.dwSize=SIZEOF(CC1)
              CC1.dwICC=%ICC_WIN95_CLASSES
              InitCommonControlsEX CC1
              hDC = GetDC(%HWND_DESKTOP)
              LogPixelsY = GetDeviceCaps(hDC, %LOGPIXELSY)
              CF = 1 ' dialog conversion factor for large fonts setting
              IF LogPixelsY = 96 THEN CF = 1.25 ' dialog conversion factor for small fonts setting
              ReleaseDC %HWND_DESKTOP, hDC
              Brush=CreateSolidBrush(RGB(255,255,255)) ' white brush
              CaseFlag=0
              CALL OpenSQLFile1 '
              ShowDialog_Form1 0 ' Sub to draw the dialog box....

              DO
              DIALOG DOEVENTS TO Count&
              LOOP UNTIL Count&=0

              'Close SQL Gardenia Database
              SQL_CloseDB: DB1OPEN = 0
              SQL_Shutdown
              DeleteObject Brush
              CALL DeleteObject (TextFont)

              END FUNCTION

              SUB OpenSQLFile1() ' SQL_FORMAT_1.CIM

              CIMSQLFILE$ = "t:\SQL_FORMAT_1.CIM"
              ON ERROR RESUME NEXT:
              GOSUB OPENTHEINPUTFILE:
              IF ERR THEN MSGBOX "Error Opening File"+STR$(ERR) + " " + CIMSQLFILE$
              EXIT SUB:

              OPENTHEINPUTFILE:

              CLOSE #(291):
              ERRCLEAR:
              OPEN CIMSQLFILE$ FOR RANDOM LOCK SHARED AS #291 LEN=900:
              DB1OPEN = 1
              LOCK#(291): UNLOCK#(291):

              RETURN:

              END SUB

              SUB ShowDialog_Form1(BYVAL hParent&) ' Main Dialog

              ON ERROR RESUME NEXT:
              ERRCLEAR:

              LOCAL Style&, ExStyle&
              LOCAL hPopup1 AS LONG
              LOCAL hPopup2 AS LONG

              LOCAL X&,Y&,X1&,Y1&
              Style& = %WS_POPUP OR %WS_CAPTION OR _ 'OR %DS_MODALFRAME
              %WS_CLIPCHILDREN OR %WS_SYSMENU OR _
              %WS_VISIBLE OR %WS_MINIMIZEBOX OR _
              %WS_MAXIMIZEBOX
              ' OR %DS_CENTER ' %WS_MAXIMIZEBOX OR %WS_BORDER
              ExStyle& = 0

              X& = GetSystemMetrics(%SM_CXSCREEN) ' Get Width
              Y& = GetSystemMetrics(%SM_CYSCREEN) ' Get Height

              X& = 450 ' 600 '300
              Y& = 300 '200

              ' DIALOG NEW hParent&, "CIM-Open Database Connectivity(tm) v 1.0 " _
              ' + "Date: " + DATE$ + " " + _
              ' "Time: " + TIME$ + " " + "Use ALT + TAB Key to Switch C I M(tm) Screens" _
              ' ,0,0 , X&,Y&, Style&, ExStyle& TO hDlg
              '
              ' DIALOG PIXELS hDlg, X&, Y& TO UNITS X1&, Y1&
              ' DIALOG SET SIZE hDlg, X1&, Y1& 'xx, yy...
              ' DIALOG SET ICON hDlg, "#" + FORMAT$(%IDR_IMGFILE4)
              ' ' Add a menu bar to the GUI
              ' '--------------------------
              ' MENU NEW BAR TO hMenu
              ' MENU NEW POPUP TO hPopup1
              ' MENU ADD POPUP, hMenu, "&File", hPopUp1, %MF_ENABLED
              ' MENU ADD STRING, hPopup1, "-", 0, 0
              ' MENU ADD STRING, hPopup1, "E&xit", %IDCancel , %MF_ENABLED , CALL Exit_Dialog()
              ' 'About
              ' MENU NEW POPUP TO hPopup2
              ' MENU ADD POPUP, hMenu, "&Help", hPopup2, %MF_ENABLED
              ' MENU ADD STRING, hPopup2, "-", 0, 0
              ' MENU ADD STRING, hPopup2, "&About", %id_Menu_About, %MF_ENABLED ,CALL AboutCim
              ' MENU ATTACH hMenu, hDlg 'Attach the File Menu
              ' MENU ATTACH hMenu, hDlg 'Attach the File Menu
              ' MENU ATTACH hMenu, hDlg 'Attach the File Menu
              ' DIALOG SHOW MODELESS hDlg , CALL Form1_DLGPROC 'Show The Dialog

              END SUB

              CALLBACK FUNCTION Form1_DLGPROC

              ON ERROR RESUME NEXT: ERRCLEAR:

              GetClientRect hGraph,Rct ' Get rectangle in label (static) control

              SELECT CASE CBMSG


              CASE %WM_SYSCOMMAND ' A window receives this message
              ' when the user chooses a command from the window menu


              IF (CBWPARAM AND &HFFF0) = %SC_MAXIMIZE THEN
              'Reset the Screen size

              MSGBOX "You Selected the Maximize Button"

              END IF

              CASE %WM_PAINT

              hDC = BeginPaint(CBHNDL, Ps)
              'obtain handle of the static label control
              hDC = GetDC(GetDlgItem(CBHNDL, %FORM1_STATIC1))
              SELECT CASE CaseFlag

              CASE 0: Sql_Update_Display1() ' Display SQL Records and Errors................
              'CASE 0: call Sql_Update_Display("A")

              'CASE 1: CALL TextGraphics0(hDC) ' Welcome Screen.....................
              'CASE 2: CALL ' Future Use......
              CASE ELSE
              END SELECT
              EndPaint CBHNDL, Ps
              ReleaseDC GetDlgItem(CBHNDL, %FORM1_STATIC1), hDC
              FUNCTION = 1

              CASE %WM_CTLCOLORMSGBOX , %WM_CTLCOLORBTN, %WM_CTLCOLOREDIT,_
              %WM_CTLCOLORSTATIC, %WM_CTLCOLORSCROLLBAR, %WM_CTLCOLORLISTBOX
              ' Control colors
              SELECT CASE GetDlgCtrlID(CBLPARAM)
              CASE %FORM1_TEXT1
              SetTextColor CBWPARAM, RGB(0,0,191) ' Org 191
              SetBkColor CBWPARAM, RGB(255,255,191) ' 191 ' 255
              FUNCTION=Brush
              CASE ELSE
              FUNCTION=0
              END SELECT
              CASE ELSE

              END SELECT

              END FUNCTION

              'FUNCTION Sql_Update_Display(BYVAL A$) as long 'EXPORT AS LONG ' REFRESH
              '
              ' Sql_Update_Display1()
              '
              'END FUNCTION

              SUB Sql_Update_Display1()

              ON ERROR RESUME NEXT: ERRCLEAR:
              LOCAL X&,Y&,COMPLETE&,lenf&:
              LOCAL A$,B$,C$:
              LOCAL Width_Case&
              LOCAL Font_Size&,Font_Width&
              LOCAL Font_Name$
              ' SQL Code
              '==============================================
              DIM sSQLStatement AS GLOBAL STRING
              DIM RLO AS LONG

              ' JLB

              IF SQLLASTRECORDREAD = 0 THEN

              X& = GetSystemMetrics(%SM_CXSCREEN) ' Get Width
              Y& = GetSystemMetrics(%SM_CYSCREEN) ' Get Height

              CONTROL KILL hDlg, %FORM1_TEXT1

              Width_Case& = X&

              SELECT CASE Width_Case&

              CASE 1024 ' Width 1024 by 768

              X& = 285 '300 ' 450
              Y& = 145 ' 150
              Font_Size& = 11 ' Font Size
              Font_Width& = 700
              Font_Name$ = "Courier"

              CASE ELSE ' 800 ' Width 800 by 600

              X& = 280 '200
              Y& = 144 '140
              Font_Size& = 8 ' Font Size
              Font_Width& = 575 ' 300
              Font_Name$ = "Courier New" ' "Courier"

              END SELECT

              CONTROL ADD TEXTBOX, hDlg, %FORM1_TEXT1,"",0,0, X&,Y&,_
              %WS_VSCROLL OR %WS_HSCROLL OR %WS_SYSMENU OR %WS_CHILD OR _
              %WS_VISIBLE OR %ES_READONLY OR %ES_LEFT OR _
              %ES_MULTILINE

              TextFont=MakeFont(Font_Size&,Font_Width,0,0,0,Font_Name$) ' Also Sets the Font To BOLD

              CONTROL HANDLE hDlg,%FORM1_TEXT1 TO hEdit

              CALL SendMessage (hEdit, %WM_SETFONT, TextFont, 0) 'send font to control

              SQLSTARTRECORD = 1 ' LOC(291)/900:

              ELSE

              IF SQLSTARTRECORD <> SQLLASTRECORDREAD THEN SQLSTARTRECORD = SQLLASTRECORDREAD:

              END IF

              DO:

              COMPLETE& = 1:

              OpenSQLFile1()

              IF LOF(291)>800 THEN

              RLO=0:

              DO:


              SLEEP 1000:

              INCR RLO: ' NEW

              GET #291 , RLO, SQ1:

              INCR SQLStartRecord ' SQLLASTRECORDREAD:

              'SHOWLINEST = RIGHT$( " " + STR$(RLO) , 5 ) + " " + Sq1.SqlType + " " +Sq1.DateST + _
              ' " " + Sq1.USERNAMEST + Sq1.USERCODEST + " " + Sq1.UpDated:
              '
              'DisplayText = Displaytext + RTRIM$(SHOWLINEST) + $CRLF:

              IF SQ1.UPDATED <> "Y" THEN

              IF DB1OPEN = 0 THEN CALL OpenSQLFile1:

              'JLB
              'Testing the DB2 Env....

              ' sSQLStatement = "Insert INTO Transactions (SqlType, Date, UserCode, " + _
              ' "UserName, TimeST, SessionLO , TransactionLO)" + _ ' , SessionLO
              ' "SessionRefST, " + _
              ' " VALUES ('" + Sq1.SqlType + "','" + _
              ' Sq1.DateST + "','" + _
              ' Sq1.UserCodeST + "','" + _
              ' Sq1.USERNAMEST + "','" + _
              ' Sq1.TIME + "','" + _ ' + "','" + _ ' + "')"
              ' Str$(Sq1.SESSION) + "','" + _
              ' STR$(Sq1.TRANSACTION) + "','" + _
              ' Sq1.SESSIONREF + "')"

              'Sq1.UPDATED + "')" ' END OF FIELDS(Colums)
              ' Sq1.TIME + "')" ' + "','" + _
              ' "Updated)" + _ ' This is the Last Field in Database


              sSQLStatement = "Insert INTO Transactions (SqlType, Date, UserCode, " + _
              "UserName, TimeST ,SessionLO, TransactionLO, " + _
              "SessionRefST, " + _
              "TransactionRefST, " + _
              "BagOrderNumber, " + _
              "BagOrderRefST, " + _
              "BankGroup , " + _
              "Department," + _
              "Transit," + _
              "Type, " + _
              "Code, " + _
              "Class, " + _
              "Reference, " + _
              "Charge_To, " + _
              "Branch_Number, " + _
              "Name, " + _
              "Address1, " + _
              "Address2, " + _
              "Address3, " + _
              "Address4, " + _
              "Phone, " + _
              "Fax, " + _
              "TaxId, " + _
              "ShipTo, " + _
              "GlCostCenter, " + _
              "GlDepositAcct, " + _
              "GlOrderAcct, " + _
              "GlAdjustAcct, " + _
              "RetailBank, " + _
              "TransactionType, " + _
              "Env_Dep_Declared,Declared, " + _
              "Total_Actual, " + _
              "Difference, " + _
              "CurrencyTot, " + _
              "CoinTot, " + _
              "CashTot, " + _
              "Ones, " + _
              "Twos, " + _
              "Fives, " + _
              "Tens, " + _
              "Twenties, " + _
              "Fifties, " + _
              "Hundreds, " + _
              "Currency_Other, " + _
              "Pennies, " + _
              "Nickles, " + _
              "Dimes, " + _
              "Quarters, " + _
              "Halves, " + _
              "Dollars, " + _
              "Coin_Other, " + _
              "Note_Pieces, " + _
              "Strap_Pieces, " + _
              "Brick_Pieces, " + _
              "Stamp_Value, " + _
              "Stamp_Pieces, " + _
              "Coin_Pieces, " + _
              "Rolls_Pieces, " + _
              "Boxes_Pieces, " + _
              "Bags_Pieces, " + _
              "Gold_Value, " + _
              "Gold_Pieces, " + _
              "Mix_Value, " + _
              "Mix_Pieces, " + _
              "Misc_Document, " + _
              "Misc_DocumentL, " + _
              "Merchandise, " + _
              "MerchandiseL, " + _
              "Checks, " + _
              "ChecksL, " + _
              "Cashier_Checks, " + _
              "Cashier_ChecksL, " + _
              "Credit_Card, " + _
              "Credit_CardL, " + _
              "Credit_NoCharge, " + _
              "Credit_NoChargeL, " + _
              "Adjust, " + _
              "AdjustL, " + _
              "Slip, " + _
              "SlipL, " + _
              "Entries, " + _
              "Seconds, " + _
              "ReferenceTextST," + _
              "WriteOff, " + _
              "WriteOffD, " + _
              "ForeignD, " + _
              "ForeignL, " + _ '"Buffer5, " "Buffer6, " "Buffer7, " "Buffer8, " "Buffer9, " "BufferA, "
              "Exemp, " + _
              "Model, " + _
              "Updated)" + _ ' This is the Last Field in Database
              " VALUES ('" + Sq1.SqlType + "','" + _
              Sq1.DateST + "','" + _
              Sq1.UserCodeST + "','" + _
              Sq1.USERNAMEST + "','" + _
              Sq1.TIME + "','" + _
              STR$(Sq1.SESSION) + "','" + _
              STR$(Sq1.TRANSACTION) + "','" + _
              Sq1.SESSIONREF + "','" + _
              Sq1.TRANSACTIONREF + "','" + _
              Sq1.BAGORORDERNO + "','" + _
              Sq1.BAGORORDERREF + "','" + _
              Sq1.BANKGROUPST + "','" + _
              STR$(Sq1.DEPARTMENTLO) + "','" + _
              Sq1.TRANSITST + "','" + _
              Sq1.TYPEST + "','" + _
              Sq1.CODEST + "','" + _
              Sq1.CLASSST + "','" + _
              Sq1.REFERENCEST + "','" + _
              Sq1.CHARGE_TOST + "','" + _
              STR$(Sq1.BRANCH_NUMBER) + "','" + _
              Sq1.NAMEST + "','" + _
              Sq1.ADD1ST + "','" + _
              Sq1.ADD2ST + "','" + _
              Sq1.ADD3ST + "','" + _
              Sq1.ADD4ST + "','" + _
              Sq1.PHONEST + "','" + _
              Sq1.FAXST + "','" + _
              Sq1.TAXIDST + "','" + _
              Sq1.SHIPTOST + "','" + _
              STR$(Sq1.GLCOSTCENTERLO) + "','" + _
              STR$(Sq1.GLDEPOSITACCTLO) + "','" + _
              STR$(Sq1.GLORDERACCTLO) + "','" + _
              STR$(Sq1.GLADJUSTACCTLO) + "','" + _
              STR$(Sq1.RETAILBANKLO)+ "','" + _
              Sq1.TRANSACTIONTYPE + "','" + _
              STR$(Sq1.Env_Dep_Declared) + "','" + _
              STR$(Sq1.DECLARED) + "','" + _
              STR$(Sq1.TOTAL_ACTUAL) + "','" + _
              STR$(Sq1.DIFFERENCE) + "','" + _
              STR$(Sq1.CURRENCYTOT) + "','" + _
              STR$(Sq1.COINTOT) + "','" + _
              STR$(Sq1.CASHTOT) + "','" + _
              STR$(Sq1.ONES) + "','" + _
              STR$(Sq1.TWOS) + "','" + _
              STR$(Sq1.FIVES) + "','" + _
              STR$(Sq1.TENS) + "','" + _
              STR$(Sq1.TWENTIES) + "','" + _
              STR$(Sq1.FIFTIES) + "','" + _
              STR$(Sq1.HUNDREDS) + "','" + _
              STR$(Sq1.CURR_OTHER) + "','" + _
              STR$(Sq1.PENNIES) + "','" + _
              STR$(Sq1.NICKELS) + "','" + _
              STR$(Sq1.DIMES) + "','" + _
              STR$(Sq1.QUARTERS) + "','" + _
              STR$(Sq1.HALVES) + "','" + _
              STR$(Sq1.DOLLARS) + "','" + _
              STR$(Sq1.COIN_OTHER) + "','" + _
              STR$(Sq1.NOTE_PIECES) + "','" + _
              STR$(Sq1.STRAP_PIECES) + "','" + _
              STR$(Sq1.BRICK_PIECES) + "','" + _
              STR$(Sq1.STAMP_VALUE) + "','" + _
              STR$(Sq1.STAMP_PIECES) + "','" + _
              STR$(Sq1.COIN_PIECES) + "','" + _
              STR$(Sq1.ROLLS_PIECES) + "','" + _
              STR$(Sq1.BOXES_PIECES) + "','" + _
              STR$(Sq1.BAGS_PIECES) + "','" + _
              STR$(Sq1.GOLD_VALUE) + "','" + _
              STR$(Sq1.GOLD_PIECES) + "','" + _
              STR$(Sq1.MIX_VALUE) + "','" + _
              STR$(Sq1.MIX_PIECES) + "','" + _
              STR$(Sq1.MISC_DOCUMENT) + "','" + _
              STR$(Sq1.MISC_DOCUMENTL) + "','" + _
              STR$(Sq1.MERCHANDISE) + "','" + _
              STR$(Sq1.MERCHANDISEL) + "','" + _
              STR$(Sq1.CHECKS) + "','" + _
              STR$(Sq1.CHECKSL) + "','" + _
              STR$(Sq1.CASHIER_CHECKS) + "','" + _
              STR$(Sq1.CASHIER_CHECKSL) + "','" + _
              STR$(Sq1.CREDIT_CARD) + "','" + _
              STR$(Sq1.CREDIT_CARDL) + "','" + _
              STR$(Sq1.CREDIT_NOCHARGE) + "','" + _
              STR$(Sq1.CREDIT_NOCHARGEL) + "','" + _
              STR$(Sq1.ADJUST) + "','" + _
              STR$(Sq1.ADJUSTL) + "','" + _
              STR$(Sq1.SLIP) + "','" + _
              STR$(Sq1.SLIPL) + "','" + _
              STR$(Sq1.ENTRIES) + "','" + _
              STR$(Sq1.SECONDS) + "','" + _
              Sq1.REFERENCETEXT + "','" + _
              STR$(Sq1.WRITEOFFDO) + "','" + _
              STR$(Sq1.WRITEOFF) + "','" + _
              STR$(Sq1.FOREIGNDO) + "','" + _
              STR$(Sq1.FOREIGNLO) + "','" + _ ' Sq1.BUFFER5ST + "','" + _
              Sq1.EXEMPTST + "','" + _
              Sq1.MODELST + "','" + _
              Sq1.UPDATED + "')" ' END OF FIELDS(Colums)

              ERRCLEAR:

              SQL_Stmt %SQL_STMT_IMMEDIATE, sSQLStatement

              'IF SQL_ErrorPending THEN
              ' ERR = 99:
              ' SQL_MsgBox SQL_ErrorQuickAll, %MSGBOX_OK
              'END IF

              IF ERR = 0 THEN

              LSET SQ1.UPDATED ="Y":
              PUT #291,RLO,SQ1:
              ' GOSUB UPDATESCREENSQL1:

              ELSE

              COMPLETE&=0:

              END IF

              END IF

              LOOP UNTIL EOF(291)

              END IF

              'SLEEP 10000
              SLEEP 1000

              ' IF COMPLETE = 1 THEN
              '
              ' CLOSE #291:
              ' A$ = CIMSQLFILE$:
              ' REPLACE "CIM" WITH "BAK" IN A$:
              ' KILL A$:
              ' NAME CIMSQLFILE$ AS A$:
              '
              ' END IF

              LOOP UNTIL EOF(291)

              SQLlastRecordRead = LOF(291)/900:

              CaseFlag = 10

              EXIT SUB

              ' UPDATESCREENSQL1:
              '
              ' IF LEN(displayText)>lenf& THEN
              '
              ' lenf&=LEN(displayText)
              ' CONTROL SET TEXT hDlg, %FORM1_TEXT1, DisplayText
              '
              ' END IF
              '
              ' LOCAL tLen AS LONG
              ' tLen = SendMessage(hEdit, %WM_GETTEXTLENGTH, 0, 0)
              ' SendMessage hEdit, %EM_SETSEL, tLen, tLen
              ' SendMessage hEdit, %EM_SCROLLCARET, 0, 0
              '
              ' RETURN:

              END SUB

              FUNCTION MakeFont(BYVAL FontTypeSize AS LONG,BYVAL FontWeight AS LONG, _
              BYVAL Italic AS LONG, BYVAL Underline AS LONG,BYVAL StrikeOut AS LONG, _
              BYVAL FaceName AS STRING) AS LONG

              ON ERROR RESUME NEXT: ERRCLEAR:

              'TYPE LOGFONT defines the attributes of a font.
              lfFont.lfHeight = -MulDiv(FontTypeSize,LogPixelsY,72) '72
              'Logical height of font
              lfFont.lfWidth = 0 'logical average character width
              lfFont.lfEscapement = 0 'angle of escapement
              lfFont.lfOrientation = 0 'base-line orientation angle
              lfFont.lfWeight = FontWeight 'font weight
              lfFont.lfItalic = Italic 'italic attribute flag
              lfFont.lfUnderline = Underline 'underline attribute flag
              lfFont.lfStrikeOut = StrikeOut 'strikeout attribute flag
              lfFont.lfCharSet = %ANSI_CHARSET 'character set identifier
              lfFont.lfOutPrecision = %OUT_TT_PRECIS 'output precision
              lfFont.lfClipPrecision = %CLIP_DEFAULT_PRECIS 'clipping precision
              lfFont.lfQuality = %DEFAULT_QUALITY 'output quality
              'lfFont.lfPitchAndFamily = %FF_DONTCARE 'pitch and family
              lfFont.lfPitchAndFamily = %FW_BOLD 'pitch and family
              lfFont.lfFaceName = FaceName 'typeface name string
              'Make font according to specifications

              FUNCTION = CreateFontIndirect (lfFont)

              END FUNCTION

              CALLBACK FUNCTION Exit_Dialog() ' Exit the application.

              DIM TITLE$
              DIM SQLresult&
              Title$ = "Exit CIM-Open Database Connectivity"
              SQLresult& = MSGBOX("Are You Sure?", %MB_YESNO + %MB_SYSTEMMODAL , title$)

              IF SQLresult& = 6 THEN 'user seleceted yes
              FUNCTION = 1
              SQL_CloseDB: DB1OPEN = 0
              DIALOG END CBHNDL, 0
              END IF

              END FUNCTION

              CALLBACK FUNCTION AboutCim

              ON ERROR RESUME NEXT: ERRCLEAR:
              DIM AboutData$
              AboutData$ = "About CIM-Open Database Connectivity(tm)"
              MSGBOX "CIM-Open Database Connectivity(tm) v 1.0" & $CRLF & _
              " ver 1.0 released" + " " + Compiled_Date & $CRLF & _
              " Gardenia Programs, Inc.", %MB_APPLMODAL + %MB_ICONINFORMATION ,AboutData$

              END FUNCTION



              ------------------
              mailto:[email protected][email protected]</A>
              mailto:[email protected][email protected]</A>

              Comment


              • #8
                Joseph,

                >> I am using PBWIN 7.0 compiler

                1) as far as I see the code samples are written for console compiler.
                Console application are native for services but you can write
                win-like services too.

                2) for swapping an application to an service you have some work to do.
                (logic may be rewritten, (in most cases) no user interface, etc)

                3) for NT4/SP4?? / W2K / and above you have task scheduler as OS build in
                the easiest way is: use this scheduler with your exe und you should be fine.
                But: your app must be runable without any user input

                Ahoj
                -Uwe



                ------------------

                Comment


                • #9
                  I deleted all the Dialog stuff from the Program.

                  What I need to get to work now is how do I get the FUNCTION WINMAINX to be called where
                  in the Code should it be call from....

                  ' Program: CIMsqlService.bas
                  ' Date: November 18, 2003
                  '
                  '-----------------------------------------------------------------------

                  $COMPILE EXE "t:\CIMservice.EXE"

                  #DIM ALL ' Force ALL vars to have to be declared
                  GLOBAL hWindow AS LONG ' You should set this in your WINMAINX to the handle
                  ' of your main window.
                  #IF NOT %DEF(%WINAPI)

                  #INCLUDE "C:\PBWin70\WinAPI\win32api.inc"

                  #ENDIF
                  #INCLUDE "c:\SQLTOOLS\SQLT_Pro.INC"
                  ' ---------------------
                  ' CONSTANT DECLARATIONS
                  ' ---------------------
                  %EVENTLOG_AUDIT_FAILURE = 16
                  %EVENTLOG_AUDIT_SUCCESS = 8
                  %EVENTLOG_ERROR_TYPE = 1
                  %EVENTLOG_INFORMATION_TYPE = 4
                  %EVENTLOG_SUCCESS = 0
                  %EVENTLOG_WARNING_TYPE = 2
                  ' This constant is used to set logging status.
                  %EVENTLOG_LEVEL = %EVENTLOG_AUDIT_FAILURE + %EVENTLOG_AUDIT_SUCCESS + %EVENTLOG_ERROR_TYPE + _
                  %EVENTLOG_INFORMATION_TYPE + %EVENTLOG_SUCCESS + %EVENTLOG_WARNING_TYPE
                  %SERVICE_WIN32_OWN_PROCESS = &H10&
                  %SERVICE_WIN32_SHARE_PROCESS = &H20&
                  %SERVICE_WIN32 = %SERVICE_WIN32_OWN_PROCESS + %SERVICE_WIN32_SHARE_PROCESS
                  %SERVICE_DEMAND_START = &H3&
                  %SERVICE_ERROR_NORMAL = &H1&
                  %WAIT_OBJECT_0 = 0&
                  ' -------------------------
                  ' FUNCTION/SUB DECLARATIONS
                  ' -------------------------
                  DECLARE FUNCTION Install() AS LONG
                  DECLARE FUNCTION InitService() AS LONG
                  DECLARE FUNCTION Uninstall() AS LONG
                  DECLARE FUNCTION WinErrorMessage(BYVAL ErrNumber AS LONG) AS STRING
                  DECLARE FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
                  DECLARE FUNCTION ServiceThread(ID AS LONG) AS LONG

                  DECLARE SUB Handler(BYVAL ControlValue AS DWORD)
                  DECLARE SUB LogNTEvent(Message AS STRING, LogCode AS LONG)
                  DECLARE SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
                  DECLARE SUB Terminate(ErrCode AS DWORD)
                  DECLARE SUB StopService()
                  DECLARE SUB PauseService()
                  DECLARE SUB ResumeService()
                  ' ----------------
                  ' GLOBAL VARIABLES
                  ' ----------------
                  GLOBAL gascServiceName AS ASCIIZ * 8
                  GLOBAL gascServiceDisplayName AS ASCIIZ * 256
                  GLOBAL gdwdhStatus AS DWORD
                  GLOBAL gdwdServiceState AS DWORD
                  GLOBAL gdwdServiceStatus AS DWORD
                  GLOBAL glnghInstance AS LONG
                  GLOBAL glnghPrvInst AS LONG
                  GLOBAL glnghTerminateEvent AS LONG
                  GLOBAL glnghServiceThread AS LONG
                  GLOBAL glngThreadResult AS LONG
                  GLOBAL glngRunningService AS LONG
                  GLOBAL glngPauseService AS LONG
                  GLOBAL glngCmdShow AS LONG
                  GLOBAL ptrCmdLine AS ASCIIZ PTR

                  FUNCTION WINMAIN(BYVAL hCurInst AS LONG, _
                  BYVAL hPrvInst AS LONG, _
                  CmdLine AS ASCIIZ PTR, _
                  BYVAL CmdShow AS LONG) EXPORT AS LONG

                  '================================================
                  ' This Must be Here Please do not move next line
                  SQL_Authorize &h284FA389
                  '================================================
                  DIM Result AS LONG
                  DIM CmdParm AS STRING
                  DIM udtSTE AS SERVICE_TABLE_ENTRY

                  ' Set the service name and display name here.

                  gascServiceName = "CIMSQL"
                  'gascServiceDisplayName = "CIM-ODBC"
                  'gascServiceDisplayName = "CIM-ODBC(tm)"
                  gascServiceDisplayName = "CIM-ODBC"

                  glngHInstance = hCurInst
                  glnghPrvInst = hPrvInst
                  glngCmdShow = CmdShow
                  ptrCmdLine = CmdLine
                  ' Get the Command Line parms
                  CmdParm = UCASE$(EXTRACT$(@CmdLine, ANY " ,." + CHR$(9)))
                  ' Get rid of any seperators
                  CmdParm = REMOVE$(CmdParm, ANY "-\/")
                  ' If the exe was executed with the Install parm then we just install and quit

                  IF CmdParm = "INSTALL" THEN
                  ' Install the service.
                  Result = Install()

                  IF ISFALSE Result THEN

                  MSGBOX "An error occured while trying to install this service

                  END IF

                  ' If the exe was executed with the UnInstall parm then we just uninstall and quit

                  ELSEIF CmdParm = "UNINSTALL" THEN
                  ' Uninstall the service.
                  Result = Uninstall()

                  IF ISFALSE Result THEN

                  MSGBOX "An error occured while trying to uninstall this service

                  END IF

                  ' No parms were given that we want so start up as service
                  ' This should only happen when the service control manager starts us

                  ELSE

                  udtSTE.lpServiceName = VARPTR(gascServiceName)
                  udtSTE.lpServiceProc = CODEPTR(ServiceMain)
                  Result = StartServiceCtrlDispatcher(udtSTE)

                  IF Result = 0 THEN
                  ExitProcess GetLastError()

                  END IF

                  END IF

                  FUNCTION = 0

                  END FUNCTION

                  ' Install the service into windows

                  FUNCTION Install() AS LONG

                  DIM dwdRet AS DWORD
                  DIM lngDQEnd AS LONG
                  DIM lngDQStart AS LONG
                  DIM lngHSCManager AS LONG
                  DIM lngHService AS LONG
                  DIM ascEXE AS ASCIIZ * %MAX_PATH
                  ON ERROR GOTO Error_Install
                  ' Assume a failure for now.

                  FUNCTION = %FALSE

                  lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)

                  IF lngHSCManager <> %NULL THEN
                  ' OK, we have a handle to the SCM.
                  ' Get the full EXE file path.
                  dwdRet = GetModuleFileName(glngHInstance, ascEXE, %MAX_PATH)
                  IF dwdRet <> 0 THEN
                  ' Install the service.
                  lngHService = CreateService(lngHSCManager, gascServiceName, gascServiceDisplayName, _
                  %SERVICE_ALL_ACCESS, %SERVICE_WIN32_OWN_PROCESS, _
                  %SERVICE_AUTO_START, %SERVICE_ERROR_NORMAL, _
                  ascEXE, BYVAL %NULL, BYVAL %NULL, _
                  BYVAL %NULL, BYVAL %NULL, BYVAL %NULL)
                  ' Close any service handles.

                  IF lngHService <> %NULL THEN

                  ' Success!

                  FUNCTION = %TRUE
                  CloseServiceHandle lngHService

                  END IF

                  CloseServiceHandle lngHSCManager

                  END IF

                  END IF

                  FUNCTION = %TRUE

                  EXIT FUNCTION

                  Error_Install:

                  FUNCTION = -1& * ERR
                  ON ERROR RESUME NEXT
                  ' Close any service handles.

                  IF lngHService <> %NULL THEN

                  CALL CloseServiceHandle(lngHService)

                  END IF

                  IF lngHSCManager <> %NULL THEN

                  CALL CloseServiceHandle(lngHSCManager)

                  END IF

                  END FUNCTION

                  ' Unistall the service

                  FUNCTION Uninstall() AS LONG

                  DIM lngHSCManager AS LONG
                  DIM lngHService AS LONG
                  ON ERROR GOTO Error_Uninstall
                  ' Assume a failure for now.
                  FUNCTION = %FALSE
                  lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)

                  IF lngHSCManager <> %NULL THEN
                  ' OK, we have a handle to the SCM.
                  ' Now open our service.
                  lngHService = OpenService(lngHSCManager, gascServiceName, %SERVICE_ALL_ACCESS)

                  IF lngHService <> %NULL THEN
                  ' Delete the service.

                  IF DeleteService(lngHService) <> %NULL THEN
                  ' Success!
                  FUNCTION = %TRUE

                  END IF

                  CloseServiceHandle lngHService

                  END IF

                  CloseServiceHandle lngHSCManager

                  END IF

                  EXIT FUNCTION

                  Error_Uninstall:

                  FUNCTION = -1& * ERR
                  ON ERROR RESUME NEXT
                  ' Close any service handles.

                  IF lngHService <> %NULL THEN

                  CloseServiceHandle lngHService

                  END IF

                  IF lngHSCManager <> %NULL THEN

                  CloseServiceHandle lngHSCManager

                  END IF

                  END FUNCTION

                  ' Start up the service

                  FUNCTION InitService() AS LONG

                  DIM lngRet AS LONG
                  DIM ID AS LONG
                  DIM udtSTE AS SERVICE_TABLE_ENTRY
                  DIM lpThreadAttributes AS SECURITY_ATTRIBUTES
                  ' Start the main thread for this service
                  glnghServiceThread = CreateThread(lpThreadAttributes, 0, CODEPTR(ServiceThread), 0, 0, ID)
                  ' Did the thread start OK

                  IF glnghServiceThread = 0 THEN

                  FUNCTION = %FALSE

                  EXIT FUNCTION

                  ELSE

                  ' Set the global to running
                  glngRunningService = %TRUE
                  FUNCTION = %TRUE
                  EXIT FUNCTION

                  END IF

                  END FUNCTION

                  SUB PauseService()

                  ' Set the global indicating that we are not paused
                  glngPauseService = %TRUE
                  ' Let er rip
                  SuspendThread glnghServiceThread
                  END SUB

                  SUB ResumeService()
                  ' Set the global indicating that we are not paused
                  glngPauseService = %FALSE
                  ' Let er rip
                  ResumeThread glnghServiceThread
                  END SUB

                  SUB StopService()

                  ' Set the global flag indicating that the service is not running
                  glngRunningService = %FALSE
                  ' Set the event so the service will stop
                  SetEvent glnghTerminateEvent

                  END SUB

                  FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, _
                  ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG

                  DIM udtSS AS SERVICE_STATUS
                  ' Reset the global service status value.
                  gdwdServiceStatus = CurrentStatus
                  ' Setup the UDT.
                  udtSS.dwServiceType = %SERVICE_WIN32_OWN_PROCESS
                  udtSS.dwCurrentState = CurrentStatus
                  ' If we are the process of starting, then don't accept control events

                  IF CurrentStatus = %SERVICE_START_PENDING THEN

                  udtSS.dwControlsAccepted = 0

                  ELSE

                  ' Take what was given
                  udtSS.dwControlsAccepted = %SERVICE_ACCEPT_STOP + %SERVICE_ACCEPT_PAUSE_CONTINUE + %SERVICE_ACCEPT_SHUTDOWN

                  END IF

                  ' If a specific ServiceSpecificExitCode is defined, setup the Win32 exit code properly

                  IF ServiceSpecificExitCode = 0 THEN

                  udtSS.dwWin32ExitCode = ExitCode

                  ELSE

                  udtSS.dwWin32ExitCode = %ERROR_SERVICE_SPECIFIC_ERROR

                  END IF

                  ' Specific Exit Code
                  udtSS.dwServiceSpecificExitCode = ServiceSpecificExitCode
                  udtSS.dwCheckPoint = Checkpoint
                  udtSS.dwWaitHint = WaitHint

                  IF SetServiceStatus(gdwdHStatus, udtSS) = 0 THEN

                  ' Something went wrong so stop the service
                  StopService
                  FUNCTION = %FALSE

                  ELSE

                  FUNCTION = %TRUE

                  END IF

                  END FUNCTION

                  SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)

                  LOCAL Result AS LONG
                  LOCAL lpEventAttributes AS SECURITY_ATTRIBUTES
                  ' Register with the SCM
                  gdwdHStatus = RegisterServiceCtrlHandler(gascServiceName, CODEPTR(Handler))
                  ' Did it work

                  IF gdwdHStatus = 0 THEN

                  ' No, so terminate
                  Terminate GetLastError()

                  EXIT SUB

                  END IF

                  ' Service has been registered and startup is pending

                  IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 1, 5000) THEN

                  Terminate GetLastError()

                  EXIT SUB

                  END IF

                  ' Create the termination event
                  glnghTerminateEvent = CreateEvent(lpEventAttributes, %TRUE, %FALSE, "")

                  IF glnghTerminateEvent = 0 THEN

                  Terminate GetLastError()
                  EXIT SUB

                  END IF

                  ' Service startup is still pending
                  IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 2, 1000) THEN

                  Terminate GetLastError()
                  EXIT SUB

                  END IF

                  ' Start the service
                  Result = InitService()

                  IF ISFALSE Result THEN

                  ' Oops
                  Terminate GetLastError()
                  EXIT SUB

                  END IF
                  ' Service is now running

                  IF ISFALSE SendStatus(%SERVICE_RUNNING, %NO_ERROR, 0, 0, 0) THEN

                  Terminate GetLastError()

                  EXIT SUB

                  END IF

                  ' Wait for the signal to end
                  WaitForSingleObject glnghTerminateEvent, %INFINITE
                  Terminate 0

                  EXIT SUB

                  END SUB

                  SUB Terminate(ErrCode AS DWORD)

                  ' If the Terminate Event has already been created then destroy it

                  IF glnghTerminateEvent <> 0 THEN

                  CloseHandle glnghTerminateEvent

                  END IF

                  IF gdwdHStatus <> 0 THEN

                  ' Send a message to the SCM and tell them that we are stopping

                  SendStatus %SERVICE_STOPPED, ErrCode, 0&, 0&, 0&

                  END IF

                  ' If the thread has started, then kill it

                  IF glnghServiceThread <> 0 THEN

                  ' Not normally here in a service
                  ' However, this program was written as an executable first and converted into a service
                  ' There is a global hWindow that is set in the WINMAINX when we start
                  ' We need to destroy the window so the WINMAINX will come back to us
                  ' Is it a valid window

                  IF IsWindow(hWindow) THEN

                  ' Yes so destroy it
                  DestroyWindow hWindow
                  ' This will cause the Message Loop in your WINMAINX to exit and return

                  END IF

                  ' Close the thread

                  CloseHandle glnghServiceThread

                  END IF

                  END SUB

                  SUB Handler(BYVAL ControlValue AS DWORD)

                  ' This procedure (by its' name) handles all service requests.
                  LOCAL Result AS LONG
                  ON ERROR RESUME NEXT
                  ' There is no Start option because the ServiceMain takes care of starting the service

                  SELECT CASE ControlValue

                  CASE %SERVICE_CONTROL_STOP
                  ' Set the global Status
                  gdwdServiceState = %SERVICE_STOP_PENDING
                  ' Tell the SCM that we are stopping
                  SendStatus %SERVICE_STOP_PENDING, %NO_ERROR, 0&, 1, 5000
                  ' Stop the service
                  StopService

                  CASE %SERVICE_CONTROL_PAUSE
                  ' Are we running and not paused

                  IF (ISTRUE glngRunningService) AND (ISFALSE glngPauseService) THEN

                  ' Tell the SCM that we are pausing
                  SendStatus %SERVICE_PAUSE_PENDING, %NO_ERROR, 0, 1, 1000
                  ' Pause it
                  PauseService
                  ' Set the current state
                  gdwdServiceState = %SERVICE_PAUSED

                  END IF

                  CASE %SERVICE_CONTROL_CONTINUE
                  ' Are we running and paused

                  IF (ISTRUE glngRunningService) AND (ISTRUE glngPauseService) THEN

                  ' Tell the SCM that we are un pausing
                  SendStatus %SERVICE_CONTINUE_PENDING, %NO_ERROR, 0, 1, 1000
                  ' Resume the service
                  ResumeService
                  ' Set the current state
                  gdwdServiceState = %SERVICE_RUNNING

                  END IF

                  CASE %SERVICE_CONTROL_INTERROGATE
                  ' Don't need to do anything
                  ' We will send the current status below


                  CASE %SERVICE_CONTROL_SHUTDOWN
                  ' We don't do anything with a shutdown

                  EXIT SUB

                  END SELECT

                  ' Tell the SCM the new status

                  SendStatus gdwdServiceState, %NO_ERROR, 0, 0, 0

                  END SUB

                  FUNCTION ServiceThread(ID AS LONG) EXPORT AS LONG

                  LOCAL Result AS LONG
                  LOCAL Msg AS tagMsg
                  MSGBOX "In the Service Thread Function'

                  ' Run until we are killed
                  ' Result = WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
                  ' call WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
                  ' I had written a program and compiled it as an exe
                  ' To make it a service, I just renamed the WINMAIN to WINMAINX and then
                  ' call it like you see above

                  END FUNCTION

                  'SQL Processing Routine

                  %FALSE = 0

                  DECLARE SUB Sql_Update_Display1
                  DECLARE SUB OpenSQLFile1()
                  ' SQL
                  TYPE SQLTYPE1 ' LEN 900

                  SQLTYPE AS STRING * 1 ' = "1" ' SqlTypeST
                  DATEST AS STRING * 10 ' Date
                  USERCODEST AS STRING * 4 ' UserCode
                  USERNAMEST AS STRING * 20 ' UserName
                  TIME AS STRING * 10 ' TimeST
                  SESSION AS LONG ' SessionLO
                  TRANSACTION AS LONG ' TransactionLO
                  SESSIONREF AS STRING * 13 ' SessionRefST
                  TRANSACTIONREF AS STRING * 13 ' TransactionRefST
                  BAGORORDERNO AS STRING * 8 ' BagOrderNumber
                  BAGORORDERREF AS STRING * 13 ' BagOrderRef
                  BANKGROUPST AS STRING * 3 ' ' BankGroup
                  DEPARTMENTLO AS LONG ' ' Department
                  TRANSITST AS STRING * 13 ' ROUTING AND TRANSIT NO ' Transit
                  TYPEST AS STRING * 1 ' ' Type
                  CODEST AS STRING * 1 ' ' Code
                  CLASSST AS STRING * 5 ' ' Class
                  REFERENCEST AS STRING * 13 ' CUST NUMBER /ACCOUNT ' Reference
                  CHARGE_TOST AS STRING * 13 ' ' Charge_To
                  BRANCH_NUMBER AS DOUBLE ' LOCATION 10 DIGIT ' Branch_Number
                  NAMEST AS STRING * 25 ' NAME ' Name
                  ADD1ST AS STRING * 25 ' ADDRESS LINE 1 ' Address1
                  ADD2ST AS STRING * 25 ' ADDRESS LINE 2 ' Address2
                  ADD3ST AS STRING * 25 ' ADDRESS LINE 3 ' Address3
                  ADD4ST AS STRING * 25 ' ADDRESS LINE 4 ' Address4
                  PHONEST AS STRING * 15 ' CUSTOMER PHONE ' Phone
                  FAXST AS STRING * 15 ' CALLBACK / FAX PHONE ' Fax
                  TAXIDST AS STRING * 13 ' TAX ID NUMBER ' TaxId
                  SHIPTOST AS STRING * 13 ' DEFAULT SHIP TO ROUTE ' ShopTo
                  GLCOSTCENTERLO AS LONG ' ' GlCostCenter
                  GLDEPOSITACCTLO AS LONG ' ' GlDepositAcct
                  GLORDERACCTLO AS LONG ' ' GlOrderAcct
                  GLADJUSTACCTLO AS LONG ' ' GlAdjustAcct
                  RETAILBANKLO AS LONG ' ' RetailBank
                  TRANSACTIONTYPE AS STRING * 10 ' TransactionType
                  ENV_DEP_DECLARED AS DOUBLE ' Env_Dep_Declared
                  DECLARED AS DOUBLE ' Declared
                  TOTAL_ACTUAL AS DOUBLE ' Total_Actual
                  DIFFERENCE AS DOUBLE ' Difference
                  CURRENCYTOT AS DOUBLE ' CurrencyTot
                  COINTOT AS DOUBLE ' CoinTot
                  CASHTOT AS DOUBLE ' CashTot
                  ONES AS DOUBLE ' Ones
                  TWOS AS DOUBLE ' Twos
                  FIVES AS DOUBLE ' Fives
                  TENS AS DOUBLE ' Tens
                  TWENTIES AS DOUBLE ' Twenties
                  FIFTIES AS DOUBLE ' Fifties
                  HUNDREDS AS DOUBLE ' Hundreds
                  CURR_OTHER AS DOUBLE ' Currency_Other
                  PENNIES AS DOUBLE ' Pennies
                  NICKELS AS DOUBLE ' Nickles
                  DIMES AS DOUBLE ' Dimes
                  QUARTERS AS DOUBLE ' Quarters
                  HALVES AS DOUBLE ' Halves
                  DOLLARS AS DOUBLE ' Dollars
                  COIN_OTHER AS DOUBLE ' Coin_Other
                  NOTE_PIECES AS LONG ' Note_Pieces
                  STRAP_PIECES AS LONG ' Strap_Pieces
                  BRICK_PIECES AS LONG ' Brick_Pieces
                  STAMP_VALUE AS DOUBLE ' Stamp_Value
                  STAMP_PIECES AS LONG ' Stamp_Pieces
                  COIN_PIECES AS LONG ' Coin_Pieces
                  ROLLS_PIECES AS LONG ' Rolls_Pieces
                  BOXES_PIECES AS LONG ' Boxes_Pieces
                  BAGS_PIECES AS LONG ' Bags_Pieces
                  GOLD_VALUE AS DOUBLE ' Gold_Value
                  GOLD_PIECES AS LONG ' Gold_Pieces
                  MIX_VALUE AS DOUBLE ' Mix_Value
                  MIX_PIECES AS LONG ' Mix_Pieces
                  MISC_DOCUMENT AS DOUBLE ' Misc_Document
                  MISC_DOCUMENTL AS LONG ' Misc_DocumentL
                  MERCHANDISE AS DOUBLE ' Merchandise
                  MERCHANDISEL AS LONG ' MerchandiseL
                  CHECKS AS DOUBLE ' Checks
                  CHECKSL AS LONG ' ChecksL
                  CASHIER_CHECKS AS DOUBLE ' Cashier_Checks
                  CASHIER_CHECKSL AS LONG ' Cashier_ChecksL
                  CREDIT_CARD AS DOUBLE ' Credit_Card
                  CREDIT_CARDL AS LONG ' Credit_CardL
                  CREDIT_NOCHARGE AS DOUBLE ' Credit_NoCharge
                  CREDIT_NOCHARGEL AS LONG ' Credit_NoChargeL
                  ADJUST AS DOUBLE ' Adjust
                  ADJUSTL AS LONG ' AdjustL
                  SLIP AS DOUBLE ' Slip
                  SLIPL AS LONG ' SlipL
                  ENTRIES AS LONG ' Entries
                  SECONDS AS LONG ' Seconds
                  REFERENCETEXT AS STRING * 30: ' ReferenceTextST
                  WRITEOFFDO AS DOUBLE ' = TWRITEOFFDO: 'AS DOUBLE ' WriteOff
                  WRITEOFF AS LONG ' = WRITEOFF: 'AS LONG ' WriteOffD
                  FOREIGNDO AS DOUBLE ' ' ForeignD
                  FOREIGNLO AS LONG ' ForeignL
                  'Problem with The Buffer*
                  '===================================================================
                  BUFFER5ST AS STRING * 25 ' Buffer5
                  BUFFER6ST AS STRING * 13 ' Buffer6
                  BUFFER7ST AS STRING * 1 ' Buffer7
                  BUFFER8ST AS STRING * 1 ' Buffer8
                  BUFFER9ST AS STRING * 1 ' Buffer9
                  BUFFERAST AS STRING * 1 ' BufferA
                  BUFFERBST AS STRING * 1 ' BufferB
                  BUFFERCST AS STRING * 1 ' BufferC
                  BUFFERDST AS STRING * 1 ' BufferD
                  BUFFEREST AS STRING * 1 ' BufferE
                  BUFFERFST AS STRING * 1 ' BufferF
                  BUFFERGST AS STRING * 1 ' BufferG
                  BUFFERHST AS STRING * 1 ' BufferH
                  BUFFERIST AS STRING * 1 ' BufferI
                  BUFFERJST AS STRING * 77 ' BufferJ
                  '===================================================================
                  EXEMPTST AS STRING * 1 ' Exemp
                  MODELST AS STRING * 1 ' Model
                  UPDATED AS STRING * 1 ' Y=UPDATED ' Updated

                  END TYPE

                  'Variables

                  GLOBAL DB1OPEN AS LONG
                  ' GLOBAL lResult&
                  ' GLOBAL hDC AS LONG
                  GLOBAL DisplayRecord AS STRING
                  GLOBAL CIMSQLFILE$
                  GLOBAL SQ1 AS SQLTYPE1


                  FUNCTION WINMAINX(BYVAL glngHInstance AS LONG , _
                  BYVAL glnghPrvInst AS LONG ,_
                  BYVAL ptrCmdLine AS ASCIIZ PTR * 40 ,_
                  BYVAL glngCmdShow AS LONG)EXPORT AS LONG

                  MSGBOX "In the WInMainX"

                  ON ERROR RESUME NEXT:
                  ERRCLEAR:
                  GLOBAL sqlCount&
                  SQL_Init
                  SQL_OpenDB "GARDENIADATA.DSN" ' This gives the Login ODBC Box
                  'SQL_OpenDB("HMGardenia.DSN") ' Home Lan ConnectionSQL Server
                  'SQL_OpenDB("HomeGardenia.DSN")

                  CALL OpenSQLFile1 '
                  CALL Sql_Update_Display1()

                  END FUNCTION

                  SUB OpenSQLFile1() ' SQL_FORMAT_1.CIM

                  CIMSQLFILE$ = "t:\SQL_FORMAT_1.CIM"
                  ON ERROR RESUME NEXT:

                  CLOSE #(291):
                  ERRCLEAR:
                  OPEN CIMSQLFILE$ FOR RANDOM LOCK SHARED AS #291 LEN=900:
                  DB1OPEN = 1
                  LOCK#(291): UNLOCK#(291):

                  ' Error Checking Here...

                  END SUB

                  SUB Sql_Update_Display1()

                  ON ERROR RESUME NEXT: ERRCLEAR:
                  LOCAL X&,Y&,COMPLETE&,lenf&:
                  LOCAL A$,B$,C$:
                  LOCAL SQLLASTRECORDREAD AS LONG

                  DIM sSQLStatement AS GLOBAL STRING
                  DIM RLO AS LONG
                  DIM SQLSTARTRECORD AS LONG

                  IF SQLLASTRECORDREAD = 0 THEN

                  SQLSTARTRECORD = 1 ' LOC(291)/900:

                  ELSE

                  IF SQLSTARTRECORD <> SQLLASTRECORDREAD THEN SQLSTARTRECORD = SQLLASTRECORDREAD:

                  END IF

                  DO:

                  COMPLETE& = 1:

                  OpenSQLFile1()

                  IF LOF(291)>800 THEN

                  RLO=0:

                  DO:

                  SLEEP 1000:

                  INCR RLO: ' NEW

                  GET #291 , RLO, SQ1:

                  INCR SQLStartRecord

                  IF SQ1.UPDATED <> "Y" THEN

                  IF DB1OPEN = 0 THEN CALL OpenSQLFile1:

                  sSQLStatement = "Insert INTO Transactions (SqlType, Date, UserCode, " + _
                  "UserName, TimeST ,SessionLO, TransactionLO, " + _
                  "SessionRefST, " + _
                  "TransactionRefST, " + _
                  "BagOrderNumber, " + _
                  "BagOrderRefST, " + _
                  "BankGroup , " + _
                  "Department," + _
                  "Transit," + _
                  "Type, " + _
                  "Code, " + _
                  "Class, " + _
                  "Reference, " + _
                  "Charge_To, " + _
                  "Branch_Number, " + _
                  "Name, " + _
                  "Address1, " + _
                  "Address2, " + _
                  "Address3, " + _
                  "Address4, " + _
                  "Phone, " + _
                  "Fax, " + _
                  "TaxId, " + _
                  "ShipTo, " + _
                  "GlCostCenter, " + _
                  "GlDepositAcct, " + _
                  "GlOrderAcct, " + _
                  "GlAdjustAcct, " + _
                  "RetailBank, " + _
                  "TransactionType, " + _
                  "Env_Dep_Declared,Declared, " + _
                  "Total_Actual, " + _
                  "Difference, " + _
                  "CurrencyTot, " + _
                  "CoinTot, " + _
                  "CashTot, " + _
                  "Ones, " + _
                  "Twos, " + _
                  "Fives, " + _
                  "Tens, " + _
                  "Twenties, " + _
                  "Fifties, " + _
                  "Hundreds, " + _
                  "Currency_Other, " + _
                  "Pennies, " + _
                  "Nickles, " + _
                  "Dimes, " + _
                  "Quarters, " + _
                  "Halves, " + _
                  "Dollars, " + _
                  "Coin_Other, " + _
                  "Note_Pieces, " + _
                  "Strap_Pieces, " + _
                  "Brick_Pieces, " + _
                  "Stamp_Value, " + _
                  "Stamp_Pieces, " + _
                  "Coin_Pieces, " + _
                  "Rolls_Pieces, " + _
                  "Boxes_Pieces, " + _
                  "Bags_Pieces, " + _
                  "Gold_Value, " + _
                  "Gold_Pieces, " + _
                  "Mix_Value, " + _
                  "Mix_Pieces, " + _
                  "Misc_Document, " + _
                  "Misc_DocumentL, " + _
                  "Merchandise, " + _
                  "MerchandiseL, " + _
                  "Checks, " + _
                  "ChecksL, " + _
                  "Cashier_Checks, " + _
                  "Cashier_ChecksL, " + _
                  "Credit_Card, " + _
                  "Credit_CardL, " + _
                  "Credit_NoCharge, " + _
                  "Credit_NoChargeL, " + _
                  "Adjust, " + _
                  "AdjustL, " + _
                  "Slip, " + _
                  "SlipL, " + _
                  "Entries, " + _
                  "Seconds, " + _
                  "ReferenceTextST," + _
                  "WriteOff, " + _
                  "WriteOffD, " + _
                  "ForeignD, " + _
                  "ForeignL, " + _ '"Buffer5, " "Buffer6, " "Buffer7, " "Buffer8, " "Buffer9, " "BufferA, "
                  "Exemp, " + _
                  "Model, " + _
                  "Updated)" + _ ' This is the Last Field in Database
                  " VALUES ('" + Sq1.SqlType + "','" + _
                  Sq1.DateST + "','" + _
                  Sq1.UserCodeST + "','" + _
                  Sq1.USERNAMEST + "','" + _
                  Sq1.TIME + "','" + _
                  STR$(Sq1.SESSION) + "','" + _
                  STR$(Sq1.TRANSACTION) + "','" + _
                  Sq1.SESSIONREF + "','" + _
                  Sq1.TRANSACTIONREF + "','" + _
                  Sq1.BAGORORDERNO + "','" + _
                  Sq1.BAGORORDERREF + "','" + _
                  Sq1.BANKGROUPST + "','" + _
                  STR$(Sq1.DEPARTMENTLO) + "','" + _
                  Sq1.TRANSITST + "','" + _
                  Sq1.TYPEST + "','" + _
                  Sq1.CODEST + "','" + _
                  Sq1.CLASSST + "','" + _
                  Sq1.REFERENCEST + "','" + _
                  Sq1.CHARGE_TOST + "','" + _
                  STR$(Sq1.BRANCH_NUMBER) + "','" + _
                  Sq1.NAMEST + "','" + _
                  Sq1.ADD1ST + "','" + _
                  Sq1.ADD2ST + "','" + _
                  Sq1.ADD3ST + "','" + _
                  Sq1.ADD4ST + "','" + _
                  Sq1.PHONEST + "','" + _
                  Sq1.FAXST + "','" + _
                  Sq1.TAXIDST + "','" + _
                  Sq1.SHIPTOST + "','" + _
                  STR$(Sq1.GLCOSTCENTERLO) + "','" + _
                  STR$(Sq1.GLDEPOSITACCTLO) + "','" + _
                  STR$(Sq1.GLORDERACCTLO) + "','" + _
                  STR$(Sq1.GLADJUSTACCTLO) + "','" + _
                  STR$(Sq1.RETAILBANKLO)+ "','" + _
                  Sq1.TRANSACTIONTYPE + "','" + _
                  STR$(Sq1.Env_Dep_Declared) + "','" + _
                  STR$(Sq1.DECLARED) + "','" + _
                  STR$(Sq1.TOTAL_ACTUAL) + "','" + _
                  STR$(Sq1.DIFFERENCE) + "','" + _
                  STR$(Sq1.CURRENCYTOT) + "','" + _
                  STR$(Sq1.COINTOT) + "','" + _
                  STR$(Sq1.CASHTOT) + "','" + _
                  STR$(Sq1.ONES) + "','" + _
                  STR$(Sq1.TWOS) + "','" + _
                  STR$(Sq1.FIVES) + "','" + _
                  STR$(Sq1.TENS) + "','" + _
                  STR$(Sq1.TWENTIES) + "','" + _
                  STR$(Sq1.FIFTIES) + "','" + _
                  STR$(Sq1.HUNDREDS) + "','" + _
                  STR$(Sq1.CURR_OTHER) + "','" + _
                  STR$(Sq1.PENNIES) + "','" + _
                  STR$(Sq1.NICKELS) + "','" + _
                  STR$(Sq1.DIMES) + "','" + _
                  STR$(Sq1.QUARTERS) + "','" + _
                  STR$(Sq1.HALVES) + "','" + _
                  STR$(Sq1.DOLLARS) + "','" + _
                  STR$(Sq1.COIN_OTHER) + "','" + _
                  STR$(Sq1.NOTE_PIECES) + "','" + _
                  STR$(Sq1.STRAP_PIECES) + "','" + _
                  STR$(Sq1.BRICK_PIECES) + "','" + _
                  STR$(Sq1.STAMP_VALUE) + "','" + _
                  STR$(Sq1.STAMP_PIECES) + "','" + _
                  STR$(Sq1.COIN_PIECES) + "','" + _
                  STR$(Sq1.ROLLS_PIECES) + "','" + _
                  STR$(Sq1.BOXES_PIECES) + "','" + _
                  STR$(Sq1.BAGS_PIECES) + "','" + _
                  STR$(Sq1.GOLD_VALUE) + "','" + _
                  STR$(Sq1.GOLD_PIECES) + "','" + _
                  STR$(Sq1.MIX_VALUE) + "','" + _
                  STR$(Sq1.MIX_PIECES) + "','" + _
                  STR$(Sq1.MISC_DOCUMENT) + "','" + _
                  STR$(Sq1.MISC_DOCUMENTL) + "','" + _
                  STR$(Sq1.MERCHANDISE) + "','" + _
                  STR$(Sq1.MERCHANDISEL) + "','" + _
                  STR$(Sq1.CHECKS) + "','" + _
                  STR$(Sq1.CHECKSL) + "','" + _
                  STR$(Sq1.CASHIER_CHECKS) + "','" + _
                  STR$(Sq1.CASHIER_CHECKSL) + "','" + _
                  STR$(Sq1.CREDIT_CARD) + "','" + _
                  STR$(Sq1.CREDIT_CARDL) + "','" + _
                  STR$(Sq1.CREDIT_NOCHARGE) + "','" + _
                  STR$(Sq1.CREDIT_NOCHARGEL) + "','" + _
                  STR$(Sq1.ADJUST) + "','" + _
                  STR$(Sq1.ADJUSTL) + "','" + _
                  STR$(Sq1.SLIP) + "','" + _
                  STR$(Sq1.SLIPL) + "','" + _
                  STR$(Sq1.ENTRIES) + "','" + _
                  STR$(Sq1.SECONDS) + "','" + _
                  Sq1.REFERENCETEXT + "','" + _
                  STR$(Sq1.WRITEOFFDO) + "','" + _
                  STR$(Sq1.WRITEOFF) + "','" + _
                  STR$(Sq1.FOREIGNDO) + "','" + _
                  STR$(Sq1.FOREIGNLO) + "','" + _ ' Sq1.BUFFER5ST + "','" + _
                  Sq1.EXEMPTST + "','" + _
                  Sq1.MODELST + "','" + _
                  Sq1.UPDATED + "')" ' END OF FIELDS(Colums)

                  ERRCLEAR:
                  SQL_Stmt %SQL_STMT_IMMEDIATE, sSQLStatement
                  'Need error Checking here....
                  IF ERR = 0 THEN

                  LSET SQ1.UPDATED ="Y":
                  PUT #291,RLO,SQ1:

                  ELSE

                  COMPLETE&=0:

                  END IF

                  END IF

                  LOOP UNTIL EOF(291)

                  END IF

                  SLEEP 1000

                  LOOP:
                  SQLlastRecordRead = LOF(291)/900:

                  END SUB

                  ------------------
                  mailto:[email protected][email protected]</A>
                  mailto:[email protected][email protected]</A>

                  Comment


                  • #10
                    Joseph,

                    >> What I need to get to work now is how do I get the FUNCTION WINMAINX to be called where
                    in the Code should it be call from.... <<

                    your template is one for PBWin and GUI.
                    (John showed one template for PBCC)

                    SCM (Service Control Manager) launch service sub ServiceMain(..) in one thread and
                    communicate with e.g. StopService() in another thread. After some
                    groundwork ServiceMain() call InitService() and then it wait for the end of run

                    ' Wait for the signal to end
                    WaitForSingleObject glnghTerminateEvent, %INFINITE
                    Terminate 0

                    InitService() create another thread
                    ' Start the main thread for this service
                    glnghServiceThread = CreateThread(lpThreadAttributes, 0, CODEPTR(ServiceThread), 0, 0, ID)

                    and in this thread ServiceThread() you will find

                    FUNCTION ServiceThread(ID AS LONG) EXPORT AS LONG
                    LOCAL Result AS LONG
                    LOCAL Msg AS tagMsg
                    MSGBOX "In the Service Thread Function'

                    ' Run until we are killed
                    ' Result = WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
                    ' call WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
                    ' I had written a program and compiled it as an exe
                    ' To make it a service, I just renamed the WINMAIN to WINMAINX and then
                    ' call it like you see above
                    END FUNCTION

                    remove one of both remarks and you are done.

                    one side note. Because we are working with one thread more than the service itself
                    you need registering hMainWnd of your app in

                    GLOBAL hWindow AS LONG ' You should set this in your WINMAINX to the handle

                    and call message pump always. Otherwise your service will not terminate properly.

                    Hope this is of some help

                    Ahoj
                    -Uwe

                    PS. I have not tested your template but it looks good so far

                    PPS. remove

                    LOCAL Msg AS tagMsg
                    MSGBOX "In the Service Thread Function'

                    either


                    ------------------


                    [This message has been edited by Uwe Heyer (edited November 18, 2003).]

                    Comment


                    • #11
                      Please use the "code" and "/code" tags for us old guys with
                      poor eyesight. See the "UBB code" explanation to the left of the
                      reply box. Thanks.

                      [This message has been edited by John McWilliams (edited November 18, 2003).]

                      Comment


                      • #12
                        I am still having a problem the Service is Hanging when it tries to start.

                        I have not a glue what is the problem


                        "code"

                        $COMPILE EXE "t:\CIMservice.EXE"

                        #DIM ALL ' Force ALL vars to have to be declared
                        GLOBAL hWindow AS LONG ' You should set this in your WINMAINX to the handle
                        ' of your main window.
                        #IF NOT %DEF(%WINAPI)

                        #INCLUDE "C:\PBWin70\WinAPI\win32api.inc"

                        #ENDIF
                        #INCLUDE "c:\SQLTOOLS\SQLT_Pro.INC"
                        ' ---------------------
                        ' CONSTANT DECLARATIONS
                        ' ---------------------
                        %EVENTLOG_AUDIT_FAILURE = 16
                        %EVENTLOG_AUDIT_SUCCESS = 8
                        %EVENTLOG_ERROR_TYPE = 1
                        %EVENTLOG_INFORMATION_TYPE = 4
                        %EVENTLOG_SUCCESS = 0
                        %EVENTLOG_WARNING_TYPE = 2
                        ' This constant is used to set logging status.
                        %EVENTLOG_LEVEL = %EVENTLOG_AUDIT_FAILURE + %EVENTLOG_AUDIT_SUCCESS + %EVENTLOG_ERROR_TYPE + _
                        %EVENTLOG_INFORMATION_TYPE + %EVENTLOG_SUCCESS + %EVENTLOG_WARNING_TYPE
                        %SERVICE_WIN32_OWN_PROCESS = &H10&
                        %SERVICE_WIN32_SHARE_PROCESS = &H20&
                        %SERVICE_WIN32 = %SERVICE_WIN32_OWN_PROCESS + %SERVICE_WIN32_SHARE_PROCESS
                        %SERVICE_DEMAND_START = &H3&
                        %SERVICE_ERROR_NORMAL = &H1&
                        %WAIT_OBJECT_0 = 0&
                        ' -------------------------
                        ' FUNCTION/SUB DECLARATIONS
                        ' -------------------------
                        DECLARE FUNCTION Install() AS LONG
                        DECLARE FUNCTION InitService() AS LONG
                        DECLARE FUNCTION Uninstall() AS LONG
                        DECLARE FUNCTION WinErrorMessage(BYVAL ErrNumber AS LONG) AS STRING
                        DECLARE FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
                        DECLARE FUNCTION ServiceThread(ID AS LONG) AS LONG

                        DECLARE SUB Handler(BYVAL ControlValue AS DWORD)
                        DECLARE SUB LogNTEvent(Message AS STRING, LogCode AS LONG)
                        DECLARE SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
                        DECLARE SUB Terminate(ErrCode AS DWORD)
                        DECLARE SUB StopService()
                        DECLARE SUB PauseService()
                        DECLARE SUB ResumeService()
                        ' ----------------
                        ' GLOBAL VARIABLES
                        ' ----------------
                        GLOBAL gascServiceName AS ASCIIZ * 8
                        GLOBAL gascServiceDisplayName AS ASCIIZ * 256
                        GLOBAL gdwdhStatus AS DWORD
                        GLOBAL gdwdServiceState AS DWORD
                        GLOBAL gdwdServiceStatus AS DWORD
                        GLOBAL glnghInstance AS LONG
                        GLOBAL glnghPrvInst AS LONG
                        GLOBAL glnghTerminateEvent AS LONG
                        GLOBAL glnghServiceThread AS LONG
                        GLOBAL glngThreadResult AS LONG
                        GLOBAL glngRunningService AS LONG
                        GLOBAL glngPauseService AS LONG
                        GLOBAL glngCmdShow AS LONG
                        GLOBAL ptrCmdLine AS ASCIIZ PTR


                        FUNCTION WINMAIN(BYVAL hCurInst AS LONG, _
                        BYVAL hPrvInst AS LONG, _
                        CmdLine AS ASCIIZ PTR, _
                        BYVAL CmdShow AS LONG) EXPORT AS LONG

                        '================================================
                        ' This Must be Here Please do not move next line
                        SQL_Authorize &h284FA389
                        '================================================
                        DIM Result AS LONG
                        DIM CmdParm AS STRING
                        DIM udtSTE AS SERVICE_TABLE_ENTRY

                        ' Set the service name and display name here.

                        gascServiceName = "CIMSQL"
                        'gascServiceDisplayName = "CIM-ODBC"
                        'gascServiceDisplayName = "CIM-ODBC(tm)"
                        gascServiceDisplayName = "CIM-ODBC"

                        glngHInstance = hCurInst
                        glnghPrvInst = hPrvInst
                        glngCmdShow = CmdShow
                        ptrCmdLine = CmdLine
                        ' Get the Command Line parms
                        CmdParm = UCASE$(EXTRACT$(@CmdLine, ANY " ,." + CHR$(9)))
                        ' Get rid of any seperators
                        CmdParm = REMOVE$(CmdParm, ANY "-\/")
                        ' If the exe was executed with the Install parm then we just install and quit

                        IF CmdParm = "INSTALL" THEN
                        ' Install the service.
                        Result = Install()

                        IF ISFALSE Result THEN

                        MSGBOX "An error occured while trying to install this service

                        END IF

                        ' If the exe was executed with the UnInstall parm then we just uninstall and quit

                        ELSEIF CmdParm = "UNINSTALL" THEN
                        ' Uninstall the service.
                        Result = Uninstall()

                        IF ISFALSE Result THEN

                        MSGBOX "An error occured while trying to uninstall this service

                        END IF

                        ' No parms were given that we want so start up as service
                        ' This should only happen when the service control manager starts us

                        ELSE

                        udtSTE.lpServiceName = VARPTR(gascServiceName)
                        udtSTE.lpServiceProc = CODEPTR(ServiceMain)
                        Result = StartServiceCtrlDispatcher(udtSTE)

                        IF Result = 0 THEN
                        ExitProcess GetLastError()

                        END IF

                        END IF

                        FUNCTION = 0

                        END FUNCTION

                        ' Install the service into windows

                        FUNCTION Install() AS LONG

                        DIM dwdRet AS DWORD
                        DIM lngDQEnd AS LONG
                        DIM lngDQStart AS LONG
                        DIM lngHSCManager AS LONG
                        DIM lngHService AS LONG
                        DIM ascEXE AS ASCIIZ * %MAX_PATH
                        ON ERROR GOTO Error_Install
                        ' Assume a failure for now.

                        FUNCTION = %FALSE

                        lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)

                        IF lngHSCManager <> %NULL THEN
                        ' OK, we have a handle to the SCM.
                        ' Get the full EXE file path.
                        dwdRet = GetModuleFileName(glngHInstance, ascEXE, %MAX_PATH)
                        IF dwdRet <> 0 THEN
                        ' Install the service.
                        lngHService = CreateService(lngHSCManager, gascServiceName, gascServiceDisplayName, _
                        %SERVICE_ALL_ACCESS, %SERVICE_WIN32_OWN_PROCESS, _
                        %SERVICE_AUTO_START, %SERVICE_ERROR_NORMAL, _
                        ascEXE, BYVAL %NULL, BYVAL %NULL, _
                        BYVAL %NULL, BYVAL %NULL, BYVAL %NULL)
                        ' Close any service handles.

                        IF lngHService <> %NULL THEN

                        ' Success!

                        FUNCTION = %TRUE
                        CloseServiceHandle lngHService

                        END IF

                        CloseServiceHandle lngHSCManager

                        END IF

                        END IF

                        FUNCTION = %TRUE

                        EXIT FUNCTION

                        Error_Install:

                        FUNCTION = -1& * ERR
                        ON ERROR RESUME NEXT
                        ' Close any service handles.

                        IF lngHService <> %NULL THEN

                        CALL CloseServiceHandle(lngHService)

                        END IF

                        IF lngHSCManager <> %NULL THEN

                        CALL CloseServiceHandle(lngHSCManager)

                        END IF

                        END FUNCTION

                        ' Unistall the service

                        FUNCTION Uninstall() AS LONG

                        DIM lngHSCManager AS LONG
                        DIM lngHService AS LONG
                        ON ERROR GOTO Error_Uninstall
                        ' Assume a failure for now.
                        FUNCTION = %FALSE
                        lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)

                        IF lngHSCManager <> %NULL THEN
                        ' OK, we have a handle to the SCM.
                        ' Now open our service.
                        lngHService = OpenService(lngHSCManager, gascServiceName, %SERVICE_ALL_ACCESS)

                        IF lngHService <> %NULL THEN
                        ' Delete the service.

                        IF DeleteService(lngHService) <> %NULL THEN
                        ' Success!
                        FUNCTION = %TRUE

                        END IF

                        CloseServiceHandle lngHService

                        END IF

                        CloseServiceHandle lngHSCManager

                        END IF

                        EXIT FUNCTION

                        Error_Uninstall:

                        FUNCTION = -1& * ERR
                        ON ERROR RESUME NEXT
                        ' Close any service handles.

                        IF lngHService <> %NULL THEN

                        CloseServiceHandle lngHService

                        END IF

                        IF lngHSCManager <> %NULL THEN

                        CloseServiceHandle lngHSCManager

                        END IF

                        END FUNCTION

                        ' Start up the service

                        FUNCTION InitService() AS LONG

                        DIM lngRet AS LONG
                        DIM ID AS LONG
                        DIM udtSTE AS SERVICE_TABLE_ENTRY
                        DIM lpThreadAttributes AS SECURITY_ATTRIBUTES
                        ' Start the main thread for this service
                        glnghServiceThread = CreateThread(lpThreadAttributes, 0, CODEPTR(ServiceThread), 0, 0, ID)
                        ' Did the thread start OK

                        'JLB
                        CALL ServiceThread(glnghServiceThread)

                        IF glnghServiceThread = 0 THEN

                        FUNCTION = %FALSE

                        EXIT FUNCTION

                        ELSE

                        ' Set the global to running
                        glngRunningService = %TRUE
                        FUNCTION = %TRUE
                        EXIT FUNCTION

                        END IF

                        END FUNCTION

                        SUB PauseService()

                        ' Set the global indicating that we are not paused
                        glngPauseService = %TRUE
                        ' Let er rip
                        SuspendThread glnghServiceThread
                        END SUB

                        SUB ResumeService()
                        ' Set the global indicating that we are not paused
                        glngPauseService = %FALSE
                        ' Let er rip
                        ResumeThread glnghServiceThread
                        END SUB

                        SUB StopService()

                        ' Set the global flag indicating that the service is not running
                        glngRunningService = %FALSE
                        ' Set the event so the service will stop
                        SetEvent glnghTerminateEvent

                        END SUB

                        FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, _
                        ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG

                        DIM udtSS AS SERVICE_STATUS
                        ' Reset the global service status value.
                        gdwdServiceStatus = CurrentStatus
                        ' Setup the UDT.
                        udtSS.dwServiceType = %SERVICE_WIN32_OWN_PROCESS
                        udtSS.dwCurrentState = CurrentStatus
                        ' If we are the process of starting, then don't accept control events

                        IF CurrentStatus = %SERVICE_START_PENDING THEN

                        udtSS.dwControlsAccepted = 0

                        ELSE

                        ' Take what was given
                        udtSS.dwControlsAccepted = %SERVICE_ACCEPT_STOP + %SERVICE_ACCEPT_PAUSE_CONTINUE + %SERVICE_ACCEPT_SHUTDOWN

                        END IF

                        ' If a specific ServiceSpecificExitCode is defined, setup the Win32 exit code properly

                        IF ServiceSpecificExitCode = 0 THEN

                        udtSS.dwWin32ExitCode = ExitCode

                        ELSE

                        udtSS.dwWin32ExitCode = %ERROR_SERVICE_SPECIFIC_ERROR

                        END IF

                        ' Specific Exit Code
                        udtSS.dwServiceSpecificExitCode = ServiceSpecificExitCode
                        udtSS.dwCheckPoint = Checkpoint
                        udtSS.dwWaitHint = WaitHint

                        IF SetServiceStatus(gdwdHStatus, udtSS) = 0 THEN

                        ' Something went wrong so stop the service
                        StopService
                        FUNCTION = %FALSE

                        ELSE

                        FUNCTION = %TRUE

                        END IF

                        END FUNCTION

                        SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)

                        LOCAL Result AS LONG
                        LOCAL lpEventAttributes AS SECURITY_ATTRIBUTES
                        ' Register with the SCM
                        gdwdHStatus = RegisterServiceCtrlHandler(gascServiceName, CODEPTR(Handler))
                        ' Did it work

                        IF gdwdHStatus = 0 THEN

                        ' No, so terminate
                        Terminate GetLastError()

                        EXIT SUB

                        END IF

                        ' Service has been registered and startup is pending

                        IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 1, 5000) THEN

                        Terminate GetLastError()

                        EXIT SUB

                        END IF

                        ' Create the termination event
                        glnghTerminateEvent = CreateEvent(lpEventAttributes, %TRUE, %FALSE, "")

                        IF glnghTerminateEvent = 0 THEN

                        Terminate GetLastError()
                        EXIT SUB

                        END IF

                        ' Service startup is still pending
                        IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 2, 1000) THEN

                        Terminate GetLastError()
                        EXIT SUB

                        END IF

                        ' Start the service
                        Result = InitService()

                        IF ISFALSE Result THEN

                        ' Oops
                        Terminate GetLastError()
                        EXIT SUB

                        END IF
                        ' Service is now running

                        IF ISFALSE SendStatus(%SERVICE_RUNNING, %NO_ERROR, 0, 0, 0) THEN

                        Terminate GetLastError()

                        EXIT SUB

                        END IF

                        ' Wait for the signal to end
                        WaitForSingleObject glnghTerminateEvent, %INFINITE
                        Terminate 0

                        EXIT SUB

                        END SUB

                        SUB Terminate(ErrCode AS DWORD)

                        ' If the Terminate Event has already been created then destroy it

                        IF glnghTerminateEvent <> 0 THEN

                        CloseHandle glnghTerminateEvent

                        END IF

                        IF gdwdHStatus <> 0 THEN

                        ' Send a message to the SCM and tell them that we are stopping

                        SendStatus %SERVICE_STOPPED, ErrCode, 0&, 0&, 0&

                        END IF

                        ' If the thread has started, then kill it

                        IF glnghServiceThread <> 0 THEN

                        ' Not normally here in a service
                        ' However, this program was written as an executable first and converted into a service
                        ' There is a global hWindow that is set in the WINMAINX when we start
                        ' We need to destroy the window so the WINMAINX will come back to us
                        ' Is it a valid window

                        IF IsWindow(hWindow) THEN

                        ' Yes so destroy it
                        DestroyWindow hWindow
                        ' This will cause the Message Loop in your WINMAINX to exit and return

                        END IF

                        ' Close the thread

                        CloseHandle glnghServiceThread

                        END IF

                        END SUB

                        SUB Handler(BYVAL ControlValue AS DWORD)

                        ' This procedure (by its' name) handles all service requests.
                        LOCAL Result AS LONG
                        ON ERROR RESUME NEXT
                        ' There is no Start option because the ServiceMain takes care of starting the service

                        SELECT CASE ControlValue

                        CASE %SERVICE_CONTROL_STOP
                        ' Set the global Status
                        gdwdServiceState = %SERVICE_STOP_PENDING
                        ' Tell the SCM that we are stopping
                        SendStatus %SERVICE_STOP_PENDING, %NO_ERROR, 0&, 1, 5000
                        ' Stop the service
                        StopService

                        CASE %SERVICE_CONTROL_PAUSE
                        ' Are we running and not paused

                        IF (ISTRUE glngRunningService) AND (ISFALSE glngPauseService) THEN

                        ' Tell the SCM that we are pausing
                        SendStatus %SERVICE_PAUSE_PENDING, %NO_ERROR, 0, 1, 1000
                        ' Pause it
                        PauseService
                        ' Set the current state
                        gdwdServiceState = %SERVICE_PAUSED

                        END IF

                        CASE %SERVICE_CONTROL_CONTINUE
                        ' Are we running and paused

                        IF (ISTRUE glngRunningService) AND (ISTRUE glngPauseService) THEN

                        ' Tell the SCM that we are un pausing
                        SendStatus %SERVICE_CONTINUE_PENDING, %NO_ERROR, 0, 1, 1000
                        ' Resume the service
                        ResumeService
                        ' Set the current state
                        gdwdServiceState = %SERVICE_RUNNING

                        END IF

                        CASE %SERVICE_CONTROL_INTERROGATE
                        ' Don't need to do anything
                        ' We will send the current status below


                        CASE %SERVICE_CONTROL_SHUTDOWN
                        ' We don't do anything with a shutdown

                        EXIT SUB

                        END SELECT

                        ' Tell the SCM the new status

                        SendStatus gdwdServiceState, %NO_ERROR, 0, 0, 0

                        END SUB

                        FUNCTION ServiceThread(ID AS LONG) EXPORT AS LONG

                        LOCAL Result AS LONG
                        'LOCAL Msg AS tagMsg

                        'MSGBOX "In the Service Thread Function'

                        ' Run until we are killed

                        ' Result = WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
                        CALL WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)

                        ' I had written a program and compiled it as an exe
                        ' To make it a service, I just renamed the WINMAIN to WINMAINX and then
                        ' call it like you see above

                        END FUNCTION

                        'SQL Processing Routine

                        %FALSE = 0

                        DECLARE SUB Sql_Update_Display1
                        DECLARE SUB OpenSQLFile1()
                        ' SQL
                        TYPE SQLTYPE1 ' LEN 900

                        SQLTYPE AS STRING * 1 ' = "1" ' SqlTypeST
                        DATEST AS STRING * 10 ' Date
                        USERCODEST AS STRING * 4 ' UserCode
                        USERNAMEST AS STRING * 20 ' UserName
                        TIME AS STRING * 10 ' TimeST
                        SESSION AS LONG ' SessionLO
                        TRANSACTION AS LONG ' TransactionLO
                        SESSIONREF AS STRING * 13 ' SessionRefST
                        TRANSACTIONREF AS STRING * 13 ' TransactionRefST
                        BAGORORDERNO AS STRING * 8 ' BagOrderNumber
                        BAGORORDERREF AS STRING * 13 ' BagOrderRef
                        BANKGROUPST AS STRING * 3 ' ' BankGroup
                        DEPARTMENTLO AS LONG ' ' Department
                        TRANSITST AS STRING * 13 ' ROUTING AND TRANSIT NO ' Transit
                        TYPEST AS STRING * 1 ' ' Type
                        CODEST AS STRING * 1 ' ' Code
                        CLASSST AS STRING * 5 ' ' Class
                        REFERENCEST AS STRING * 13 ' CUST NUMBER /ACCOUNT ' Reference
                        CHARGE_TOST AS STRING * 13 ' ' Charge_To
                        BRANCH_NUMBER AS DOUBLE ' LOCATION 10 DIGIT ' Branch_Number
                        NAMEST AS STRING * 25 ' NAME ' Name
                        ADD1ST AS STRING * 25 ' ADDRESS LINE 1 ' Address1
                        ADD2ST AS STRING * 25 ' ADDRESS LINE 2 ' Address2
                        ADD3ST AS STRING * 25 ' ADDRESS LINE 3 ' Address3
                        ADD4ST AS STRING * 25 ' ADDRESS LINE 4 ' Address4
                        PHONEST AS STRING * 15 ' CUSTOMER PHONE ' Phone
                        FAXST AS STRING * 15 ' CALLBACK / FAX PHONE ' Fax
                        TAXIDST AS STRING * 13 ' TAX ID NUMBER ' TaxId
                        SHIPTOST AS STRING * 13 ' DEFAULT SHIP TO ROUTE ' ShopTo
                        GLCOSTCENTERLO AS LONG ' ' GlCostCenter
                        GLDEPOSITACCTLO AS LONG ' ' GlDepositAcct
                        GLORDERACCTLO AS LONG ' ' GlOrderAcct
                        GLADJUSTACCTLO AS LONG ' ' GlAdjustAcct
                        RETAILBANKLO AS LONG ' ' RetailBank
                        TRANSACTIONTYPE AS STRING * 10 ' TransactionType
                        ENV_DEP_DECLARED AS DOUBLE ' Env_Dep_Declared
                        DECLARED AS DOUBLE ' Declared
                        TOTAL_ACTUAL AS DOUBLE ' Total_Actual
                        DIFFERENCE AS DOUBLE ' Difference
                        CURRENCYTOT AS DOUBLE ' CurrencyTot
                        COINTOT AS DOUBLE ' CoinTot
                        CASHTOT AS DOUBLE ' CashTot
                        ONES AS DOUBLE ' Ones
                        TWOS AS DOUBLE ' Twos
                        FIVES AS DOUBLE ' Fives
                        TENS AS DOUBLE ' Tens
                        TWENTIES AS DOUBLE ' Twenties
                        FIFTIES AS DOUBLE ' Fifties
                        HUNDREDS AS DOUBLE ' Hundreds
                        CURR_OTHER AS DOUBLE ' Currency_Other
                        PENNIES AS DOUBLE ' Pennies
                        NICKELS AS DOUBLE ' Nickles
                        DIMES AS DOUBLE ' Dimes
                        QUARTERS AS DOUBLE ' Quarters
                        HALVES AS DOUBLE ' Halves
                        DOLLARS AS DOUBLE ' Dollars
                        COIN_OTHER AS DOUBLE ' Coin_Other
                        NOTE_PIECES AS LONG ' Note_Pieces
                        STRAP_PIECES AS LONG ' Strap_Pieces
                        BRICK_PIECES AS LONG ' Brick_Pieces
                        STAMP_VALUE AS DOUBLE ' Stamp_Value
                        STAMP_PIECES AS LONG ' Stamp_Pieces
                        COIN_PIECES AS LONG ' Coin_Pieces
                        ROLLS_PIECES AS LONG ' Rolls_Pieces
                        BOXES_PIECES AS LONG ' Boxes_Pieces
                        BAGS_PIECES AS LONG ' Bags_Pieces
                        GOLD_VALUE AS DOUBLE ' Gold_Value
                        GOLD_PIECES AS LONG ' Gold_Pieces
                        MIX_VALUE AS DOUBLE ' Mix_Value
                        MIX_PIECES AS LONG ' Mix_Pieces
                        MISC_DOCUMENT AS DOUBLE ' Misc_Document
                        MISC_DOCUMENTL AS LONG ' Misc_DocumentL
                        MERCHANDISE AS DOUBLE ' Merchandise
                        MERCHANDISEL AS LONG ' MerchandiseL
                        CHECKS AS DOUBLE ' Checks
                        CHECKSL AS LONG ' ChecksL
                        CASHIER_CHECKS AS DOUBLE ' Cashier_Checks
                        CASHIER_CHECKSL AS LONG ' Cashier_ChecksL
                        CREDIT_CARD AS DOUBLE ' Credit_Card
                        CREDIT_CARDL AS LONG ' Credit_CardL
                        CREDIT_NOCHARGE AS DOUBLE ' Credit_NoCharge
                        CREDIT_NOCHARGEL AS LONG ' Credit_NoChargeL
                        ADJUST AS DOUBLE ' Adjust
                        ADJUSTL AS LONG ' AdjustL
                        SLIP AS DOUBLE ' Slip
                        SLIPL AS LONG ' SlipL
                        ENTRIES AS LONG ' Entries
                        SECONDS AS LONG ' Seconds
                        REFERENCETEXT AS STRING * 30: ' ReferenceTextST
                        WRITEOFFDO AS DOUBLE ' = TWRITEOFFDO: 'AS DOUBLE ' WriteOff
                        WRITEOFF AS LONG ' = WRITEOFF: 'AS LONG ' WriteOffD
                        FOREIGNDO AS DOUBLE ' ' ForeignD
                        FOREIGNLO AS LONG ' ForeignL
                        'Problem with The Buffer*
                        '===================================================================
                        BUFFER5ST AS STRING * 25 ' Buffer5
                        BUFFER6ST AS STRING * 13 ' Buffer6
                        BUFFER7ST AS STRING * 1 ' Buffer7
                        BUFFER8ST AS STRING * 1 ' Buffer8
                        BUFFER9ST AS STRING * 1 ' Buffer9
                        BUFFERAST AS STRING * 1 ' BufferA
                        BUFFERBST AS STRING * 1 ' BufferB
                        BUFFERCST AS STRING * 1 ' BufferC
                        BUFFERDST AS STRING * 1 ' BufferD
                        BUFFEREST AS STRING * 1 ' BufferE
                        BUFFERFST AS STRING * 1 ' BufferF
                        BUFFERGST AS STRING * 1 ' BufferG
                        BUFFERHST AS STRING * 1 ' BufferH
                        BUFFERIST AS STRING * 1 ' BufferI
                        BUFFERJST AS STRING * 77 ' BufferJ
                        '===================================================================
                        EXEMPTST AS STRING * 1 ' Exemp
                        MODELST AS STRING * 1 ' Model
                        UPDATED AS STRING * 1 ' Y=UPDATED ' Updated

                        END TYPE

                        'Variables

                        GLOBAL DB1OPEN AS LONG
                        GLOBAL DisplayRecord AS STRING
                        GLOBAL CIMSQLFILE$
                        GLOBAL SQ1 AS SQLTYPE1


                        FUNCTION WINMAINX(BYVAL glngHInstance AS LONG , _
                        BYVAL glnghPrvInst AS LONG ,_
                        BYVAL ptrCmdLine AS ASCIIZ PTR * 40 ,_
                        BYVAL glngCmdShow AS LONG)EXPORT AS LONG

                        ' MSGBOX "In the WInMainX"

                        LOCAL hWindow AS LONG

                        ON ERROR RESUME NEXT:
                        ERRCLEAR:
                        GLOBAL sqlCount&
                        SQL_Init
                        ' SQL_OpenDB "GARDENIADATA.DSN" ' This gives the Login ODBC Box

                        Sql_OpenDB "GardeniaDb2.DSN"
                        'SQL_OpenDB("HMGardenia.DSN") ' Home Lan ConnectionSQL Server
                        'SQL_OpenDB("HomeGardenia.DSN")

                        CALL OpenSQLFile1 '
                        CALL Sql_Update_Display1()

                        END FUNCTION

                        SUB OpenSQLFile1() ' SQL_FORMAT_1.CIM

                        CIMSQLFILE$ = "t:\SQL_FORMAT_1.CIM"
                        ON ERROR RESUME NEXT:

                        CLOSE #(291):
                        ERRCLEAR:
                        OPEN CIMSQLFILE$ FOR RANDOM LOCK SHARED AS #291 LEN=900:
                        DB1OPEN = 1
                        LOCK#(291): UNLOCK#(291):

                        ' Error Checking Here...

                        END SUB

                        SUB Sql_Update_Display1()

                        ON ERROR RESUME NEXT: ERRCLEAR:
                        LOCAL X&,Y&,COMPLETE&,lenf&:
                        LOCAL A$,B$,C$:
                        LOCAL SQLLASTRECORDREAD AS LONG

                        DIM sSQLStatement AS GLOBAL STRING
                        DIM RLO AS LONG
                        DIM SQLSTARTRECORD AS LONG

                        IF SQLLASTRECORDREAD = 0 THEN

                        SQLSTARTRECORD = 1 ' LOC(291)/900:

                        ELSE

                        IF SQLSTARTRECORD <> SQLLASTRECORDREAD THEN SQLSTARTRECORD = SQLLASTRECORDREAD:

                        END IF

                        DO:

                        COMPLETE& = 1:

                        OpenSQLFile1()

                        IF LOF(291)>800 THEN

                        RLO=0:

                        DO:

                        SLEEP 1000:

                        INCR RLO: ' NEW

                        GET #291 , RLO, SQ1:

                        INCR SQLStartRecord

                        IF SQ1.UPDATED <> "Y" THEN

                        IF DB1OPEN = 0 THEN CALL OpenSQLFile1:

                        sSQLStatement = "Insert INTO Transactions (SqlType, Date, UserCode, " + _
                        "UserName, TimeST ,SessionLO, TransactionLO, " + _
                        "SessionRefST, " + _
                        "TransactionRefST, " + _
                        "BagOrderNumber, " + _
                        "BagOrderRefST, " + _
                        "BankGroup , " + _
                        "Department," + _
                        "Transit," + _
                        "Type, " + _
                        "Code, " + _
                        "Class, " + _
                        "Reference, " + _
                        "Charge_To, " + _
                        "Branch_Number, " + _
                        "Name, " + _
                        "Address1, " + _
                        "Address2, " + _
                        "Address3, " + _
                        "Address4, " + _
                        "Phone, " + _
                        "Fax, " + _
                        "TaxId, " + _
                        "ShipTo, " + _
                        "GlCostCenter, " + _
                        "GlDepositAcct, " + _
                        "GlOrderAcct, " + _
                        "GlAdjustAcct, " + _
                        "RetailBank, " + _
                        "TransactionType, " + _
                        "Env_Dep_Declared,Declared, " + _
                        "Total_Actual, " + _
                        "Difference, " + _
                        "CurrencyTot, " + _
                        "CoinTot, " + _
                        "CashTot, " + _
                        "Ones, " + _
                        "Twos, " + _
                        "Fives, " + _
                        "Tens, " + _
                        "Twenties, " + _
                        "Fifties, " + _
                        "Hundreds, " + _
                        "Currency_Other, " + _
                        "Pennies, " + _
                        "Nickles, " + _
                        "Dimes, " + _
                        "Quarters, " + _
                        "Halves, " + _
                        "Dollars, " + _
                        "Coin_Other, " + _
                        "Note_Pieces, " + _
                        "Strap_Pieces, " + _
                        "Brick_Pieces, " + _
                        "Stamp_Value, " + _
                        "Stamp_Pieces, " + _
                        "Coin_Pieces, " + _
                        "Rolls_Pieces, " + _
                        "Boxes_Pieces, " + _
                        "Bags_Pieces, " + _
                        "Gold_Value, " + _
                        "Gold_Pieces, " + _
                        "Mix_Value, " + _
                        "Mix_Pieces, " + _
                        "Misc_Document, " + _
                        "Misc_DocumentL, " + _
                        "Merchandise, " + _
                        "MerchandiseL, " + _
                        "Checks, " + _
                        "ChecksL, " + _
                        "Cashier_Checks, " + _
                        "Cashier_ChecksL, " + _
                        "Credit_Card, " + _
                        "Credit_CardL, " + _
                        "Credit_NoCharge, " + _
                        "Credit_NoChargeL, " + _
                        "Adjust, " + _
                        "AdjustL, " + _
                        "Slip, " + _
                        "SlipL, " + _
                        "Entries, " + _
                        "Seconds, " + _
                        "ReferenceTextST," + _
                        "WriteOff, " + _
                        "WriteOffD, " + _
                        "ForeignD, " + _
                        "ForeignL, " + _ '"Buffer5, " "Buffer6, " "Buffer7, " "Buffer8, " "Buffer9, " "BufferA, "
                        "Exemp, " + _
                        "Model, " + _
                        "Updated)" + _ ' This is the Last Field in Database
                        " VALUES ('" + Sq1.SqlType + "','" + _
                        Sq1.DateST + "','" + _
                        Sq1.UserCodeST + "','" + _
                        Sq1.USERNAMEST + "','" + _
                        Sq1.TIME + "','" + _
                        STR$(Sq1.SESSION) + "','" + _
                        STR$(Sq1.TRANSACTION) + "','" + _
                        Sq1.SESSIONREF + "','" + _
                        Sq1.TRANSACTIONREF + "','" + _
                        Sq1.BAGORORDERNO + "','" + _
                        Sq1.BAGORORDERREF + "','" + _
                        Sq1.BANKGROUPST + "','" + _
                        STR$(Sq1.DEPARTMENTLO) + "','" + _
                        Sq1.TRANSITST + "','" + _
                        Sq1.TYPEST + "','" + _
                        Sq1.CODEST + "','" + _
                        Sq1.CLASSST + "','" + _
                        Sq1.REFERENCEST + "','" + _
                        Sq1.CHARGE_TOST + "','" + _
                        STR$(Sq1.BRANCH_NUMBER) + "','" + _
                        Sq1.NAMEST + "','" + _
                        Sq1.ADD1ST + "','" + _
                        Sq1.ADD2ST + "','" + _
                        Sq1.ADD3ST + "','" + _
                        Sq1.ADD4ST + "','" + _
                        Sq1.PHONEST + "','" + _
                        Sq1.FAXST + "','" + _
                        Sq1.TAXIDST + "','" + _
                        Sq1.SHIPTOST + "','" + _
                        STR$(Sq1.GLCOSTCENTERLO) + "','" + _
                        STR$(Sq1.GLDEPOSITACCTLO) + "','" + _
                        STR$(Sq1.GLORDERACCTLO) + "','" + _
                        STR$(Sq1.GLADJUSTACCTLO) + "','" + _
                        STR$(Sq1.RETAILBANKLO)+ "','" + _
                        Sq1.TRANSACTIONTYPE + "','" + _
                        STR$(Sq1.Env_Dep_Declared) + "','" + _
                        STR$(Sq1.DECLARED) + "','" + _
                        STR$(Sq1.TOTAL_ACTUAL) + "','" + _
                        STR$(Sq1.DIFFERENCE) + "','" + _
                        STR$(Sq1.CURRENCYTOT) + "','" + _
                        STR$(Sq1.COINTOT) + "','" + _
                        STR$(Sq1.CASHTOT) + "','" + _
                        STR$(Sq1.ONES) + "','" + _
                        STR$(Sq1.TWOS) + "','" + _
                        STR$(Sq1.FIVES) + "','" + _
                        STR$(Sq1.TENS) + "','" + _
                        STR$(Sq1.TWENTIES) + "','" + _
                        STR$(Sq1.FIFTIES) + "','" + _
                        STR$(Sq1.HUNDREDS) + "','" + _
                        STR$(Sq1.CURR_OTHER) + "','" + _
                        STR$(Sq1.PENNIES) + "','" + _
                        STR$(Sq1.NICKELS) + "','" + _
                        STR$(Sq1.DIMES) + "','" + _
                        STR$(Sq1.QUARTERS) + "','" + _
                        STR$(Sq1.HALVES) + "','" + _
                        STR$(Sq1.DOLLARS) + "','" + _
                        STR$(Sq1.COIN_OTHER) + "','" + _
                        STR$(Sq1.NOTE_PIECES) + "','" + _
                        STR$(Sq1.STRAP_PIECES) + "','" + _
                        STR$(Sq1.BRICK_PIECES) + "','" + _
                        STR$(Sq1.STAMP_VALUE) + "','" + _
                        STR$(Sq1.STAMP_PIECES) + "','" + _
                        STR$(Sq1.COIN_PIECES) + "','" + _
                        STR$(Sq1.ROLLS_PIECES) + "','" + _
                        STR$(Sq1.BOXES_PIECES) + "','" + _
                        STR$(Sq1.BAGS_PIECES) + "','" + _
                        STR$(Sq1.GOLD_VALUE) + "','" + _
                        STR$(Sq1.GOLD_PIECES) + "','" + _
                        STR$(Sq1.MIX_VALUE) + "','" + _
                        STR$(Sq1.MIX_PIECES) + "','" + _
                        STR$(Sq1.MISC_DOCUMENT) + "','" + _
                        STR$(Sq1.MISC_DOCUMENTL) + "','" + _
                        STR$(Sq1.MERCHANDISE) + "','" + _
                        STR$(Sq1.MERCHANDISEL) + "','" + _
                        STR$(Sq1.CHECKS) + "','" + _
                        STR$(Sq1.CHECKSL) + "','" + _
                        STR$(Sq1.CASHIER_CHECKS) + "','" + _
                        STR$(Sq1.CASHIER_CHECKSL) + "','" + _
                        STR$(Sq1.CREDIT_CARD) + "','" + _
                        STR$(Sq1.CREDIT_CARDL) + "','" + _
                        STR$(Sq1.CREDIT_NOCHARGE) + "','" + _
                        STR$(Sq1.CREDIT_NOCHARGEL) + "','" + _
                        STR$(Sq1.ADJUST) + "','" + _
                        STR$(Sq1.ADJUSTL) + "','" + _
                        STR$(Sq1.SLIP) + "','" + _
                        STR$(Sq1.SLIPL) + "','" + _
                        STR$(Sq1.ENTRIES) + "','" + _
                        STR$(Sq1.SECONDS) + "','" + _
                        Sq1.REFERENCETEXT + "','" + _
                        STR$(Sq1.WRITEOFFDO) + "','" + _
                        STR$(Sq1.WRITEOFF) + "','" + _
                        STR$(Sq1.FOREIGNDO) + "','" + _
                        STR$(Sq1.FOREIGNLO) + "','" + _ ' Sq1.BUFFER5ST + "','" + _
                        Sq1.EXEMPTST + "','" + _
                        Sq1.MODELST + "','" + _
                        Sq1.UPDATED + "')" ' END OF FIELDS(Colums)

                        ERRCLEAR:
                        SQL_Stmt %SQL_STMT_IMMEDIATE, sSQLStatement
                        'Need error Checking here....
                        IF ERR = 0 THEN

                        LSET SQ1.UPDATED ="Y":
                        PUT #291,RLO,SQ1:

                        ELSE

                        COMPLETE&=0:

                        END IF

                        END IF

                        LOOP UNTIL EOF(291)

                        END IF

                        SLEEP 1000

                        LOOP:
                        SQLlastRecordRead = LOF(291)/900:

                        END SUB


                        "/code"

                        ------------------
                        mailto:[email protected][email protected]</A>
                        mailto:[email protected][email protected]</A>

                        Comment


                        • #13
                          This is the Correct code....
                          Sorry for the Mistake.

                          This is the Source Code that is still not working

                          Code:
                          '-----------------------------------------------------------------------
                          
                          $COMPILE EXE "t:\CIMservice.EXE"
                          
                          #DIM ALL                            ' Force ALL vars to have to be declared
                          GLOBAL hWindow AS LONG              ' You should set this in your WINMAINX to the handle
                                                              ' of your main window.
                          #IF NOT %DEF(%WINAPI)
                          
                              #INCLUDE "C:\PBWin70\WinAPI\win32api.inc"
                          
                          #ENDIF
                          #INCLUDE "c:\SQLTOOLS\SQLT_Pro.INC"
                          '  ---------------------
                          '  CONSTANT DECLARATIONS
                          '  ---------------------
                          %EVENTLOG_AUDIT_FAILURE                 = 16
                          %EVENTLOG_AUDIT_SUCCESS                 = 8
                          %EVENTLOG_ERROR_TYPE                    = 1
                          %EVENTLOG_INFORMATION_TYPE              = 4
                          %EVENTLOG_SUCCESS                       = 0
                          %EVENTLOG_WARNING_TYPE                  = 2
                          '  This constant is used to set logging status.
                          %EVENTLOG_LEVEL = %EVENTLOG_AUDIT_FAILURE + %EVENTLOG_AUDIT_SUCCESS + %EVENTLOG_ERROR_TYPE + _
                                            %EVENTLOG_INFORMATION_TYPE + %EVENTLOG_SUCCESS + %EVENTLOG_WARNING_TYPE
                          %SERVICE_WIN32_OWN_PROCESS              = &H10&
                          %SERVICE_WIN32_SHARE_PROCESS            = &H20&
                          %SERVICE_WIN32                          = %SERVICE_WIN32_OWN_PROCESS + %SERVICE_WIN32_SHARE_PROCESS
                          %SERVICE_DEMAND_START                   = &H3&
                          %SERVICE_ERROR_NORMAL                   = &H1&
                          %WAIT_OBJECT_0                          = 0&
                          '  -------------------------
                          '  FUNCTION/SUB DECLARATIONS
                          '  -------------------------
                          DECLARE FUNCTION Install() AS LONG
                          DECLARE FUNCTION InitService() AS LONG
                          DECLARE FUNCTION Uninstall() AS LONG
                          DECLARE FUNCTION WinErrorMessage(BYVAL ErrNumber AS LONG) AS STRING
                          DECLARE FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
                          DECLARE FUNCTION ServiceThread(ID AS LONG) AS LONG
                          
                          DECLARE SUB Handler(BYVAL ControlValue AS DWORD)
                          DECLARE SUB LogNTEvent(Message AS STRING, LogCode AS LONG)
                          DECLARE SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
                          DECLARE SUB Terminate(ErrCode AS DWORD)
                          DECLARE SUB StopService()
                          DECLARE SUB PauseService()
                          DECLARE SUB ResumeService()
                          '  ----------------
                          '  GLOBAL VARIABLES
                          '  ----------------
                          GLOBAL gascServiceName                      AS ASCIIZ * 8
                          GLOBAL gascServiceDisplayName               AS ASCIIZ * 256
                          GLOBAL gdwdhStatus                          AS DWORD
                          GLOBAL gdwdServiceState                     AS DWORD
                          GLOBAL gdwdServiceStatus                    AS DWORD
                          GLOBAL glnghInstance                        AS LONG
                          GLOBAL glnghPrvInst                         AS LONG
                          GLOBAL glnghTerminateEvent                  AS LONG
                          GLOBAL glnghServiceThread                   AS LONG
                          GLOBAL glngThreadResult                     AS LONG
                          GLOBAL glngRunningService                   AS LONG
                          GLOBAL glngPauseService                     AS LONG
                          GLOBAL glngCmdShow                          AS LONG
                          GLOBAL ptrCmdLine                           AS ASCIIZ PTR
                          
                          
                          FUNCTION WINMAIN(BYVAL hCurInst AS LONG, _
                                              BYVAL hPrvInst AS LONG, _
                                                  CmdLine AS ASCIIZ PTR, _
                                                      BYVAL CmdShow AS LONG) EXPORT AS LONG
                          
                              '================================================
                              ' This Must be Here Please do not move next line
                              SQL_Authorize  &h284FA389
                              '================================================
                              DIM Result              AS LONG
                              DIM CmdParm             AS STRING
                              DIM udtSTE              AS SERVICE_TABLE_ENTRY
                          
                              '  Set the service name and display name here.
                          
                              gascServiceName             = "CIMSQL"
                              'gascServiceDisplayName      = "CIM-ODBC"
                              'gascServiceDisplayName      = "CIM-ODBC(tm)"
                              gascServiceDisplayName      = "CIM-ODBC"
                          
                              glngHInstance               = hCurInst
                              glnghPrvInst                = hPrvInst
                              glngCmdShow                 = CmdShow
                              ptrCmdLine                  = CmdLine
                              ' Get the Command Line parms
                              CmdParm = UCASE$(EXTRACT$(@CmdLine, ANY " ,." + CHR$(9)))
                              ' Get rid of any seperators
                              CmdParm = REMOVE$(CmdParm, ANY "-\/")
                              ' If the exe was executed with the Install parm then we just install and quit
                          
                              IF CmdParm = "INSTALL" THEN
                                  '  Install the service.
                                  Result = Install()
                          
                                  IF ISFALSE Result THEN
                          
                                      MSGBOX "An error occured while trying to install this service
                          
                                  END IF
                          
                              ' If the exe was executed with the UnInstall parm then we just uninstall and quit
                          
                              ELSEIF CmdParm = "UNINSTALL" THEN
                                  '  Uninstall the service.
                                  Result = Uninstall()
                          
                                  IF ISFALSE Result THEN
                          
                                      MSGBOX "An error occured while trying to uninstall this service
                          
                                  END IF
                          
                              ' No parms were given that we want so start up as service
                              ' This should only happen when the service control manager starts us
                          
                              ELSE
                          
                                  udtSTE.lpServiceName = VARPTR(gascServiceName)
                                  udtSTE.lpServiceProc = CODEPTR(ServiceMain)
                                  Result = StartServiceCtrlDispatcher(udtSTE)
                          
                                  IF Result = 0 THEN
                                      ExitProcess GetLastError()
                          
                                  END IF
                          
                              END IF
                          
                              FUNCTION = 0
                          
                          END FUNCTION
                          
                          ' Install the service into windows
                          
                          FUNCTION Install() AS LONG
                          
                              DIM dwdRet                          AS DWORD
                              DIM lngDQEnd                        AS LONG
                              DIM lngDQStart                      AS LONG
                              DIM lngHSCManager                   AS LONG
                              DIM lngHService                     AS LONG
                              DIM ascEXE                          AS ASCIIZ * %MAX_PATH
                              ON ERROR GOTO Error_Install
                              '  Assume a failure for now.
                          
                              FUNCTION = %FALSE
                          
                              lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)
                          
                              IF lngHSCManager <> %NULL THEN
                                  '  OK, we have a handle to the SCM.
                                  '  Get the full EXE file path.
                                  dwdRet = GetModuleFileName(glngHInstance, ascEXE, %MAX_PATH)
                                  IF dwdRet <> 0 THEN
                                      '  Install the service.
                                      lngHService = CreateService(lngHSCManager, gascServiceName, gascServiceDisplayName, _
                                                                  %SERVICE_ALL_ACCESS, %SERVICE_WIN32_OWN_PROCESS, _
                                                                  %SERVICE_AUTO_START, %SERVICE_ERROR_NORMAL, _
                                                                  ascEXE, BYVAL %NULL, BYVAL %NULL, _
                                                                  BYVAL %NULL, BYVAL %NULL, BYVAL %NULL)
                                      '  Close any service handles.
                          
                                      IF lngHService <> %NULL THEN
                          
                                          '  Success!
                          
                                          FUNCTION = %TRUE
                                          CloseServiceHandle lngHService
                          
                                      END IF
                          
                                      CloseServiceHandle lngHSCManager
                          
                                  END IF
                          
                              END IF
                          
                              FUNCTION = %TRUE
                          
                              EXIT FUNCTION
                          
                          Error_Install:
                          
                              FUNCTION = -1& * ERR
                              ON ERROR RESUME NEXT
                              '  Close any service handles.
                          
                              IF lngHService <> %NULL THEN
                          
                                  CALL CloseServiceHandle(lngHService)
                          
                              END IF
                          
                              IF lngHSCManager <> %NULL THEN
                          
                                  CALL CloseServiceHandle(lngHSCManager)
                          
                              END IF
                          
                          END FUNCTION
                          
                          ' Unistall the service
                          
                          FUNCTION Uninstall() AS LONG
                          
                              DIM lngHSCManager               AS LONG
                              DIM lngHService                 AS LONG
                              ON ERROR GOTO Error_Uninstall
                              '  Assume a failure for now.
                              FUNCTION = %FALSE
                              lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)
                          
                              IF lngHSCManager <> %NULL THEN
                                  '  OK, we have a handle to the SCM.
                                  '  Now open our service.
                                  lngHService = OpenService(lngHSCManager, gascServiceName, %SERVICE_ALL_ACCESS)
                          
                                  IF lngHService <> %NULL THEN
                                      '  Delete the service.
                          
                                      IF DeleteService(lngHService) <> %NULL THEN
                                          '  Success!
                                          FUNCTION = %TRUE
                          
                                      END IF
                          
                                      CloseServiceHandle lngHService
                          
                                  END IF
                          
                                  CloseServiceHandle lngHSCManager
                          
                              END IF
                          
                              EXIT FUNCTION
                          
                          Error_Uninstall:
                          
                              FUNCTION = -1& * ERR
                              ON ERROR RESUME NEXT
                              '  Close any service handles.
                          
                              IF lngHService <> %NULL THEN
                          
                                  CloseServiceHandle lngHService
                          
                              END IF
                          
                              IF lngHSCManager <> %NULL THEN
                          
                                  CloseServiceHandle lngHSCManager
                          
                              END IF
                          
                          END FUNCTION
                          
                          '  Start up the service
                          
                          FUNCTION InitService() AS LONG
                          
                              DIM lngRet                      AS LONG
                              DIM ID                          AS LONG
                              DIM udtSTE                      AS SERVICE_TABLE_ENTRY
                              DIM lpThreadAttributes          AS SECURITY_ATTRIBUTES
                              ' Start the main thread for this service
                              glnghServiceThread = CreateThread(lpThreadAttributes, 0, CODEPTR(ServiceThread), 0, 0, ID)
                              ' Did the thread start OK
                          
                          'JLB
                              CALL ServiceThread(glnghServiceThread)
                          
                              IF glnghServiceThread = 0 THEN
                          
                                  FUNCTION = %FALSE
                          
                                  EXIT FUNCTION
                          
                              ELSE
                          
                                  ' Set the global to running
                                  glngRunningService = %TRUE
                                  FUNCTION = %TRUE
                                  EXIT FUNCTION
                          
                              END IF
                          
                          END FUNCTION
                          
                          SUB PauseService()
                          
                              ' Set the global indicating that we are not paused
                              glngPauseService = %TRUE
                              ' Let er rip
                              SuspendThread glnghServiceThread
                          END SUB
                          
                          SUB ResumeService()
                              ' Set the global indicating that we are not paused
                              glngPauseService = %FALSE
                              ' Let er rip
                              ResumeThread glnghServiceThread
                          END SUB
                          
                          SUB StopService()
                          
                              ' Set the global flag indicating that the service is not running
                              glngRunningService = %FALSE
                              ' Set the event so the service will stop
                              SetEvent glnghTerminateEvent
                          
                          END SUB
                          
                          FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, _
                                      ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
                          
                              DIM udtSS               AS SERVICE_STATUS
                              '  Reset the global service status value.
                              gdwdServiceStatus = CurrentStatus
                              '  Setup the UDT.
                              udtSS.dwServiceType = %SERVICE_WIN32_OWN_PROCESS
                              udtSS.dwCurrentState = CurrentStatus
                              ' If we are the process of starting, then don't accept control events
                          
                              IF CurrentStatus = %SERVICE_START_PENDING THEN
                          
                                  udtSS.dwControlsAccepted = 0
                          
                              ELSE
                          
                                  ' Take what was given
                                  udtSS.dwControlsAccepted = %SERVICE_ACCEPT_STOP + %SERVICE_ACCEPT_PAUSE_CONTINUE + %SERVICE_ACCEPT_SHUTDOWN
                          
                              END IF
                          
                              ' If a specific ServiceSpecificExitCode is defined, setup the Win32 exit code properly
                          
                              IF ServiceSpecificExitCode = 0 THEN
                          
                                  udtSS.dwWin32ExitCode = ExitCode
                          
                              ELSE
                          
                                  udtSS.dwWin32ExitCode = %ERROR_SERVICE_SPECIFIC_ERROR
                          
                              END IF
                          
                              ' Specific Exit Code
                              udtSS.dwServiceSpecificExitCode = ServiceSpecificExitCode
                              udtSS.dwCheckPoint = Checkpoint
                              udtSS.dwWaitHint = WaitHint
                          
                              IF SetServiceStatus(gdwdHStatus, udtSS) = 0 THEN
                          
                                  ' Something went wrong so stop the service
                                  StopService
                                  FUNCTION = %FALSE
                          
                              ELSE
                          
                                  FUNCTION = %TRUE
                          
                              END IF
                          
                          END FUNCTION
                          
                          SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
                          
                              LOCAL Result                AS LONG
                              LOCAL lpEventAttributes     AS SECURITY_ATTRIBUTES
                              ' Register with the SCM
                              gdwdHStatus = RegisterServiceCtrlHandler(gascServiceName, CODEPTR(Handler))
                              ' Did it work
                          
                              IF gdwdHStatus = 0 THEN
                          
                                  ' No, so terminate
                                  Terminate GetLastError()
                          
                                  EXIT SUB
                          
                              END IF
                          
                              '  Service has been registered and startup is pending
                          
                              IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 1, 5000) THEN
                          
                                  Terminate GetLastError()
                          
                                  EXIT SUB
                          
                              END IF
                          
                              ' Create the termination event
                              glnghTerminateEvent = CreateEvent(lpEventAttributes, %TRUE, %FALSE, "")
                          
                              IF glnghTerminateEvent = 0 THEN
                          
                                  Terminate GetLastError()
                                  EXIT SUB
                          
                              END IF
                          
                              '  Service startup is still pending
                              IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 2, 1000) THEN
                          
                                  Terminate GetLastError()
                                  EXIT SUB
                          
                              END IF
                          
                              ' Start the service
                              Result = InitService()
                          
                              IF ISFALSE Result THEN
                          
                                  ' Oops
                                  Terminate GetLastError()
                                  EXIT SUB
                          
                              END IF
                              ' Service is now running
                          
                              IF ISFALSE SendStatus(%SERVICE_RUNNING, %NO_ERROR, 0, 0, 0) THEN
                          
                                  Terminate GetLastError()
                          
                                  EXIT SUB
                          
                              END IF
                          
                              ' Wait for the signal to end
                              WaitForSingleObject glnghTerminateEvent, %INFINITE
                              Terminate 0
                          
                              EXIT SUB
                          
                          END SUB
                          
                          SUB Terminate(ErrCode AS DWORD)
                          
                              ' If the Terminate Event has already been created then destroy it
                          
                              IF glnghTerminateEvent <> 0 THEN
                          
                                  CloseHandle glnghTerminateEvent
                          
                              END IF
                          
                              IF gdwdHStatus <> 0 THEN
                          
                                  ' Send a message to the SCM and tell them that we are stopping
                          
                                  SendStatus %SERVICE_STOPPED, ErrCode, 0&, 0&, 0&
                          
                              END IF
                          
                              ' If the thread has started, then kill it
                          
                              IF glnghServiceThread <> 0 THEN
                          
                                  ' Not normally here in a service
                                  ' However, this program was written as an executable first and converted into a service
                                  ' There is a global hWindow that is set in the WINMAINX when we start
                                  ' We need to destroy the window so the WINMAINX will come back to us
                                  ' Is it a valid window
                          
                                  IF IsWindow(hWindow) THEN
                          
                                      ' Yes so destroy it
                                      DestroyWindow hWindow
                                      ' This will cause the Message Loop in your WINMAINX to exit and return
                          
                                  END IF
                          
                                  ' Close the thread
                          
                                  CloseHandle glnghServiceThread
                          
                              END IF
                          
                          END SUB
                          
                          SUB Handler(BYVAL ControlValue AS DWORD)
                          
                              '  This procedure (by its' name) handles all service requests.
                              LOCAL Result                AS LONG
                              ON ERROR RESUME NEXT
                              ' There is no Start option because the ServiceMain takes care of starting the service
                          
                              SELECT CASE ControlValue
                          
                                  CASE %SERVICE_CONTROL_STOP
                                      ' Set the global Status
                                      gdwdServiceState = %SERVICE_STOP_PENDING
                                      ' Tell the SCM that we are stopping
                                      SendStatus %SERVICE_STOP_PENDING, %NO_ERROR, 0&, 1, 5000
                                      ' Stop the service
                                      StopService
                          
                                  CASE %SERVICE_CONTROL_PAUSE
                                      ' Are we running and not paused
                          
                                      IF (ISTRUE glngRunningService) AND (ISFALSE glngPauseService) THEN
                          
                                          ' Tell the SCM that we are pausing
                                          SendStatus %SERVICE_PAUSE_PENDING, %NO_ERROR, 0, 1, 1000
                                          ' Pause it
                                          PauseService
                                          ' Set the current state
                                          gdwdServiceState = %SERVICE_PAUSED
                          
                                      END IF
                          
                                  CASE %SERVICE_CONTROL_CONTINUE
                                      ' Are we running and paused
                          
                                      IF (ISTRUE glngRunningService) AND (ISTRUE glngPauseService) THEN
                          
                                          ' Tell the SCM that we are un pausing
                                          SendStatus %SERVICE_CONTINUE_PENDING, %NO_ERROR, 0, 1, 1000
                                          ' Resume the service
                                          ResumeService
                                          ' Set the current state
                                          gdwdServiceState = %SERVICE_RUNNING
                          
                                      END IF
                          
                                  CASE %SERVICE_CONTROL_INTERROGATE
                                      ' Don't need to do anything
                                      ' We will send the current status below
                          
                          
                                  CASE %SERVICE_CONTROL_SHUTDOWN
                                      ' We don't do anything with a shutdown
                          
                                      EXIT SUB
                          
                              END SELECT
                          
                              ' Tell the SCM the new status
                          
                              SendStatus gdwdServiceState, %NO_ERROR, 0, 0, 0
                          
                          END SUB
                          
                          FUNCTION ServiceThread(ID AS LONG) EXPORT AS LONG
                          
                              LOCAL Result                    AS LONG
                              'LOCAL Msg                       AS tagMsg
                          
                              'MSGBOX "In the Service Thread Function'
                          
                              ' Run until we are killed
                          
                              ' Result = WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
                              CALL WINMAINX(BYVAL glngHInstance, BYVAL glnghPrvInst, BYVAL ptrCmdLine, BYVAL glngCmdShow)
                          
                              ' I had written a program and compiled it as an exe
                              ' To make it a service, I just renamed the WINMAIN to WINMAINX and then
                              ' call it like you see above
                          
                          END FUNCTION
                          
                          'SQL Processing Routine
                          
                          %FALSE = 0
                          
                          DECLARE SUB Sql_Update_Display1
                          DECLARE SUB OpenSQLFile1()
                                                                                        ' SQL
                          TYPE SQLTYPE1       ' LEN 900
                          
                             SQLTYPE          AS STRING * 1   ' = "1"                   ' SqlTypeST
                             DATEST           AS STRING * 10                            ' Date
                             USERCODEST       AS STRING * 4                             ' UserCode
                             USERNAMEST       AS STRING * 20                            ' UserName
                             TIME             AS STRING * 10                            ' TimeST
                             SESSION          AS LONG                                   ' SessionLO
                             TRANSACTION      AS LONG                                   ' TransactionLO
                             SESSIONREF       AS STRING * 13                            ' SessionRefST
                             TRANSACTIONREF   AS STRING * 13                            ' TransactionRefST
                             BAGORORDERNO     AS STRING * 8                             ' BagOrderNumber
                             BAGORORDERREF    AS STRING * 13                            ' BagOrderRef
                             BANKGROUPST      AS STRING * 3   '                         ' BankGroup
                             DEPARTMENTLO     AS LONG         '                         ' Department
                             TRANSITST        AS STRING * 13  ' ROUTING AND TRANSIT NO  ' Transit
                             TYPEST           AS STRING * 1   '                         ' Type
                             CODEST           AS STRING * 1   '                         ' Code
                             CLASSST          AS STRING * 5   '                         ' Class
                             REFERENCEST      AS STRING * 13  ' CUST NUMBER /ACCOUNT    ' Reference
                             CHARGE_TOST      AS STRING * 13  '                         ' Charge_To
                             BRANCH_NUMBER    AS DOUBLE       ' LOCATION 10 DIGIT       ' Branch_Number
                             NAMEST           AS STRING * 25  ' NAME                    ' Name
                             ADD1ST           AS STRING * 25  ' ADDRESS LINE 1          ' Address1
                             ADD2ST           AS STRING * 25  ' ADDRESS LINE 2          ' Address2
                             ADD3ST           AS STRING * 25  ' ADDRESS LINE 3          ' Address3
                             ADD4ST           AS STRING * 25  ' ADDRESS LINE 4          ' Address4
                             PHONEST          AS STRING * 15  ' CUSTOMER PHONE          ' Phone
                             FAXST            AS STRING * 15  ' CALLBACK / FAX PHONE    ' Fax
                             TAXIDST          AS STRING * 13       ' TAX ID NUMBER      ' TaxId
                             SHIPTOST         AS STRING * 13  ' DEFAULT SHIP TO ROUTE   ' ShopTo
                             GLCOSTCENTERLO   AS LONG   '                               ' GlCostCenter
                             GLDEPOSITACCTLO  AS LONG   '                               ' GlDepositAcct
                             GLORDERACCTLO    AS LONG   '                               ' GlOrderAcct
                             GLADJUSTACCTLO   AS LONG   '                               ' GlAdjustAcct
                             RETAILBANKLO     AS LONG   '                               ' RetailBank
                             TRANSACTIONTYPE  AS STRING * 10                            ' TransactionType
                             ENV_DEP_DECLARED AS DOUBLE                                 ' Env_Dep_Declared
                             DECLARED         AS DOUBLE                                 ' Declared
                             TOTAL_ACTUAL     AS DOUBLE                                 ' Total_Actual
                             DIFFERENCE       AS DOUBLE                                 ' Difference
                             CURRENCYTOT      AS DOUBLE                                 ' CurrencyTot
                             COINTOT          AS DOUBLE                                 ' CoinTot
                             CASHTOT          AS DOUBLE                                 ' CashTot
                             ONES             AS DOUBLE                                 ' Ones
                             TWOS             AS DOUBLE                                 ' Twos
                             FIVES            AS DOUBLE                                 ' Fives
                             TENS             AS DOUBLE                                 ' Tens
                             TWENTIES         AS DOUBLE                                 ' Twenties
                             FIFTIES          AS DOUBLE                                 ' Fifties
                             HUNDREDS         AS DOUBLE                                 ' Hundreds
                             CURR_OTHER       AS DOUBLE                                 ' Currency_Other
                             PENNIES          AS DOUBLE                                 ' Pennies
                             NICKELS          AS DOUBLE                                 ' Nickles
                             DIMES            AS DOUBLE                                 ' Dimes
                             QUARTERS         AS DOUBLE                                 ' Quarters
                             HALVES           AS DOUBLE                                 ' Halves
                             DOLLARS          AS DOUBLE                                 ' Dollars
                             COIN_OTHER       AS DOUBLE                                 ' Coin_Other
                             NOTE_PIECES      AS LONG                                   ' Note_Pieces
                             STRAP_PIECES     AS LONG                                   ' Strap_Pieces
                             BRICK_PIECES     AS LONG                                   ' Brick_Pieces
                             STAMP_VALUE      AS DOUBLE                                 ' Stamp_Value
                             STAMP_PIECES     AS LONG                                   ' Stamp_Pieces
                             COIN_PIECES      AS LONG                                   ' Coin_Pieces
                             ROLLS_PIECES     AS LONG                                   ' Rolls_Pieces
                             BOXES_PIECES     AS LONG                                   ' Boxes_Pieces
                             BAGS_PIECES      AS LONG                                   ' Bags_Pieces
                             GOLD_VALUE       AS DOUBLE                                 ' Gold_Value
                             GOLD_PIECES      AS LONG                                   ' Gold_Pieces
                             MIX_VALUE        AS DOUBLE                                 ' Mix_Value
                             MIX_PIECES       AS LONG                                   ' Mix_Pieces
                             MISC_DOCUMENT    AS DOUBLE                                 ' Misc_Document
                             MISC_DOCUMENTL   AS LONG                                   ' Misc_DocumentL
                             MERCHANDISE      AS DOUBLE                                 ' Merchandise
                             MERCHANDISEL     AS LONG                                   ' MerchandiseL
                             CHECKS           AS DOUBLE                                 ' Checks
                             CHECKSL          AS LONG                                   ' ChecksL
                             CASHIER_CHECKS   AS DOUBLE                                 ' Cashier_Checks
                             CASHIER_CHECKSL  AS LONG                                   ' Cashier_ChecksL
                             CREDIT_CARD      AS DOUBLE                                 ' Credit_Card
                             CREDIT_CARDL     AS LONG                                   ' Credit_CardL
                             CREDIT_NOCHARGE  AS DOUBLE                                 ' Credit_NoCharge
                             CREDIT_NOCHARGEL AS LONG                                   ' Credit_NoChargeL
                             ADJUST           AS DOUBLE                                 ' Adjust
                             ADJUSTL          AS LONG                                   ' AdjustL
                             SLIP             AS DOUBLE                                 ' Slip
                             SLIPL            AS LONG                                   ' SlipL
                             ENTRIES          AS LONG                                   ' Entries
                             SECONDS          AS LONG                                   ' Seconds
                             REFERENCETEXT    AS STRING * 30:                           ' ReferenceTextST
                             WRITEOFFDO       AS DOUBLE  ' = TWRITEOFFDO:  'AS DOUBLE   ' WriteOff
                             WRITEOFF         AS LONG    ' = WRITEOFF:    'AS LONG      ' WriteOffD
                             FOREIGNDO        AS DOUBLE  '                              ' ForeignD
                             FOREIGNLO        AS LONG                                   ' ForeignL
                             'Problem with The Buffer*
                             '===================================================================
                             BUFFER5ST        AS STRING * 25                            ' Buffer5
                             BUFFER6ST        AS STRING * 13                            ' Buffer6
                             BUFFER7ST        AS STRING * 1                             ' Buffer7
                             BUFFER8ST        AS STRING * 1                             ' Buffer8
                             BUFFER9ST        AS STRING * 1                             ' Buffer9
                             BUFFERAST        AS STRING * 1                             ' BufferA
                             BUFFERBST        AS STRING * 1                             ' BufferB
                             BUFFERCST        AS STRING * 1                             ' BufferC
                             BUFFERDST        AS STRING * 1                             ' BufferD
                             BUFFEREST        AS STRING * 1                             ' BufferE
                             BUFFERFST        AS STRING * 1                             ' BufferF
                             BUFFERGST        AS STRING * 1                             ' BufferG
                             BUFFERHST        AS STRING * 1                             ' BufferH
                             BUFFERIST        AS STRING * 1                             ' BufferI
                             BUFFERJST        AS STRING * 77                            ' BufferJ
                             '===================================================================
                             EXEMPTST         AS STRING * 1                             ' Exemp
                             MODELST          AS STRING * 1                             ' Model
                             UPDATED          AS STRING * 1   ' Y=UPDATED               ' Updated
                          
                          END TYPE
                          
                          'Variables
                          
                          GLOBAL DB1OPEN AS LONG
                          GLOBAL  DisplayRecord AS STRING
                          GLOBAL CIMSQLFILE$
                          GLOBAL SQ1 AS  SQLTYPE1
                          
                          
                          FUNCTION WINMAINX(BYVAL glngHInstance AS LONG , _
                                              BYVAL glnghPrvInst AS LONG ,_
                                                  BYVAL ptrCmdLine AS ASCIIZ PTR * 40 ,_
                                                      BYVAL glngCmdShow AS LONG)EXPORT AS LONG
                          
                               ' MSGBOX "In the WInMainX"
                          
                               LOCAL hWindow AS LONG
                          
                              ON ERROR RESUME NEXT:
                              ERRCLEAR:
                              GLOBAL sqlCount&
                              SQL_Init
                              ' SQL_OpenDB "GARDENIADATA.DSN"                '     This gives the Login ODBC Box
                          
                              Sql_OpenDB "GardeniaDb2.DSN"
                              'SQL_OpenDB("HMGardenia.DSN")                '     Home Lan ConnectionSQL Server
                              'SQL_OpenDB("HomeGardenia.DSN")
                          
                              CALL OpenSQLFile1                   '
                              CALL Sql_Update_Display1()
                          
                          END FUNCTION
                          
                          SUB OpenSQLFile1()  ' SQL_FORMAT_1.CIM
                          
                              CIMSQLFILE$ = "t:\SQL_FORMAT_1.CIM"
                              ON ERROR RESUME NEXT:
                          
                              CLOSE #(291):
                              ERRCLEAR:
                              OPEN CIMSQLFILE$ FOR RANDOM LOCK SHARED AS #291 LEN=900:
                              DB1OPEN = 1
                              LOCK#(291): UNLOCK#(291):
                          
                          '  Error Checking Here...
                          
                          END SUB
                          
                          SUB Sql_Update_Display1()
                          
                              ON ERROR RESUME NEXT: ERRCLEAR:
                              LOCAL X&,Y&,COMPLETE&,lenf&:
                              LOCAL A$,B$,C$:
                              LOCAL SQLLASTRECORDREAD AS LONG
                          
                              DIM sSQLStatement   AS GLOBAL STRING
                              DIM RLO             AS LONG
                              DIM SQLSTARTRECORD  AS LONG
                          
                              IF SQLLASTRECORDREAD = 0 THEN
                          
                                  SQLSTARTRECORD =  1 ' LOC(291)/900:
                          
                              ELSE
                          
                                  IF SQLSTARTRECORD <> SQLLASTRECORDREAD THEN SQLSTARTRECORD = SQLLASTRECORDREAD:
                          
                              END IF
                          
                              DO:
                          
                                  COMPLETE& = 1:
                          
                                  OpenSQLFile1()
                          
                                  IF LOF(291)>800 THEN
                          
                                      RLO=0:
                          
                                      DO:
                          
                                          SLEEP 1000:
                          
                                          INCR RLO: ' NEW
                          
                                          GET #291 , RLO, SQ1:
                          
                                          INCR SQLStartRecord
                          
                                          IF SQ1.UPDATED <> "Y" THEN
                          
                                              IF DB1OPEN = 0 THEN CALL OpenSQLFile1:
                          
                                              sSQLStatement = "Insert INTO Transactions (SqlType, Date, UserCode, " + _
                                                              "UserName, TimeST ,SessionLO, TransactionLO, " + _
                                                              "SessionRefST, " + _
                                                              "TransactionRefST, " +  _
                                                              "BagOrderNumber, " + _
                                                              "BagOrderRefST, "  + _
                                                              "BankGroup , " + _
                                                              "Department," + _
                                                              "Transit," + _
                                                              "Type, " + _
                                                              "Code, " + _
                                                              "Class, " + _
                                                              "Reference, " + _
                                                              "Charge_To, " + _
                                                              "Branch_Number, " + _
                                                              "Name, " + _
                                                              "Address1, " + _
                                                              "Address2, " + _
                                                              "Address3, " + _
                                                              "Address4, " + _
                                                              "Phone, " + _
                                                              "Fax, " + _
                                                              "TaxId, " + _
                                                              "ShipTo, " + _
                                                              "GlCostCenter, " + _
                                                              "GlDepositAcct, " + _
                                                              "GlOrderAcct, " + _
                                                              "GlAdjustAcct, " + _
                                                              "RetailBank, "  + _
                                                              "TransactionType, " + _
                                                              "Env_Dep_Declared,Declared, " + _
                                                              "Total_Actual, " + _
                                                              "Difference, " + _
                                                              "CurrencyTot, " + _
                                                              "CoinTot, "  + _
                                                              "CashTot, " + _
                                                              "Ones, " + _
                                                              "Twos, " + _
                                                              "Fives, " + _
                                                              "Tens, "  + _
                                                              "Twenties, " + _
                                                              "Fifties, "  + _
                                                              "Hundreds, " + _
                                                              "Currency_Other, "  + _
                                                              "Pennies, " + _
                                                              "Nickles, " + _
                                                              "Dimes, " + _
                                                              "Quarters, " + _
                                                              "Halves, " + _
                                                              "Dollars, "  + _
                                                              "Coin_Other, " + _
                                                              "Note_Pieces, " + _
                                                              "Strap_Pieces, " + _
                                                              "Brick_Pieces, " + _
                                                              "Stamp_Value, "  + _
                                                              "Stamp_Pieces, " + _
                                                              "Coin_Pieces, " + _
                                                              "Rolls_Pieces, " + _
                                                              "Boxes_Pieces, " + _
                                                              "Bags_Pieces, "  + _
                                                              "Gold_Value, " + _
                                                              "Gold_Pieces, "  + _
                                                              "Mix_Value, "  + _
                                                              "Mix_Pieces, " + _
                                                              "Misc_Document, " + _
                                                              "Misc_DocumentL, " + _
                                                              "Merchandise, " + _
                                                              "MerchandiseL, "  + _
                                                              "Checks, " + _
                                                              "ChecksL, "  + _
                                                              "Cashier_Checks, "  + _
                                                              "Cashier_ChecksL, " + _
                                                              "Credit_Card, " + _
                                                              "Credit_CardL, " + _
                                                              "Credit_NoCharge, " + _
                                                              "Credit_NoChargeL, " + _
                                                              "Adjust, " + _
                                                              "AdjustL, " + _
                                                              "Slip, " + _
                                                              "SlipL, " + _
                                                              "Entries, " + _
                                                              "Seconds, "  + _
                                                              "ReferenceTextST," + _
                                                              "WriteOff, " + _
                                                              "WriteOffD, " + _
                                                              "ForeignD, " + _
                                                              "ForeignL, " + _       '"Buffer5, "  "Buffer6, " "Buffer7, " "Buffer8, " "Buffer9, "  "BufferA, "
                                                              "Exemp, " + _
                                                              "Model, " + _
                                                              "Updated)" + _   ' This is the Last Field in Database
                                                              " VALUES ('" + Sq1.SqlType + "','" + _
                                                              Sq1.DateST + "','" + _
                                                              Sq1.UserCodeST + "','" + _
                                                              Sq1.USERNAMEST + "','" + _
                                                              Sq1.TIME + "','" + _
                                                              STR$(Sq1.SESSION) + "','" + _
                                                              STR$(Sq1.TRANSACTION) + "','" + _
                                                              Sq1.SESSIONREF + "','" + _
                                                              Sq1.TRANSACTIONREF + "','" + _
                                                              Sq1.BAGORORDERNO + "','" + _
                                                              Sq1.BAGORORDERREF  + "','" + _
                                                              Sq1.BANKGROUPST + "','" + _
                                                              STR$(Sq1.DEPARTMENTLO) + "','" + _
                                                              Sq1.TRANSITST + "','" + _
                                                              Sq1.TYPEST + "','" + _
                                                              Sq1.CODEST + "','" + _
                                                              Sq1.CLASSST + "','" + _
                                                              Sq1.REFERENCEST + "','" + _
                                                              Sq1.CHARGE_TOST + "','" + _
                                                              STR$(Sq1.BRANCH_NUMBER) + "','" + _
                                                              Sq1.NAMEST + "','" + _
                                                              Sq1.ADD1ST + "','" + _
                                                              Sq1.ADD2ST + "','" + _
                                                              Sq1.ADD3ST + "','" + _
                                                              Sq1.ADD4ST + "','" + _
                                                              Sq1.PHONEST + "','" + _
                                                              Sq1.FAXST + "','" + _
                                                              Sq1.TAXIDST + "','" + _
                                                              Sq1.SHIPTOST + "','" + _
                                                              STR$(Sq1.GLCOSTCENTERLO) + "','" + _
                                                              STR$(Sq1.GLDEPOSITACCTLO) + "','" + _
                                                              STR$(Sq1.GLORDERACCTLO) + "','" + _
                                                              STR$(Sq1.GLADJUSTACCTLO) + "','" + _
                                                              STR$(Sq1.RETAILBANKLO)+ "','" + _
                                                              Sq1.TRANSACTIONTYPE + "','" + _
                                                              STR$(Sq1.Env_Dep_Declared) + "','" + _
                                                              STR$(Sq1.DECLARED) + "','" + _
                                                              STR$(Sq1.TOTAL_ACTUAL) + "','" + _
                                                              STR$(Sq1.DIFFERENCE) + "','" + _
                                                              STR$(Sq1.CURRENCYTOT) + "','" + _
                                                              STR$(Sq1.COINTOT)  + "','" + _
                                                              STR$(Sq1.CASHTOT) + "','" + _
                                                              STR$(Sq1.ONES) + "','" + _
                                                              STR$(Sq1.TWOS) + "','" + _
                                                              STR$(Sq1.FIVES) + "','" + _
                                                              STR$(Sq1.TENS) + "','" + _
                                                              STR$(Sq1.TWENTIES) + "','" + _
                                                              STR$(Sq1.FIFTIES)  + "','" + _
                                                              STR$(Sq1.HUNDREDS) + "','" + _
                                                              STR$(Sq1.CURR_OTHER) + "','" + _
                                                              STR$(Sq1.PENNIES)  + "','" + _
                                                              STR$(Sq1.NICKELS) + "','" + _
                                                              STR$(Sq1.DIMES) + "','" + _
                                                              STR$(Sq1.QUARTERS) + "','" + _
                                                              STR$(Sq1.HALVES) + "','" + _
                                                              STR$(Sq1.DOLLARS) + "','" + _
                                                              STR$(Sq1.COIN_OTHER) + "','" + _
                                                              STR$(Sq1.NOTE_PIECES) + "','" + _
                                                              STR$(Sq1.STRAP_PIECES) + "','" + _
                                                              STR$(Sq1.BRICK_PIECES) + "','" + _
                                                              STR$(Sq1.STAMP_VALUE)  + "','" + _
                                                              STR$(Sq1.STAMP_PIECES) + "','" + _
                                                              STR$(Sq1.COIN_PIECES)  + "','" + _
                                                              STR$(Sq1.ROLLS_PIECES) + "','" + _
                                                              STR$(Sq1.BOXES_PIECES) + "','" + _
                                                              STR$(Sq1.BAGS_PIECES) + "','" + _
                                                              STR$(Sq1.GOLD_VALUE)  + "','" + _
                                                              STR$(Sq1.GOLD_PIECES) + "','" + _
                                                              STR$(Sq1.MIX_VALUE) + "','" + _
                                                              STR$(Sq1.MIX_PIECES) + "','" + _
                                                              STR$(Sq1.MISC_DOCUMENT) + "','" + _
                                                              STR$(Sq1.MISC_DOCUMENTL) + "','" + _
                                                              STR$(Sq1.MERCHANDISE)  + "','" + _
                                                              STR$(Sq1.MERCHANDISEL) + "','" + _
                                                              STR$(Sq1.CHECKS) + "','" + _
                                                              STR$(Sq1.CHECKSL) + "','" + _
                                                              STR$(Sq1.CASHIER_CHECKS)  + "','" + _
                                                              STR$(Sq1.CASHIER_CHECKSL)  + "','" + _
                                                              STR$(Sq1.CREDIT_CARD)  + "','" + _
                                                              STR$(Sq1.CREDIT_CARDL)  + "','" + _
                                                              STR$(Sq1.CREDIT_NOCHARGE) + "','" + _
                                                              STR$(Sq1.CREDIT_NOCHARGEL) + "','" + _
                                                              STR$(Sq1.ADJUST) + "','" + _
                                                              STR$(Sq1.ADJUSTL) + "','" + _
                                                              STR$(Sq1.SLIP) + "','" + _
                                                              STR$(Sq1.SLIPL) + "','" + _
                                                              STR$(Sq1.ENTRIES) + "','" + _
                                                              STR$(Sq1.SECONDS) + "','" + _
                                                              Sq1.REFERENCETEXT + "','" + _
                                                              STR$(Sq1.WRITEOFFDO) + "','" + _
                                                              STR$(Sq1.WRITEOFF) + "','" + _
                                                              STR$(Sq1.FOREIGNDO) + "','" + _
                                                              STR$(Sq1.FOREIGNLO) + "','" + _             ' Sq1.BUFFER5ST + "','" + _
                                                              Sq1.EXEMPTST + "','" + _
                                                              Sq1.MODELST + "','" + _
                                                              Sq1.UPDATED  + "')"   ' END OF FIELDS(Colums)
                          
                                              ERRCLEAR:
                                              SQL_Stmt %SQL_STMT_IMMEDIATE, sSQLStatement
                                              'Need error Checking here....
                                              IF ERR = 0 THEN
                          
                                                  LSET SQ1.UPDATED ="Y":
                                                  PUT #291,RLO,SQ1:
                          
                                              ELSE
                          
                                                  COMPLETE&=0:
                          
                                              END IF
                          
                                          END IF
                          
                                      LOOP UNTIL EOF(291)
                          
                                  END IF
                          
                                  SLEEP 1000
                          
                              LOOP:
                              SQLlastRecordRead = LOF(291)/900:
                          
                          END SUB
                          ------------------
                          mailto:[email protected][email protected]</A>
                          mailto:[email protected][email protected]</A>

                          Comment


                          • #14
                            I assume you know you cannot run a service from a removable drive
                            (like network drive, ect...) That because I saw you copiled to t:\

                            ------------------
                            Regards,
                            Peter

                            [This message has been edited by Peter Lameijn (edited November 19, 2003).]
                            Regards,
                            Peter

                            Comment


                            • #15
                              I am compiling the Exec file to the network drive of the server
                              that I am trying to get the service to start. The Source code is
                              on my Desktop Computer.


                              Could not start the CIM-ODBC service on Local Computer.


                              Error 1053: The service did not respond to the start or control
                              request in a timely fashion.


                              ------------------
                              mailto:[email protected][email protected]</A>
                              mailto:[email protected][email protected]</A>

                              Comment


                              • #16
                                A few things:

                                - You should not use the Create/SuspendThread functions but Thread Create, ect... (according to PB staff)
                                - To pause a Service it's not advisable to use suspendthread. If you do, the service will be stopped in mid-air.
                                It can't close open databases or save data. Better use an Event and let thread pause itself.
                                - You use CreateThread and then at next line call the thread as a normal function. You shouldn't do so...
                                - You shouldn't use msgbox in services.
                                - Make sure that last call with SERVICE_START_PENDING the time-out 1000 is enough to initialize...

                                The service starts normal on my system (although I had to strip the SQL stuff; don't have it...)



                                ------------------
                                Regards,
                                Peter
                                Regards,
                                Peter

                                Comment


                                • #17
                                  Joseph,

                                  as Peter said
                                  ---------------------
                                  You use CreateThread and then at next line call the thread as a normal function. You shouldn't do so...
                                  ---------------------

                                  this will give you truely that time out

                                  because CreateThread() launch the same code as your call do either.
                                  You run your code twice and the call will not return to tell SCM "Service is running now"
                                  So SCM will time out.
                                  CIMsqlupdate code is not thread safe - it will crash anytime under such conditions.

                                  ----------
                                  Compile the template alone (the messagbox included).
                                  Install the service
                                  open SCM Dialog
                                  open properties of your service and enable
                                  dataflow between service and desktop
                                  (sorry I don't know in English,
                                  or include flag SERVICE_INTERACTIVE_PROCESS in installer)
                                  start service

                                  you should see msgbox
                                  "In the Service Thread Function"

                                  ok, add your own code.
                                  add msgboxes/beeps for testing.

                                  I believe it will run - or it would like do the job

                                  but maybe you need more rights for attaching your sql-server.
                                  Service is running as user "Local System", that is not the same
                                  as user "Joseph" and his "standalone.exe" !
                                  If you change service rights to user "Joseph" you will lose
                                  dataflow between service and desktop, so you can't include
                                  messageboxes anymore.

                                  Ahoj
                                  -Uwe


                                  ------------------

                                  Comment

                                  Working...
                                  X