Absent Member.
Absent Member.

[archive] Active-X events

[Migrated content. Thread originally posted on 07 August 2006]

I purchased an active-x control from Desaware called Gallimufry. I have used in succesfully in retrieving scanned images. However I have not been able to retrieve the name of the scanned file. It is supposed to be part of the data items supplied when the "ImageFileScanned" event is raised. I do not think I am doing the event control coding correctly. Everything else about this control is working great. Below is the relative code. Can someone help me?

* code snippet for Acu Forum
* following items to recieve values from scan event
01 event-items.
03 image-name pic x(40).
03 number-of-images pic 9(3).
03 image-true pic 9.

* active X control placed on screen

01 report-screen.
03 twain-1 TwainControl
size .1 line 22 col 80

03 push-button title "&Acquire "
self-act exception-value = 37
SIZE 20 font small_font
line 5 col 5.

display floating window lines 30 size 80
title "BRICS 2000 Document Imaging"
color black + bckgrnd-white
with system menu
font is large-font
control font is large-font
handle in MAIN_window
cell size push-button font large-font.


evaluate true
when event_notified
evaluate event-type
when w-event
evaluate event-data-2
************************************************************ * from active-x documentation manual: *
Raised when an image has been transferred successfully to disk when the DownloadToFiles property is True.
Syntax Event ImageFileScanned(Filename As String, ByVal PendingImages As Integer, cancelpending As Boolean)
Remarks The Filename parameter contains the name of the file that was saved. The PendingImages parameter contains the number of additional images that are ready to retrieve (if allowed by the current value of the MaxImagesToScan property). Further retrieval of images can be cancelled by setting the cancelpending parameter to True.

* *
* the code below is not entered, *
* What am I doing wrong in the event logic *
when @TwainControlImageFileScanned
call "c$geteventdata"
set exit_report_button_pushed to true


when start-scanning-button-pushed
** when scan button clicked the following code works great *
** image is returned and written to the path given *
** however, the code above for getting the file name is not *
** accessed. *
perform start-the-scan

inquire twain-1 ControlState return-control-state.
modify twain-1 @Manufacturer (brics-name)
giving return-error.

modify twain-1 @ProductName (app-name)
giving return-error.

if return-control-state
modify twain-1 @DownLoadToFiles="true".

if return-control-state
modify twain-1

if return-control-state
modify twain-1 OpenAndSelectScanner ()

inquire twain-1 ControlState return-control-state.
if return-control-state = 0
display message box "No Device Selected"
title "Acquire Stopped"
modify twain-1 ScanImageWithUI ().
**** as this point the file is transferred to disk, I would think control would
**** be passed to the event handling procedure

display message box "Scan Complete"

modify twain-1 CloseScanner ().
9 Replies
Absent Member.
Absent Member.

RE: [archive] Active-X events

It is not clear to me what the problem is;

Is it that the TwainControlImageFileScanned does not occur?

Is it that the TwainControlImageFileScanned event does occur, but the filename is either missing or wrong?

Is it that the TwainControlImageFileScanned event does occur, the filename is right, but the image is not being saved with that name?

When I check out the source of the example program, I see that they (Desaware) does this in a different way. They respond to the event TwainControlImageScanned, upon which the call the control method SaveImageToFile with their own name. In ACUCOBOL-GT this would be like:

WHEN event_notified
  IF event-type = w-event
  AND event-data-2 = TwainControlImageFileScanned
    MODIFY Twain-1 SaveImageToFile("C:\YourNameGoesHere.bmp")

I suggest you try this alternative instead of the other event, if not for anything else just because this is what the vendor does in their example.

In general, I would recommend using events inside the ACCEPT for this, that is, do not terminate the ACCEPT. As you are now outside an ACCEPT, you have no further means to receive additional events from the control.
Absent Member.
Absent Member.

RE: [archive] Active-X events

I have set the control to transfer the image directly to disk. so I do not want to use the "saveimagetofile". Desaware uses the path name I have provided but assigns the base image name, example image0001, image0002. It can detect and assign the next numeric number. This occurs when in the code when "modify twain-1 "scanimagewithui". In debug mode after this line of code, the resident scanner software takes over, the image is scanned and the file is created. When the operator exits the resident scanner software, the close scanner code is executed then control returns to the accept screen statement, waiting for the next operator action. The code I have to respond to the event twaincontrolimagescanned is not accessed, so yes, the file is created using the name desaware assigns but I have not been able to retrieve that name (image0001, image0002, etc).
What to you mean exactly by inside and outside of the "accept"?
Thanks for your help,
Absent Member.
Absent Member.

