#SB_GETTEXT result in crash

Just starting out? Need help? Post your questions and find answers here.
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

#SB_GETTEXT result in crash

Post by Otrebor »

Hi,

This works ok.

Code: Select all

Nb=SendMessage_(Handle_statusbar,#SB_GETPARTS,0,0) 
len=SendMessage_(Handle_statusbar, #SB_GETTEXTLENGTH, 0, 0)
But this is causing crash in the program i want to read.

Code: Select all

text.s=Space(len)
SendMessage_(Handle_statusbar, #SB_GETTEXT, 0, @text)
Can someone tell what is wrong?
Thank you!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: #SB_GETTEXT result in crash

Post by Dude »

You haven't shown us any other code or how you made the StatusBar (PureBasic or API?) but look at the following code.

Also, searching the forums reveals: http://www.purebasic.fr/english/viewtop ... 0&p=349833 ;)

Code: Select all

If OpenWindow(0, 100, 150, 300, 100, "PureBasic - StatusBar Example", #PB_Window_SystemMenu | #PB_Window_SizeGadget)

  If CreateStatusBar(0, WindowID(0))
    AddStatusBarField(100)
    AddStatusBarField(50)
    AddStatusBarField(100)
  EndIf

  num=Random(999)

  StatusBarText(0, 0, "Area 1")
  StatusBarText(0, 1, Str(num), #PB_StatusBar_BorderLess)
  StatusBarText(0, 2, "Area 3", #PB_StatusBar_Right | #PB_StatusBar_Raised)

  text.s=Space(999)
  SendMessage_(StatusBarID(0), #SB_GETTEXT, 1, @text)
  Debug text ; Should be same as "num" variable.

  Repeat

  Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: #SB_GETTEXT result in crash

Post by netmaestro »

Code: Select all

Nb=SendMessage_(Handle_statusbar,#SB_GETPARTS,0,0) 
len=SendMessage_(Handle_statusbar, #SB_GETTEXTLENGTH, 0, 0)


; But this is causing crash in the program i want to read.
; Code:
text.s=Space(len)
SendMessage_(Handle_statusbar, #SB_GETTEXT, 0, @text)
You appear to be using the raw return value of SB_GETTEXTLENGTH. In fact, only the loword contains the length. The hiword contains other information, resulting in a much much higher number and it's probably causing your crash. Try:

Code: Select all

len=SendMessage_(Handle_statusbar, #SB_GETTEXTLENGTH, 0, 0)
len=len&FFFF
Sendmessage_(... etc.
BERESHEIT
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: #SB_GETTEXT result in crash

Post by Otrebor »

Thank's Dude and netmaestro
Still crashing...

This is the program i want to read the statusbar:
WZebra
http://www.radagast.se/othello/index.html
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Re: #SB_GETTEXT result in crash

Post by hallodri »

The external program can only write their own memory. Try with VirtualAllocEx and ReadProcessMemory.
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: #SB_GETTEXT result in crash

Post by Otrebor »

hallodri wrote:The external program can only write their own memory. Try with VirtualAllocEx and ReadProcessMemory.
Oh...I will try to find some example on the forum.
Thank you.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4661
Joined: Sun Apr 12, 2009 6:27 am

Re: #SB_GETTEXT result in crash

Post by RASHAD »

Workaround suitable for any external object

Code: Select all

Prototype.l ProtoAccessibleObjectFromPoint(x.l,y.l,*ia,*var)
Global AccessibleObjectFromPoint.ProtoAccessibleObjectFromPoint,r.RECT, hdll,name.string

CoInitialize_(0)
hdll=OpenLibrary(#PB_Any,"Oleacc.dll")
AccessibleObjectFromPoint=GetFunction(hdll,"AccessibleObjectFromPoint")

Procedure TextFromWindowPosition(*Name.String)
  Define vt.VARIANT,*pIAcc.IAccessible,pName.l,len.l
  If AccessibleObjectFromPoint(r\left+20,r\top+20,@*pIAcc,@vt)=#S_OK ;Use the coordinate of the proper StatusBar Part
    *Name\s=""
    If *pIAcc\get_accName(vt, @pName) = #S_OK
      len = SysStringLen_(pName)
      *Name\s = Space(len)
      CompilerIf #PB_Compiler_Unicode  
          PokeS( @*Name\s,PeekS(pName))
      CompilerElse
          WideCharToMultiByte_(#CP_ACP, 0,pName, -1, @*Name\s, len, 0, 0) 
      CompilerEndIf
      SysFreeString_(pName)
    EndIf
  EndIf
  ProcedureReturn #True
EndProcedure

Repeat
   hwnd = FindWindow_(0,"WZebra")
Until hwnd
Repeat
   hwnd2 = FindWindowEx_(hwnd,0,"msctls_statusbar32",0)
Until hwnd2
GetWindowRect_(hwnd2,r.RECT)
SetForegroundWindow_(hwnd)
TextFromWindowPosition(@name)

Debug name\s
SetClipboardText(name\s)

CoUninitialize_() 
CloseLibrary(hdll) 
Egypt my love
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: #SB_GETTEXT result in crash

Post by Otrebor »

Thank's RASHAD, unfortunately it's returning the text on titlebar (WZebra) :|

I found some related topic here:
http://www.purebasic.fr/english/viewtop ... ualAllocEx

and i'm trying to adapt:

Code: Select all

hWin = FindWindow_(0, "WZebra")
Handle_statusbar = FindWindowEx_(hWin,0,"msctls_statusbar32",0)


sBufferSize = SendMessage_(Handle_statusbar, #SB_GETTEXTLENGTH, 0, 0)
sBufferSize = (sBufferSize &$FFFF)
sBuffer.s = Space(sBufferSize)


GetWindowThreadProcessId_(Handle_statusbar, @ProcID)
hProc = OpenProcess_(#PROCESS_ALL_ACCESS, 0, ProcID)
Result = VirtualAllocEx_(hProc, 0, SizeOf(sBufferSize), #MEM_COMMIT, #PAGE_READWRITE)
SendMessage_(Handle_statusbar, #SB_GETTEXT, 0, @Result)

But still crashing...
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: #SB_GETTEXT result in crash

Post by Otrebor »

Hi
Now it's working :)

Code: Select all

hWin = FindWindow_(0, "WZebra")
Handle_statusbar = FindWindowEx_(hWin,0,"msctls_statusbar32",0)


sBufferSize = SendMessage_(Handle_statusbar, #SB_GETTEXTLENGTH, 0, 0)
sBufferSize = (sBufferSize &$FFFF)
sBuffer.s = Space(sBufferSize)


GetWindowThreadProcessId_(Handle_statusbar, @ProcID)
hProc = OpenProcess_(#PROCESS_ALL_ACCESS, 0, ProcID)
*Result = VirtualAllocEx_(hProc, 0, SizeOf(sBufferSize), #MEM_COMMIT, #PAGE_READWRITE)
SendMessage_(Handle_statusbar, #SB_GETTEXT, 0, *Result)
ReadProcessMemory_(hProc, *Result, sBuffer, sBufferSize, 0)
Debug sBuffer
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: #SB_GETTEXT result in crash

Post by Otrebor »

Hi

The above code usually works with winXP.
But does not with Win Vista 32.

I checked every step is ok.
The problem seems to be here:

Code: Select all

ReadProcessMemory_(hProc, *Result, sBuffer, sBufferSize, 0)
After run this line sBuffer.s = "".
But sBufferSize returns 25!

Someone knows what is wrong?
Post Reply