Forum: Ruby calling winapi for EnumDisplayMonitors

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A07f0662e96a632d9447a710adadbe05?d=identicon&s=25 Phil Smy (phileuro)
on 2009-05-06 13:59
I am trying to write a desktop application in Ruby. We need to find out
what monitors are attached to the XP machine this will run on.
I looked at how SWT does this and it is accomplished via calling
EnumDisplayMonitors (windows api).

I cannot figure out how to call this from within Ruby using Win32API.
EnumDisplayMonitors's api is this:
BOOL EnumDisplayMonitors(
  HDC hdc,
  LPCRECT lprcClip,
  MONITORENUMPROC lpfnEnum,
  LPARAM dwData
);

How can I supply a proc? Is what I want to do even possible?

Any help would be appreciated. All this kind of stuff is not very
clearly documented for the novice and I can't find any relevant samples.
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-05-06 16:44
(Received via mailing list)
Hi,

2009/5/6 Phil Smy <phil@philsmy.com>:
>  MONITORENUMPROC lpfnEnum,
>  LPARAM dwData
> );
>
> How can I supply a proc? Is what I want to do even possible?
>
> Any help would be appreciated. All this kind of stuff is not very
> clearly documented for the novice and I can't find any relevant samples.

As you know, Win32API doesn't support callback.
Try with Ruby/DL or win32-api gem.

Here is a win32-api sample:

require 'win32/api'

EnumDisplayMonitors = Win32::API.new('EnumDisplayMonitors', 'LPKL',
'B', 'user32')
GetMonitorInfo = Win32::API.new('GetMonitorInfo', 'LP', 'B', 'user32')

MyInfoEnumProc = Win32::API::Callback.new('LLPL','L') {
|hMonitor,hdcMonitor,lprcMonitor,dwData|
  lpmi = [72].pack('L') + 0.chr * 68
  GetMonitorInfo.call(hMonitor,lpmi)
  info = lpmi.unpack('L10Z*')
  puts "Name:#{info[10]}, Width:#{info[3]}, Height:#{info[4]} "
  1
}

EnumDisplayMonitors.call(0, nil, MyInfoEnumProc, 0)


Regards,
Park Heesob
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-05-06 21:21
Phil Smy wrote:
> I am trying to write a desktop application in Ruby. We need to find out
> what monitors are attached to the XP machine this will run on.
> I looked at how SWT does this and it is accomplished via calling
> EnumDisplayMonitors (windows api).

Ruby wmi is also quite nice (dunno if it helps here though).
http://betterlogic.com/roger/?p=1291
A07f0662e96a632d9447a710adadbe05?d=identicon&s=25 Phil Smy (phileuro)
on 2009-05-07 10:57
Hi Park,
In short - you rule!
Thanks - this does exactly what we need. I guess I need to do some
reading up on win32-api to understand HOW it does it, but this is a
great start.
Much appreciated.

Heesob Park wrote:
> Here is a win32-api sample:
>
> require 'win32/api'
>
> EnumDisplayMonitors = Win32::API.new('EnumDisplayMonitors', 'LPKL',
> 'B', 'user32')
> GetMonitorInfo = Win32::API.new('GetMonitorInfo', 'LP', 'B', 'user32')
>
> MyInfoEnumProc = Win32::API::Callback.new('LLPL','L') {
> |hMonitor,hdcMonitor,lprcMonitor,dwData|
>   lpmi = [72].pack('L') + 0.chr * 68
>   GetMonitorInfo.call(hMonitor,lpmi)
>   info = lpmi.unpack('L10Z*')
>   puts "Name:#{info[10]}, Width:#{info[3]}, Height:#{info[4]} "
>   1
> }
>
> EnumDisplayMonitors.call(0, nil, MyInfoEnumProc, 0)
>
>
> Regards,
> Park Heesob
This topic is locked and can not be replied to.