RE: [archive] Active-X events

What to you mean exactly by inside and outside of the "accept"?

Control events need to be sent somewhere. They need a destination. When your application is processing an ACCEPT statement waiting for user activity, events have a destination, the ACCEPT statement. When you "leave" an ACCEPT statement to process an EVENT PROCEDURE, you are still in the ACCEPT, you are not leaving it unless you tell it to terminate.
When you process an AFTER, EXCEPTION procedure, you are also sort of in the ACCEPT statement, although not quite the same.
If you terminate the ACCEPT statement, like in your example you let the event terminate teh ACCEPT statement and then processes the event, then you are outside of the ACCEPT and there is no longer any processing of events. The consequence is that you will not receive any other events from the control(s).

Now, for seasoned Windows programmers, this might sound a bit odd as Windows really is about events all over. But, for providing an interface like Windows to COBOL programmers, this is in fact truly an elegant solution. Believe me, you would not want to handle all events by yourself and have to implement event procedures for everything you do.

So, what I would suggest is that you change your screen section, so that the scanner control is assigned an EVENT PROCEDURE. Then change your ACCEPT to ON EXCEPTION CONTINUE, handle the events inside the event loop, if your exit button is pushed then terminate the ACCEPT by setting event action accordingly.

In the EVENT PROCEDURE of the control you then respond to the various events it generates.

There are many examples of this around.

As for your desire to have the control deal with the naming, this is of course no problem. As long as you know what you do is supported by the control it is fine. ACUCOBOL-GT can do this, but that you do not get the name then is something you will have to ask the control vendor.
Absent Member.
Absent Member.

RE: [archive] Active-X events

Thanks for your help. The event procedure is now being invoked but I get a ?memory access error?when I issue the c$geteventparam command.

Call ?c$geteventparam?
Using eventcontrolhandle ?Filename? image-name

Do I have wrong syntax or is something else going on? filename is the name of the first of three parameters for this event as given by the .def generated by Axgen.

The other thing I don?t understand is my program works in the debug mode. But when I?m not in the debug mode when I reach the code where I select the scanner, I get a message box (I assume from the active-x control) telling me ?It Worked?, but then my program aborts. Do I need some sort of routine to handle error codes coming from the control? In the debug mode I lose focus on the debug screen, but when I click back to the debug screen, the program continues and works fine until I reach the geteventparam command described above. I do not get the message box when in debug mode or at least I don?t see it.
Absent Member.
Absent Member.

RE: [archive] Active-X events

but I get a ?memory access error?when I issue the c$geteventparam command.

You probably don't distinguish your events, you are getting all kinds of events in there, other events have other parameters. You cannot use the geteventparam call before you know which event you are handling and that you are handling the right one.

Call ?c$geteventparam? Using eventcontrolhandle ?Filename? image-name

Looks good to me.

The other thing I don?t understand is my program works in the debug mode.

Memory errors tend to disappear in debug mode, that is because you in debug mode has much more memory allocated and the memory violation is more likely to hit areas in memory that are not harmful, at least not to begin with...
Absent Member.
Absent Member.

RE: [archive] Active-X events

All is working well with the exception of getting a memory access violation (occurs in or out of debug) when I execute the following:

evaluate event-type
when msg-ax-event
evaluate event-data-2
when @TwainControlImageFileScanned
call "c$geteventdata"
using event-control-handle,
image-name, number-of-images, image-true
***************this is where it blows out with the memory access violation,
Question? Do I have to set the event-control-handle or does the system know that, I assume it does as it has made its way to that line of code thru the msg-ax-event and the event-data-2 value, Could it have to do with the way the variables are declared. I have them as pic x(40), pic 9(8) and pic 9.
The documentation says "filename as strin, PendingImages as integer, cancel pending as boolean. Other than those two things I am out of ideas as to what might be going on unless it is a problem with the active-x control itself.
I have also tried getevenparam which gives me the same error.
Absent Member.
Absent Member.

