WindowFromPoint_()?

Just starting out? Need help? Post your questions and find answers here.
dongnanyanhai
User
User
Posts: 10
Joined: Thu Aug 05, 2010 4:15 pm

WindowFromPoint_()?

Post by dongnanyanhai »

Hi.
I'v got a problem when using the WindowsAPI "WindowFromPoint_(m)".Why the code below alway returns zero?And how can I use the "WindowFromPoint_()" function?

Code: Select all

m.point
m\x = 100
m\y = 100
Debug WindowFromPoint_(m)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: WindowFromPoint_()?

Post by Trond »

It's a long story, but PB does not support passing structures as parameters by value, only by reference. WindowFromPoint_() is one of the rare functions that need a structure by value. We can work around this by pretending the structure is a single quad, since both a quad and a POINT is 8 bytes:

Code: Select all

m.point
m\x = 100
m\y = 100
Debug WindowFromPoint_(PeekQ(@m))
Last edited by Trond on Wed Aug 18, 2010 9:13 pm, edited 1 time in total.
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: WindowFromPoint_()?

Post by Arctic Fox »

Another workaround which seems to work:

Code: Select all

m.point
m\x = 100
m\y = 100
Debug WindowFromPoint_(m\y << 32 + m\x)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: WindowFromPoint_()?

Post by Trond »

And two more for the collectors out there: :lol:

Code: Select all

m.q
PokeL(@m, 100)
PokeL(@m+4, 100)
Debug WindowFromPoint_(m)

Code: Select all

Structure QuadPointUnion
  StructureUnion
    p.POINT
    q.q
  EndStructureUnion
EndStructure

m.QuadPointUnion
m\p\x = 100
m\p\y = 100
Debug WindowFromPoint_(m\q)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: WindowFromPoint_()?

Post by Trond »

Oh I just can't stop! I guess I forgot to drink my brake fluid today.

Code: Select all

m.POINT
m\x = 100
m\y = 100
Debug WindowFromPoint_(Val("$"+RSet(Hex(m\y), 8, "0")+RSet(Hex(m\x), 8, "0")))
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: WindowFromPoint_()?

Post by Trond »

There is simply NO MERCY!

Code: Select all

Import ""
  WindowFromPoint(X.l, Y.l)
EndImport

m.POINT
m\x = 100
m\y = 100
Debug WindowFromPoint(m\x, m\y)
dongnanyanhai
User
User
Posts: 10
Joined: Thu Aug 05, 2010 4:15 pm

Re: WindowFromPoint_()?

Post by dongnanyanhai »

Thank you for all of your replies.
Now I will try to use it in my code!
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: WindowFromPoint_()?

Post by Fred »

Trond wrote:There is simply NO MERCY!

Code: Select all

Import ""
  WindowFromPoint(X.l, Y.l)
EndImport

m.POINT
m\x = 100
m\y = 100
Debug WindowFromPoint(m\x, m\y)
Try that one on x64 ;)
dongnanyanhai
User
User
Posts: 10
Joined: Thu Aug 05, 2010 4:15 pm

Re: WindowFromPoint_()?

Post by dongnanyanhai »

Trond wrote:It's a long story, but PB does not support passing structures as parameters by value, only by reference. WindowFromPoint_() is one of the rare functions that need a structure by value. We can work around this by pretending the structure is a single quad, since both a quad and a POINT is 8 bytes:

Code: Select all

m.point
m\x = 100
m\y = 100
Debug WindowFromPoint_(PeekQ(@m))
Hi,Trond.

I want to know does(or is?) the WinAPI "Process32First" also need a structure by value?

The code below always return Zero.

Code: Select all

Structure pp
  StructureUnion
    p.Processentry32
  EndStructureUnion
