AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Embedded Windows Scripting (VBScript & JScript) and COM
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
vashk



Joined: 07 Jan 2008
Posts: 11

PostPosted: Wed Feb 06, 2008 7:21 am    Post subject: ws_uninitialize Reply with quote

I'm using WS library in the following code to work with some class objects. The code is accessing the objects without errors. But, I'm running into a problem when I call WS_Uninitialize() at the end. When I call it, AHK crashes after a second or two. If I don't call it, the objects persist over different function calls, even if I explicitly release the pointers. Is there something obvious I'm missing?

Code:

WC_GetMouseText()
{
    CoordMode ToolTip, Screen ;sets ToolTip mode to global coordinates
    CoordMode Mouse, Screen ;sets Mouse mode to global coordinates
    MouseGetPos mX, mY, w_hWnd, c_hWnd, 2
   
    WS_Initialize()
    c_suuid := "{8F267988-0CA4-418C-8F94-B4BC5862B390}" ;WCaptureX
    m_suuid := "{C7E06D1D-4099-43D4-8C22-718E39713773}" ;WMonitorX
    i_suuid := "{4B484CCE-9120-49B7-A5F2-B8B183BFD808}" ;WInput
    r_suuid := "{064E314E-2382-46F2-A93A-239C7115579A}" ;WResult
   
    pWcx := WS_CreateObject(c_suuid)
    pWmx := WS_CreateObject(m_suuid)
    pWi := WS_CreateObject(i_suuid)
    pWr := WS_CreateObject(r_suuid)
    ;debug_ToolTip( ErrorLevel )
   
    WS_AddObject(pWcx,"capture")
    WS_AddObject(pWmx,"monitor")
    WS_AddObject(pWi,"objInput")
    ;WS_AddObject(pWr,"objResult")
   
    WS_ReleaseObject(pWcx)
    WS_ReleaseObject(pWmx)
    WS_ReleaseObject(pWi)
    ;WS_ReleaseObject(pWr)
   
    ;Code = WS_codef("Call monitor.Start(%v,%v,%v)","key","mouse","bGesture")
            ;objResult = capture.Capture(objInput)
    WS_Exec("Call monitor.Start(2,2,True)")
    ;debug_ToolTip( ErrorLevel )

    Declarations =
    (
        Function GetText(hwnd,x,y)
            hwnd = CLng("&H" & hwnd)
            x = CLng("&H" & x)
            y = CLng("&H" & y)
            objInput.Hwnd = hwnd
            objInput.StartX = x
            objInput.StartY = y
            objInput.EndX = x
            objInput.EndY = y
            objInput.ContextWordsLeft = 1
            objInput.ContextWordsRight = 1
            objInput.Options = 1
            Set GetText = capture.Capture(objInput)
        End Function
    )
    WS_Exec(Declarations)
    ;debug_ToolTip(w_hWnd)
    h := SubStr(c_hWnd,3) ;need control hwnd to pass to capture method
    x := SubStr(mX,3)
    y := SubStr(mY,3)
    Code := WS_codef("GetText(%s,%s,%s)",h,x,y)
    WS_Eval(pWr,Code)
    WS_AddObject(pWr,"objResult")
    ;WS_Eval(context,"objResult.Context")
    WS_Eval(text,"objResult.Text")
    ;WS_Eval(locale,"objResult.Locale")
    ;WS_Eval(font,"objResult.Font")
    WS_ReleaseObject(pWr)
    ;debug_ToolTip( ErrorLevel ,"","",1500)
    debug_ToolTip(text)
    WS_Uninitialize() ;must call this to refresh objects
}


_________________
--v
Back to top
View user's profile Send private message
erictheturtle



Joined: 27 Jun 2007
Posts: 58
Location: California

PostPosted: Wed Feb 06, 2008 8:14 am    Post subject: Reply with quote

I see you are creating the objects via their CLSID. If you can create them via the ProgID directly in VB code, could you try that?

Also, I see you are adding pWr back into the scripting environment as soon as you've received it in AHK. Could you change this:
Code:
    Code := codef("GetText(%s,%s,%s)",h,x,y)
    WS_Eval(pWr,Code)
    WS_AddObject(pWr,"objResult")
   ...
    WS_ReleaseObject(pWr)
to this?
Code:
    Code := codef("Set objResult = GetText(%s,%s,%s)",h,x,y)
    WS_Exec(Code)

If changing these things fixes the problem, then ws4ahk probably has something wrong with it. If you've tried all these things already, then something could be really wrong with ws4ahk, or the object you're playing with might have the problem. I'll check more tomorrow.

