VBScript functions, activeX, and access violations

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
Greg
Newbie
Posts: 3
Joined: Tue Jul 10, 2012 2:29 am
Location: Louisville, kY

VBScript functions, activeX, and access violations

Post by Greg » Tue Jul 10, 2012 2:42 am

Macro Scheduler is great!

I do have an issue. For some reason, this code snippet:
------
VBSTART
Function OcrText(x1,y1,x2,y2)
OcrRead.CaptureWindow(x1,y1,x2,y2)
ocrText=OcrRead.Text
End Function
VBEND

VBEval>OcrText(%Left%,%Top%,%Right%,%Bottom%),sResult
MessageModal>sResult
-------
works at first, but eventually leads to the message box showing unprintable characters, and then access violations.

However, this code does not:
--------
VBSTART
Dim gsText
Function OcrText(x1,y1,x2,y2)
OcrRead.CaptureWindow(x1,y1,x2,y2)
gsText=OcrRead.Text
End Function
VBEND

VBEval>OcrText(%Left%,%Top%,%Right%,%Bottom%),sResult
VBEval>gsText,sResult
MessageModal>sResult
---------
Although clunkier looking, it works fine time after time, though I worry that something I am doing wrong will eventually lead to memory corruption and a script crash.

(In both cases the Macro Scheduler variables are loaded with the screen coordinates of a text painted onto a control. The OcrRead control was assigned with a CreateObject command and initialized in the same way, and terminated/cleaned up in the same way when the script is done.)

The OcrRead class is a third party control class, and may have a bug. However, I wonder if there is something I am intrinsically doing wrong in setting up my VBScript function-- for instance, should I "allocate" memory by preloading the OcrText function in the first example code with enough characters to hold the resulting screen?

Greg
Newbie
Posts: 3
Joined: Tue Jul 10, 2012 2:29 am
Location: Louisville, kY

Post by Greg » Thu Jul 19, 2012 1:22 am

Actually, it turned out that both the above sets of code still caused crashes.

The problem turned out to be that the activeX control returned a BSTR (bstring)-- and I needed to convert it to a CSTR.

So, the code below works without any access violations or crashing.

Code: Select all

VBSTART 
Function OcrText(x1,y1,x2,y2) 
OcrRead.CaptureWindow(x1,y1,x2,y2) 
ocrText=CStr(OcrRead.Text) 
End Function 
VBEND 

VBEval>OcrText(%Left%,%Top%,%Right%,%Bottom%),sResult 
MessageModal>sResult 


Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts