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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Another Program's Text (Through SendMessage Or OCR)

Status
Not open for further replies.

ApocY85

Programmer
Dec 20, 2007
36
US
Hello!

I am new to API calls that take use of a device context. I just read up a little on what a device context is, and have been working on a certain "test" procedure that ultimately will be used at work.

The procedure that I am trying to get to work will actually be used for two circumstances:

1) A little while back, I originally attempted to pull the text from another application's window. The reason was to monitor the transfer of data through the application's proprietary transfer process. I can no longer remember, but I believe I used SendMessage with a WM_GETTEXT parameter. Anyhow, I was able to retrieve nearly all the text from the window except for the actual text I was focused on retrieving. I believe the text that I wanted and couldn't get would not work because it was hitting a proprierty control labeled as a "Static" type. So, I thought I could take a screenshot of the window and run OCR to get the text.

2) I need to monitor a command shell window to determine when it is ready for input. Normally, I could simply pipe the text to a file and monitor the file, however the text I am looking for will not pipe to a file. I believe due to security purposes, piping was deliberatly blocked (When using a RunAs command, it prompts for a password. I would like to use a better method than simply "sleeping" for a few seconds). Once again, I considered using OCR on an image of the window.

So, I came up with the following code (please excuse it's messiness as I was simply trying to throw something together to see if I could even get it working):


Private Sub testSub()
Dim Hwndwindow As Long
Dim Form1DC As Long
Dim lMemoryDC As Long
Dim lBmp, lOldBmp As Long
Dim modiDoc As New MODI.Document
Dim modiImg As New MODI.Image
Dim rgxStringSearch As New RegExp
Dim strRegexResults() As String

Do
Hwndwindow = 5704966 'I just grabbed the hWnd from something similar to Spy++

Form1DC = GetDC(0)


lMemoryDC = CreateCompatibleDC(Form1DC)
'lBmp = CreateCompatibleBitmap(Form1DC, 347, 275)
lBmp = CreateCompatibleBitmap(Form1DC, 300, 300)

lOldBmp = SelectObject(lMemoryDC, lBmp)

DeleteObject lOldBmp
DeleteObject lBmp

PrintWindow Hwndwindow, lMemoryDC, 0



Dim testDC, testBmp, testOldBmp As Long
testDC = CreateCompatibleDC(Form1DC)
'lBmp = CreateCompatibleBitmap(Form1DC, 347, 275)
testBmp = CreateCompatibleBitmap(Form1DC, 300, 300)
testOldBmp = SelectObject(testDC, testBmp)
DeleteObject testOldBmp
DeleteObject testBmp
MsgBox StretchBlt(testDC, 0, 0, 300, 300, lMemoryDC, 100, 100, 100, 100, SRCCOPY)


If Dir("C:\Documents and Settings\myID\Desktop\testPic.bmp") <> "" Then
Kill "C:\Documents and Settings\myID\Desktop\testPic.bmp"
End If
stdole.SavePicture GetBitmapPic(lBmp), "C:\Documents and Settings\myID\Desktop\testPic.bmp"


If Dir("C:\Documents and Settings\myID\Desktop\testPic2.bmp") <> "" Then
Kill "C:\Documents and Settings\myID\Desktop\testPic2.bmp"
End If
stdole.SavePicture GetBitmapPic(testBmp), "C:\Documents and Settings\myID\Desktop\testPic2.bmp"




modiDoc.Create "C:\Documents and Settings\myID\Desktop\testPic.bmp"

'can I skip the file?

modiDoc.OCR miLANG_ENGLISH, False, False

Set modiImg = modiDoc.Images(0)

rgxStringSearch.Search modiImg.Layout.Text, "password", False, strRegexResults

If strRegexResults(0) = "password" Then
MsgBox "yes"
Else
MsgBox "no"
End If

DeleteDC Form1DC
DeleteDC lMemoryDC

Loop While 1

End Sub


When I view the file "testPic.bmp", the image is as I had expected. However, "testPic2.bmp" always gives a pure-black image, no matter what parameters I use in StretchBlt. I would like to pin-point the image retrieved to the specific section I would like to look at, rather than the upper-left portion of the window.

Could anyone be of any assistance? Please be patient as I am new to this type of thing. Also, if you know of a way I can simply avoid creating a file for the OCR to grab the image, that would be helpful as well, as I assume writing a new file over and over would be inefficient.

Thank you in advance for all of your help and patience! If you know of any better method for these two examples, feel free to offer your suggestions! Perhaps there is something I am not aware of with SendMessage to where I could skip the OCR process altogether. Or, perhaps the password-prompting text CAN be piped an I did something wrong (I could get it to pipe manually, but not through code). Thanks again for your help!
 
Unfortunately, MODI won't OCR without an image file.
If you're really feeling up to it, I know the Good Folks at Google put some OCR tools together called Tesseract, which turned into OCRopus.
But I really don't know its capabilities.

Have you checked out the API-Guide? Maybe you can suss out a different message to use.
 
Don't delete your bitmap objects until after you have finished using them.
 
Thank you for your responses! Before I was able to read them, I was able to get it working. I was deleting a bitmap before using it. Also, certain bitmap-dimension values that I was using as StretchBlt parameters were causing me to "look past" the original image, so the copied image was pure-black since the API apparently cannot return part of an image that does not exist.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top