I'm surprised someone is actually using the codef function Shocked But I see I failed to handle hexadecimal values for %v...I would like to fix that, but I don't think I can...
_________________
-m35
Back to top
View user's profile Send private message
vashk



Joined: 07 Jan 2008
Posts: 11

PostPosted: Wed Feb 06, 2008 5:26 pm    Post subject: Reply with quote

eric,

Thank you for the fast response. You even answered some questions I was thinking about but hadn't asked Razz

I implemented your suggestion to use WS_Exec(Code) setting the value in the VB and not bothering with keeping a pWr pointer. I've just started using your library, so I was trying many different things and missed that in the shuffle. It solves my persistence issue nicely. However I'm still seeing AHK crash if I use WS_Uninitialize()...

I'm thinking if I take your suggestion to instantiate inside VB code instead of WS_AddObject() it might help, but I'll have to take some time to debug it because the DLL complains when I try that. Come to think of it, it might be the DLL which is causing the crash in the first place. I'll respond again if I find a fix.

The hex code and codef use is not at all important. It's just a remnant of when I ran into some trouble early on with the VB end and made a workaround.

Again, thanks for the suggestions and the cool library to play with.

Razz
_________________
--v
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 676

PostPosted: Thu Feb 07, 2008 2:41 am    Post subject: Reply with quote

strange i wonder why it crashes i use this library alot and never experience a problem wiht crashing ahk
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
erictheturtle



Joined: 27 Jun 2007
Posts: 58
Location: California

PostPosted: Thu Feb 07, 2008 4:57 am    Post subject: Reply with quote

vashk

I took another long look at your code, and you've written it basically how I would. So my final troubleshooting suggestion is to re-write the code entirely in VB, VBA, or VBScript, and feeding it some numbers from Window Spy (manually copy-paste the values into the script). If it crashes in these environments, then we can be sure the COM object(s) are the source of the crash.

If you {aren't sure how to write | don't really want to write | don't have time to write | don't really care to write | have betting things to do than write | are enjoying leisurely walks on the beach instead of writing} this test by sometime tomorrow, I'll write it up and post it along with some instructions.
_________________
-m35
Back to top
View user's profile Send private message
vashk



Joined: 07 Jan 2008
Posts: 11

PostPosted: Thu Feb 07, 2008 7:54 pm    Post subject: Reply with quote

I found a quick fix using the current code which requires the script to wait at least 1500 ms before calling WS_Uninitialize(). With the usage I have in mind, it's a perfectly acceptable delay. I don't want to call it multiple times within that period, so I don't think a Timer would work well.

Code:
WC_GetMouseText()
{
    ...
    WS_Exec(Code)
    WS_Eval(text,"objResult.Text")
    ...
    Sleep 1500
    WS_Uninitialize()
}


This prevents the crash. I'm investigating whether this has to do with the DLL I'm using... I suspect it does.

The last issue I'm having occurs when objResult.Text contains non-Latin-encoded characters. WS_Eval(text,"objResult.Text") assigns "????" to the text variable. It's definitely the Latin "?" (%3f) not Unicode. A MsgBox in VB confirms that objResult.Text is retrieving the correctly encoded text, but there is no clean way I can think of to put it into an AHK variable without proper Unicode support.
_________________
--v
Back to top
View user's profile Send private message
erictheturtle



Joined: 27 Jun 2007
Posts: 58
Location: California

PostPosted: Fri Feb 08, 2008 11:28 pm    Post subject: Reply with quote

Sorry I didn't get this posted yesterday.

Your script converted to VBS
Code:

' fill in these values
c_sprogid = "" ' WCaptureX PROGID
m_sprogid = "" ' WMonitorX PROGID
i_sprogid = "" ' WInput PROGID
r_sprogid = "" ' WResult PROGID


' use the other script to get the x, y
' and HWND of the window and control 
mX = 0 'paste value here
mY = 0 'paste value here
w_hWnd = 0 'paste HWND here
c_hWnd = 0 'paste HWND here


Function GetText(hwnd,x,y)
   'hwnd = CLng("&H" & hwnd)
   'x = CLng("&H" & x)
   'y = CLng("&H" & y)
   objInput.Hwnd = hwnd
   objInput.StartX = x
   objInput.StartY = y
   objInput.EndX = x
   objInput.EndY = y
   objInput.ContextWordsLeft = 1
   objInput.ContextWordsRight = 1
   objInput.Options = 1
   Set GetText = capture.Capture(objInput)
End Function


Set capture   = CreateObject(c_sprogid)
Set monitor   = CreateObject(m_sprogid)
Set objInput  = CreateObject(i_sprogid)
'Set objResult = CreateObject(r_sprogid)

Call monitor.Start(2,2,True)

Set objResult = GetText(c_hWnd, mX, mY)