EndStructure 
Global pe.pp
Procedure.s GetWinName(shandle.l)
  oPid.l
  GetWindowThreadProcessId_(shandle,@oPid)
  Pth = CreateToolhelp32Snapshot_(#TH32CS_SNAPALL,oPid)
  bOk = Process32First_(pth,pe\p)
  Debug bOK
  While bOk
    If pe\p\th32processID = opid
      Debug pe\p\szexefile
    EndIf
    bOk = Process32Next_(Pth,@pe)
  Wend
EndProcedure

h = OpenWindow(0,0,0,300,160,"欧耶")
Debug GetwinName(h)
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: WindowFromPoint_()?

Post by cas »

You forgot one thing:
http://msdn.microsoft.com/en-us/library/ms684834%28VS.85%29.aspx wrote:The calling application must set the dwSize member of PROCESSENTRY32 to the size, in bytes, of the structure.

Code: Select all

Procedure.s GetWinName(shandle.l)
  pe.Processentry32
  pe\dwSize = SizeOf(Processentry32) ; <-------------------HERE
  oPid.l
  GetWindowThreadProcessId_(shandle,@oPid)
  Pth = CreateToolhelp32Snapshot_(#TH32CS_SNAPALL,oPid)
  bOk = Process32First_(pth,pe)
  
  While bOk
    If pe\th32processID = opid
      procname$=Space(#MAX_PATH)
      CopyMemory(@pe\szExeFile,@procname$,#MAX_PATH)
      CloseHandle_(Pth)
      ProcedureReturn procname$
    EndIf
    bOk = Process32Next_(Pth,@pe)
  Wend
EndProcedure

h = OpenWindow(0,0,0,300,160,"??")
Debug GetwinName(h)
dongnanyanhai
User
User
Posts: 10
Joined: Thu Aug 05, 2010 4:15 pm

Re: WindowFromPoint_()?

Post by dongnanyanhai »

cas wrote:You forgot one thing:
http://msdn.microsoft.com/en-us/library/ms684834%28VS.85%29.aspx wrote:The calling application must set the dwSize member of PROCESSENTRY32 to the size, in bytes, of the structure.

Code: Select all

Procedure.s GetWinName(shandle.l)
  pe.Processentry32
  pe\dwSize = SizeOf(Processentry32) ; <-------------------HERE
  oPid.l
  GetWindowThreadProcessId_(shandle,@oPid)
  Pth = CreateToolhelp32Snapshot_(#TH32CS_SNAPALL,oPid)
  bOk = Process32First_(pth,pe)
  
  While bOk
    If pe\th32processID = opid
      procname$=Space(#MAX_PATH)
      CopyMemory(@pe\szExeFile,@procname$,#MAX_PATH)
      CloseHandle_(Pth)
      ProcedureReturn procname$
    EndIf
    bOk = Process32Next_(Pth,@pe)
  Wend
EndProcedure

h = OpenWindow(0,0,0,300,160,"??")
Debug GetwinName(h)

Yeah, You are right!
Now I set the dwsize and this is my new code!Thank you,cas!

Code: Select all

Global pe.Processentry32
pe\dwsize = SizeOf(Processentry32)
Procedure.s GetWinName(shandle.l)
  oPid.l
  GetWindowThreadProcessId_(shandle,@oPid)
  Pth = CreateToolhelp32Snapshot_(#TH32CS_SNAPALL,0)
  bOk = Process32First_(pth,pe)
  While bOk
    If pe\th32processID = opid
      Debug PeekS(@pe\szexefile, -1, #PB_Ascii)
    EndIf
    bOk = Process32Next_(Pth,pe)
  Wend
EndProcedure

h = OpenWindow(0,0,0,300,160,"欧耶")
Debug GetwinName(h)
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: WindowFromPoint_()?

Post by cas »

I don't see use of global variable "pe.Processentry32" in your code and you are still having memory leak because there is missing "CloseHandle_(Pth)". :wink:
You can always use this to get exe name of your own application:

Code: Select all

Debug GetFilePart(ProgramFilename())
dongnanyanhai
User
User
Posts: 10
Joined: Thu Aug 05, 2010 4:15 pm

Re: WindowFromPoint_()?

Post by dongnanyanhai »

cas wrote:I don't see use of global variable "pe.Processentry32" in your code and you are still having memory leak because there is missing "CloseHandle_(Pth)". :wink:
You can always use this to get exe name of your own application:

Code: Select all

Debug GetFilePart(ProgramFilename())
Thank you,cas!
I forget to close the handle used.
I want to get other process's filename so I used the main window's handle to test the procedure-"GetWinName".

I've try to use "PBOSL" - PureBasic OpenSource Libraries,there is a function call "GetProcessName(optional PID)" in the "PBOSL".When using that function ,I always get a error "The following PureLibrary is missing: StringExtension" and can not solve it.Than I had to use the WinAPI to get the process's filename.

Sorry for my English !
Post Reply