Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automation Error with GetWindowText and Dynawrap

Status
Not open for further replies.

mnik3

Technical User
May 11, 2009
10
US
I am using Dynawrap to get text from a dialog box, and when I try to grab anything of the class "Static", I receive the following error: "Run-time error '-2147417848 (80010108)': Automation error. The object invoked has disconnected from its clients." I wrote the code for VBS but copied the code into a VBA module to debug and step through the code. It will get the text from a button in the dialog box and from the title of the dialog box, but not the text in the box. If someone could help, I would appreciate it! The following code is what I'm using:

[blue]<snip>
dim sWindowTitle

hwnd = clng(&H02000534) [!]'handle to the desired window[/!]
dasLength = GetWindowTextLength(hwnd)
sWindowTitle = String(dasLength + 1, vbnullchar)[!]'to create the necessary buffer space[/!]
charnum = GetWindowText(hwnd, sWindowTitle, dasLength + 1)
MsgBox (sWindowTitle)

Function GetWindowText(hwnd, txt, char)
Set oDW = Nothing [!]'remove previous instances[/!]
Set oDW = CreateObject("DynamicWrapper") [!]'create new instance[/!]
oDW.Register "USER32.DLL", "GetWindowTextA", "i=lrl", "f=s", "r=l" [!]'register the dll for use[/!]
GetWindowText = oDW.GetWindowTextA(hwnd, txt, char)
End Function

Function GetWindowTextLength(hwnd)
Set oDW = Nothing
Set oDW = CreateObject("DynamicWrapper")
oDW.Register "USER32.DLL", "GetWindowTextLengthA", "i=h", "f=s", "r=l"
GetWindowTextLength = oDW.GetWindowTextLengthA(hwnd)
End Function
</snip>[/blue]
 
>It will get the text from a button in the dialog box and from the title of the dialog box...
If I look at your function on getting window text, I don't think it will return windowtext of any window. It is largely incorrectly implemented. So before I could help, I need to know if you mean what you said and/or said what you mean.
 
Um - dynawrap implementation looks OK to me ...

I guess the question is how are you finding the handle of the Static window that contains the dialog box text; i.e. are you sure you have the right handle?
 
If you enjoy more rhetoric, maybe I would say "largely [blue]insufficiently[/blue] implemented", and "incorrect" only in the sense of the function GetWindowText itself to meet its purpose.
 
I'm sorry, tsuji, but I have absolutely no idea what you are trying to say.
 
Let the op's re-surface to put substance to the question. I don't think "implementation looks ok" is meaning much more neither.
 
Tsuji, it WILL retrieve the window text, however, the specified return value in "i=lrl" of r, which is to pass by reference, seems to be limited to 48 characters. I disagree that the implementation is incorrect. There is only one other similar function that I know of; InternalGetWindowText does approximately the same thing, and neither achieve my goal. What I was attempting to do is retrieve the dialog from a cmd prompt net send dialog box.
Strongm, yes I have the correct handle. I am using a program similar to spy++ to see it. I just hadn't put in the FindWindow portion of the code at the time I posted this question.
 
[0] Well that make things clearer. If I get that outcome, I would say that is correct. Besides, I don't think what you said really reflecting what gave you in msgbox! I know quite a bit of that domain. You said implementation is negative of incorrect, fine. If you see how the correct outcome can return, people would change their mind. Correct? that is always comforting. You stay with the correctness, ok.

[1] You can keep the getwindowtextlength, it is simpler. The rest I would say do this or variation according to your taste and originality.
[tt]
Function GetWindowText(hWnd, byref ttl, n)
dim odw, nret, ttlbuf
set odw=createobject("DynamicWrapper")
odw.Register "user32.dll", "GetWindowTextA", "i=hll","f=s","r=l"
odw.Register "kernel32.dll", "MultiByteToWideChar", "i=llllll","f=s","r=l"
ttlbuf=string(1024,chr(0))
ttl=string(n,chr(0))
nret=odw.GetWindowTextA(hWnd,pBSTRAddr(ttlbuf),len(ttlbuf)*2)
odw.MultiByteToWideChar 0, 0, pBSTRAddr(ttlbuf), -1, pBSTRAddr(ttl), len(ttl)*2 'CP_ACP=0
GetWindowText=nret
set odw=nothing
End Function

function pBSTRAddr(byref s)
dim sp, rp, odw, odw2
set odw=createobject("DynamicWrapper")
set odw2=odw
odw.register "kernel32.dll", "lstrcat", "i=ws","f=s","r=l"
odw2.register "kernel32.dll", "RtlMoveMemory", "i=lll","f=s","r=l"
sp=odw.lstrcat(s,"")
pBSTRAddr=""
rp=odw.lstrcat(pBSTRAddr,"")
pBSTRAddr=clng(0)
odw2.RtlMovememory rp+8, sp+8, 4
set odw2=nothing
set odw=nothing
end function
[/tt]
[2] You can do supplementary checking in the main. In any case, if the hwnd is properly set up, the echo/msgbox will be showing sensible character encoding string.
[tt]
'etc etc
[red]'[/red]sWindowTitle = String(dasLength + 1, vbnullchar) 'buffer and all that is taken care of in function and it is an out-param in any case
charnum = GetWindowText(hwnd, sWindowTitle, dasLength + 1) 'last param has some degree of freedom
[red]'[/red]MsgBox (sWindowTitle)
if dasLength=charnum then
MsgBox charnum & vbcrlf & sWindowTitle
else
msgbox "inconsistent: error"
end if
[/tt]
 
>Self: If I get that outcome, I would say that is correct.
If I get that outcome, I would[red]n't[/red] say that is correct.
 
That's a workaround for versions of dynawrap that do not support William Epp's 'r' parameter type - and from the original post it is indicated that 'r' is supported.

It'll be interesting to see whether the workaround fixes an issue the library is already supposed to deal with.
 
So you mean it works with r, or you mean you are interested in seeing if it works. Can't figure out what you really mean.
 
The original post makes it perfectly clear that

a) they are using a version of DynaWrap that supports the 'r' option
b) that their correctly implemented code, using 'r', works fine when retrieving window text from standard windows and controls, It only seems to have a problem when retrieving the text from a Static text control.

This would suggest that maybe, just maybe, Dynawrap has some internal issues dealing with some OUT string parameters passed by reference, 'r' (i.e. a bug).

And given that there might be an internal bug in the 'r' implementation, it will therefore be interesting to see whether your workaround - originally written for versions of Dynawrap that didn't support 'r', and therefore bypassing Dynawrap's own internal implementation of functions similar to both MultiByteToWideChar and pBSTRAddr (if you've seen the dynawrap source code, then I'm basically talking about A2W and VariantCopy respectively) - solves the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top