WScript.Echo objResult.Context
WScript.Echo objResult.Text   
WScript.Echo objResult.Locale
WScript.Echo objResult.Font      



Little AHK program to get the HWND and coordinates
Code:
CoordMode ToolTip, Screen ;sets ToolTip mode to global coordinates
CoordMode Mouse, Screen ;sets Mouse mode to global coordinates

Msgbox % "Ctrl+Left Click to capture values."


^LButton::

   MouseGetPos mX, mY, w_hWnd, c_hWnd, 2
   
   SetFormat, integer, d
   VariableContainingAnInteger += 0
   mX += 0
   mY += 0
   w_hWnd += 0
   c_hWnd += 0
   
   s := "mX = " mX "`nmY = " mY "`nw_hWnd = " w_hWnd "`nc_hWnd = " c_hWnd
   
   Tooltip % s "`n`nThis is copied to the clipboard"
   
   Clipboard := s
   
   Return


If you could fill in the blanks in the VB script and test it, I'd be very interested in the results.

In any case, I'm glad you found a workaround for the crash.

Unicode you say? Mmm, yes. Adjusting ws4ahk to return Unicode text wouldn't be too much work, although you're right that AHK doesn't have good handling for it.
_________________
-m35
Back to top
View user's profile Send private message
vashk



Joined: 07 Jan 2008
Posts: 11

PostPosted: Sat Feb 09, 2008 8:53 pm    Post subject: Reply with quote

The test script works fine. I've found the API documentation for this DLL to be wrong in a few places, so I'm not going to tear my hair out looking for the "best usage" award as long as I can get something that works. I managed to find and modify some VB code to convert Unicode text to UTF-8 URI-style hex codes before sending it back to AHK. Lack of Unicode support in AHK is such a pain... hacking it on seems to be the only option.
_________________
--v
Back to top
View user's profile Send private message
erictheturtle



Joined: 27 Jun 2007
Posts: 58
Location: California

PostPosted: Tue Feb 12, 2008 4:53 am    Post subject: Reply with quote

Thanks for testing that script, vashk. If it worked in VBS, it worries me that ws4ahk has the problem. I would really appreciate it if you could try one more script.

Could you test this VBS code (simialr to the previous code)?
Also, if you have Microsoft Office, could you also try it in a VBA macro as well?

If you can't be bothered with this, I understand.

Code:
Sub Test()

   ' fill in these values
   c_sprogid = "" ' WCaptureX PROGID
   m_sprogid = "" ' WMonitorX PROGID
   i_sprogid = "" ' WInput PROGID
   r_sprogid = "" ' WResult PROGID
   
   
   ' use the other script to get the x, y
   ' and HWND of the window and control
   mX = 0 'paste value here
   mY = 0 'paste value here
   w_hWnd = 0 'paste HWND here
   c_hWnd = 0 'paste HWND here
   
   
   s = "Function GetText(hwnd,x,y)" & VbCrLf & _
      "   objInput.Hwnd = hwnd" & VbCrLf & _
      "   objInput.StartX = x" & VbCrLf & _
      "   objInput.StartY = y" & VbCrLf & _
      "   objInput.EndX = x" & VbCrLf & _
      "   objInput.EndY = y" & VbCrLf & _
      "   objInput.ContextWordsLeft = 1" & VbCrLf & _
      "   objInput.ContextWordsRight = 1" & VbCrLf & _
      "   objInput.Options = 1" & VbCrLf & _
      "   Set GetText = capture.Capture(objInput)" & VbCrLf & _
      "End Function"
   
   
   Set capture   = CreateObject(c_sprogid)
   Set monitor   = CreateObject(m_sprogid)
   Set objInput  = CreateObject(i_sprogid)
   
   Set winscript = CreateObject("MSScriptControl.ScriptControl")
   winscript.Language = "VBScript"
   winscript.AddObject "capture" , capture 
   winscript.AddObject "monitor" , monitor 
   winscript.AddObject "objInput", objInput
   winscript.ExecuteStatement s
   
   winscript.ExecuteStatement "Call monitor.Start(2,2,True)"
   
   Set objResult = winscript.Eval("GetText(" & c_hWnd & ", " & mX & ", " & mY & ")")
   
   Msgbox objResult.Context
   Msgbox objResult.Text   
   Msgbox objResult.Locale
   Msgbox objResult.Font 

End Sub

Call Test()

_________________
-m35
Back to top
View user's profile Send private message
vashk



Joined: 07 Jan 2008
Posts: 11

PostPosted: Tue Feb 12, 2008 5:10 am    Post subject: Reply with quote