RE: [archive] Active-X events

This looks sound to me, of course I cannot see all the code, but what you tell sounds good.
Yes, the event-control-handle is the one to be used, this will direct the system to the correct handler.
Variable datatypes should also be okay.
Any chance to get the full program?
Absent Member.
Absent Member.

RE: [archive] Active-X events

thanks again for your help, attached is the complete program listing,
Absent Member.
Absent Member.

RE: [archive] Active-X events

One major mistake you do (which you are not alone of, it is a common thing) is that you actually close the control before it is finished.
Scanning is indeed a very good example why it is preferable to do events and communicate status via statusbar rather than exceptions and messageboxes, this because a scanner operates simultanously with your application going further.

One more thing; For some odd reason the control reports it can only be run in VB, that is just something they say, it is not true. Anyway, when you license it, this will disappear.

This example worked great for me.

       PROGRAM-ID. Scan.
           COPY "DWTWAIN.DEF".

       77  CNTL-FONT                USAGE HANDLE OF FONT SMALL-FONT.
       77  KEY-STATUS               IS SPECIAL-NAMES
           CRT STATUS               PIC 9(4) VALUE 0.
           88  EXIT-PRESSED         VALUE 27.
       77  CtlState                 PIC S9(3).

       01  EVENT-STATUS
           03  EVENT-TYPE           PIC X(4) COMP-X.
           03  EVENT-CONTROL-ID     PIC XX COMP-X.
           03  EVENT-DATA-1         SIGNED-SHORT.
           03  EVENT-DATA-2         SIGNED-LONG.
           03  EVENT-ACTION         PIC X COMP-X.

       78  MSG-AX-EVENT             VALUE 16436.

       01  EVENT-DATA.
           03  IMAGE-NAME           PIC X(260).
           03  NUMBER-OF-IMAGES     PIC 9(8).
           03  IMAGE-TRUE           PIC 9.
       01  SCAN-SCREEN.
           03  Twain1               TwainControl
               SIZE                 1
               LINE                 1
               COL                  1
               VISIBLE              0

           03  FRAME
               TITLE                "Scan an image"
               LINE                 2   
               COL                  2
               LINES                27
               SIZE                 78

           03  bScan                PUSH-BUTTON
               TITLE                "&Scan"
               LINE                 26
               COL                  4
               EVENT PROCEDURE      START-SCAN.

           03  bExit                PUSH-BUTTON
               TITLE                "&Exit"
               LINE                 26
               COL                  65.

           03  MYSbar               STATUS-BAR
               PANEL-WIDTHS         (12, -1)
               PANEL-STYLE          (1, 1)
               PANEL-TEXT           ("Scan status", "Idle").

                   LINES            30
                   SIZE             80
                   FONT             CNTL-FONT
                   CONTROL FONT     CNTL-FONT
                   TITLE "Scan an image".
                   ACCEPT           SCAN-SCREEN
           DESTROY ALL.

           INQUIRE Twain1           ControlState IN CtlState
           IF      CtlState         > 1
                   EXIT             PARAGRAPH

           MODIFY  bScan            Enabled 0
           MODIFY  bExit            Enabled 0
           MODIFY  Twain1
                   Manufacturer     = "Acme Scans Inc."
                   Productname      = "AScan"
                   DownloadToFiles  = 1
                   DirectoryPath    = "C:\"
           INQUIRE Twain1           ControlState IN CtlState

           IF      CtlState         = 0
                   DISPLAY          MESSAGE BOX "Device error"
                   Modify           bScan  Enabled 1
                   Modify           bExit  Enabled 1
                   EXIT             PARAGRAPH

           MODIFY  MYSBar           
                   Panel-Index      2
                   Panel-Text       "Scanning"
           MODIFY  Twain1           ScanImageWithUI()
           EXIT    PARAGRAPH
           IF      Event-Type       NOT = Msg-Ax-Event
                   EXIT             PARAGRAPH

           IF      Event-Data-2     NOT = TwainControlImageFileScanned
                   EXIT             PARAGRAPH

           MODIFY  Twain1           CloseScanner()
           Modify  bScan            Enabled 1
           Modify  bExit            Enabled 1
           EXIT    PARAGRAPH
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.