I won't be able to test soon, but I will see if I can try it next week. Thanks for investigating this so carefully. Most people don't care to bug-hunt. If you're feeling ambitious you can download the DLL itself (demo version, uhh let's not mention retail $$$). Just look up WordCaptureX.

AHK is taking up so much of my time. LOL @ "productivity enhancers."
_________________
--v
Back to top
View user's profile Send private message
Rad777



Joined: 05 Jul 2007
Posts: 9

PostPosted: Tue May 27, 2008 7:32 pm    Post subject: Could anyone convert this to get me started? Reply with quote

Code:

;   Public Function GetSource(screen, x1, y1, x2, y2)
   Dim selectionArea, HostObj
   Set HostObj = WScript.CreateObject("HostExplorer").Hosts(screen)
   Set selectionArea = HostObj.Area(x1, y1, x2, y2)
   selectionArea.select
   GetSource = HostObj.Selection
   Set selectionArea = Nothing
   Set HostObj = Nothing
;   End Function


I tried the following, without success:
Code:

#Include ws4ahk.ahk
WS_Initialize()
HostObj =
(
   Public Function GetSource(screen, x1, y1, x2, y2)
   Dim selectionArea, HostObj
   Set HostObj = WScript.CreateObject("HostExplorer").Hosts screen)
   Set selectionArea = HostObj.Area(x1, y1, x2, y2)
   selectionArea.select
   GetSource = HostObj.Selection
   Set selectionArea = Nothing
   Set HostObj = Nothing
   End Function
)
Ws_Exec(Code)
WS_Uninitialize()

Code:

WS_Exec("Set HostObj = GetObject(,""HostExplorer"")")
WS_Exec("Set selectionArea = HostObj.Area(x1, y1, x2, y2)")
WS_Exec("selectionArea.select")
WS_Exec("GetSource = HostObj.Selection")
WS_Exec("Set selectionArea = Nothing")
WS_Exec("Set HostObj = Nothing")


Neither one of the above worked. Thanks for any assistance provided!

-Rad777
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 676

PostPosted: Tue May 27, 2008 8:01 pm    Post subject: Reply with quote

Code:


#Include ws4ahk.ahk
WS_Initialize()
HostObj =
(
   Public Function GetSource(screen, x1, y1, x2, y2)
   Dim selectionArea, HostObj
   Set HostObj = WScript.CreateObject("HostExplorer").Hosts(screen)
   Set selectionArea = HostObj.Area(x1, y1, x2, y2)
   selectionArea.select
   GetSource = HostObj.Selection
   Set selectionArea = Nothing
   Set HostObj = Nothing
   End Function
)Ws_Exec(Code)
WS_Uninitialize()

and what about this?
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
erictheturtle



Joined: 27 Jun 2007
Posts: 58
Location: California

PostPosted: Tue May 27, 2008 9:00 pm    Post subject: Reply with quote

With a quick glance I see the problem of using the WScript object. The WScript object is unfortunately only available to scripts (vbs or js) that are run by the wscript.exe or cscript.exe programs.

Try changeing your
Code:
WScript.CreateObject(...)
to just
Code:
CreateObject(...)
and see what happens.
_________________
-m35
Back to top
View user's profile Send private message
pokercurious



Joined: 16 Dec 2007
Posts: 38

PostPosted: Tue May 27, 2008 9:29 pm    Post subject: Reply with quote

If the code you want to execute is...

Code:
HostObj =
(
   Public Function GetSource(screen, x1, y1, x2, y2)
   Dim selectionArea, HostObj
   Set HostObj = WScript.CreateObject("HostExplorer").Hosts(screen)
   Set selectionArea = HostObj.Area(x1, y1, x2, y2)
   selectionArea.select
   GetSource = HostObj.Selection
   Set selectionArea = Nothing
   Set HostObj = Nothing
   End Function
)


...shouldn't the code to execute it be...

Code:
Ws_Exec(HostObj)


...and not:

Code:
Ws_Exec(code)
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 676

PostPosted: Tue May 27, 2008 10:26 pm    Post subject: Reply with quote

pokercurious wrote:
If the code you want to execute is...

Code:
HostObj =
(
   Public Function GetSource(screen, x1, y1, x2, y2)
   Dim selectionArea, HostObj
   Set HostObj = WScript.CreateObject("HostExplorer").Hosts(screen)
   Set selectionArea = HostObj.Area(x1, y1, x2, y2)
   selectionArea.select
   GetSource = HostObj.Selection
   Set selectionArea = Nothing
   Set HostObj = Nothing
   End Function
)


...shouldn't the code to execute it be...

Code:
Ws_Exec(HostObj)


...and not:

Code:
Ws_Exec(code)


I am assuming this is directed at me
yes that is correct
copy paste error
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next
Page 5 of 8

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group